在企业级开发中,Redis可以用做数据库,缓存(热点数据(经常会被查询,但是不经常被修改或者删除的数据))和消息中间件等大部分功能,使用频率非常高,下面对redis的安装和配置做详细介绍。
1、安装
- 下载安装包
Redis官网下载地址
上传到路径 /home/sophia/ 下
- 解压
tar -xvf redis-5.0.0.tar.gz
- 编译安装
mv redis-5.0.0 redis
cd redis
make && make install
2、配置
修改安装目录下的redis.conf文件
vim redis.conf
修改以下配置
#bind 127.0.0.1 # 将这行代码注释,监听所有的ip地址,外网可以访问
protected-mode no # 把yes改成no,允许外网访问
daemonize yes # 把no改成yes,后台运行
requirepass root #设置密码
3、启动或停止
redis提供了服务端命令和客户端命令:
- redis-server 服务端命令,可以包含以下参数:
start 启动
stop 停止 - redis-cli 客户端控制台,包含参数:
-h xxx 指定服务端地址,缺省值是127.0.0.1
-p xxx 指定服务端端口,缺省值是 6379
4、设置开机启动
1)输入命令,新建文件
vim /etc/init.d/redis
输入下面内容:
#!/bin/sh
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/home/leyou/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
然后保存退出
注意:以下信息需要根据安装目录进行调整:
EXEC=/usr/local/bin/redis-server # 执行脚本的地址
REDIS_CLI=/usr/local/bin/redis-cli # 客户端执行脚本的地址
PIDFILE=/var/run/redis.pid # 进程id文件地址
CONF="/home/sophia/redis/redis.conf" #配置文件地址
2)设置权限
chmod 755 /etc/init.d/redis
3)启动测试
/etc/init.d/redis start
启动成功会提示以下信息:
Starting Redis server...
Redis is running...
4)设置开机自启动
chkconfig --add /etc/init.d/redis
chkconfig redis on
注意:防火墙一定要关闭,要不然配置文件中把保护节点关闭了 protected-mode no, 本机上也会提示访问不到,提示以下信息:DENIED Redis is running in protected mode because protected mode is enabled
1 Caused by: redis.clients.jedis.exceptions.JedisDataException:
2 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified,
no authentication password is requested to clients.
3 In this mode connections are only accepted from the loopback interface.
4 If you want to connect from external computers to Redis you may adopt one of the following solutions:
5 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
6 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
7 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option.
8 4) Setup a bind address or an authentication password.
9
10 NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.