CentOS 6.5下Redis开机启动配置记录

Linux就该这么学

CentOS 6.5下Redis开机启动配置记录说一下。

下载安装

参考:“CentOS 6.5下Redis安装记录

如果你只是执行了Make,要配置开机启动还需要执行:

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. sudo make install  

install的时候,redis的命令会被拷贝到/usr/local/bin下面

 

复制配置文件到 /etc 目录下

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. cp redis.conf /etc  

 

建立用户与日志目录

建议为Redis单独建立一个用户,并新建data和日志文件夹

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. sudo useradd redis  
  2. sudo mkdir -p /var/lib/redis  
  3. sudo mkdir -p /var/log/redis  
  4. sudo chown redis.redis /var/lib/redis  
  5. sudo chown redis.redis /var/log/redis  

修改配置文件

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. vi /etc/redis.conf  

修改绑定的IP,解决本机之外其它IP无法访问的问题(如果需要在其它电脑上访问);

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. ################################## NETWORK #####################################  
  2.   
  3. # By default, if no "bind" configuration directive is specified, Redis listens  
  4. # for connections from all the network interfaces available on the server.  
  5. # It is possible to listen to just one or multiple selected interfaces using  
  6. # the "bind" configuration directive, followed by one or more IP addresses.  
  7. #  
  8. # Examples:  
  9. #  
  10. # bind 192.168.1.100 10.0.0.1  
  11. # bind 127.0.0.1 ::1  
  12. #  
  13. # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the  
  14. # internet, binding to all the interfaces is dangerous and will expose the  
  15. # instance to everybody on the internet. So by default we uncomment the  
  16. # following bind directive, that will force Redis to listen only into  
  17. # the IPv4 lookback interface address (this means Redis will be able to  
  18. # accept connections only from clients running into the same computer it  
  19. # is running).  
  20. #  
  21. # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES  
  22. # JUST COMMENT THE FOLLOWING LINE.  
  23. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  24. bind 0.0.0.0  

默认是“bind 127.0.0.1 ::1”,改为“bind 0.0.0.0”;

修改启动模式为后台启动

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. ################################# GENERAL #####################################  
  2.   
  3. # By default Redis does not run as a daemon. Use 'yes' if you need it.  
  4. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.  
  5. daemonize yes  

daemonize yes

 

修改数据文件存储位置

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. # The working directory.  
  2. #  
  3. # The DB will be written inside this directory, with the filename specified  
  4. # above using the 'dbfilename' configuration directive.  
  5. #  
  6. # The Append Only File will also be created inside this directory.  
  7. #  
  8. # Note that you must specify a directory here, not a file name.  
  9. dir /var/lib/redis  

注意:是指定一个目录,不带文件名;

 

配置init脚本

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. vi /etc/init.d/redis  
[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. # chkconfig:   2345 90 10  
  2.   
  3. # description:  Redis is a persistent key-value database  
  4.   
  5. ###########################  
  6. PATH=/usr/local/bin:/sbin:/usr/bin:/bin  
  7.      
  8. REDISPORT=6379  
  9. EXEC=/usr/local/bin/redis-server  
  10. REDIS_CLI=/usr/local/bin/redis-cli  
  11.      
  12. PIDFILE=/var/run/redis.pid  
  13. CONF="/etc/redis.conf"  
  14.      
  15. case "$1" in  
  16.     start)  
  17.         if [ -f $PIDFILE ]  
  18.         then  
  19.                 echo "$PIDFILE exists, process is already running or crashed"  
  20.         else  
  21.                 echo "Starting Redis server..."  
  22.                 $EXEC $CONF  
  23.         fi  
  24.         if [ "$?"="0" ]   
  25.         then  
  26.               echo "Redis is running..."  
  27.         fi  
  28.         ;;  
  29.     stop)  
  30.         if [ ! -f $PIDFILE ]  
  31.         then  
  32.                 echo "$PIDFILE does not exist, process is not running"  
  33.         else  
  34.                 PID=$(cat $PIDFILE)  
  35.                 echo "Stopping ..."  
  36.                 $REDIS_CLI -p $REDISPORT SHUTDOWN  
  37.                 while [ -x ${PIDFILE} ]  
  38.                do  
  39.                     echo "Waiting for Redis to shutdown ..."  
  40.                     sleep 1  
  41.                 done  
  42.                 echo "Redis stopped"  
  43.         fi  
  44.         ;;  
  45.    restart|force-reload)  
  46.         ${0} stop  
  47.         ${0} start  
  48.         ;;  
  49.   *)  
  50.     echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2  
  51.         exit 1  
  52. esac  
  53. ##############################  

注意开头的两句:

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. # chkconfig:   2345 90 10  
  2.   
  3. # description:  Redis is a persistent key-value database  

这虽然是注释,但要是没有它,就会报错:service redis does not support chkconfig

添加执行权限

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. chmod +x /etc/init.d/redis  

设定开机启动服务

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. sudo chkconfig redis on    

启动,停止redis

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. service redis start    
  2. service redis stop   

或者:

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. /etc/init.d/redis start    
  2. /etc/init.d/redis stop   

测试redis

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. # redis-cli  
  2. 127.0.0.1:6379> set key 123  
  3. OK  
  4. 127.0.0.1:6379> get key  
  5. "123"  
  6. 127.0.0.1:6379> exit  

你也可以使用Telnet来测试:

 

 

[plain] view plain copy

 

 在CODE上查看代码片派生到我的代码片

  1. telnet  192.168.1.100 6379  

连接之后执行相同的命令就行。

本文由 CentOS中文站 - 专注Linux技术 作者:centos 发表,其版权均为 CentOS中文站 - 专注Linux技术 所有,文章内容系作者个人观点,不代表 CentOS中文站 - 专注Linux技术 对观点赞同或支持。如需转载,请注明文章来源。

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注