上篇“CentOS 6.5下Redis安装记录”,只是安装了,可以运行可以用了,但是系统重启之后并不会随系统自动启动,用起来很不方便,这里就把CentOS 6.5下Redis开机启动配置记录说一下。
下载安装
参考:“CentOS 6.5下Redis安装记录”
如果你只是执行了Make,要配置开机启动还需要执行:
sudo make installinstall的时候,redis的命令会被拷贝到/usr/local/bin下面
复制配置文件到 /etc 目录下
cp redis.conf /etc
建立用户与日志目录
建议为Redis单独建立一个用户,并新建data和日志文件夹
sudo useradd redis sudo mkdir -p /var/lib/redis sudo mkdir -p /var/log/redis sudo chown redis.redis /var/lib/redis sudo chown redis.redis /var/log/redis
修改配置文件
vi /etc/redis.conf修改绑定的IP,解决本机之外其它IP无法访问的问题(如果需要在其它电脑上访问);
################################## NETWORK ##################################### # By default, if no "bind" configuration directive is specified, Redis listens # for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected interfaces using # the "bind" configuration directive, followed by one or more IP addresses. # # Examples: # # bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 # # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ bind 0.0.0.0默认是“bind 127.0.0.1 ::1”,改为“bind 0.0.0.0”;
修改启动模式为后台启动
################################# GENERAL ##################################### # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize yesdaemonize yes
修改数据文件存储位置
# The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir /var/lib/redis注意:是指定一个目录,不带文件名;
配置init脚本
vi /etc/init.d/redis
# 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="/etc/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 ##############################注意开头的两句:
# chkconfig: 2345 90 10 # description: Redis is a persistent key-value database这虽然是注释,但要是没有它,就会报错:service redis does not support chkconfig
添加执行权限
chmod +x /etc/init.d/redis
设定开机启动服务
sudo chkconfig redis on
启动,停止redis
service redis start service redis stop或者:
/etc/init.d/redis start /etc/init.d/redis stop
测试redis
# redis-cli 127.0.0.1:6379> set key 123 OK 127.0.0.1:6379> get key "123" 127.0.0.1:6379> exit你也可以使用Telnet来测试:
telnet 192.168.1.100 6379连接之后执行相同的命令就行。