redis第二篇:Centos下配置Redis开机启动

来源:互联网 发布:国内erp软件排行 编辑:程序博客网 时间:2024/05/30 05:42
1、下载安装
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz  
  2. tar -zxf redis-2.2.13.tar.gz  
  3. cd redis-2.2.13  
  4. make  
  5. sudo make install    
  6. cp redis.conf /etc  
install的时候,redis的命令会被拷贝到/usr/local/bin下面

2,建立用户与日志目录

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

[html] 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 #db文件放在这里,要修改redis.conf  
  5. sudo chown redis.redis /var/log/redis  

3,配置init脚本

其实github上很多老外写好的启动脚本,不过大部分都是ubuntu的,对于Centos,也有一份

https://gist.github.com/1335694
经过修改,如下:

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

把上述代码存为redis,放到/etc/init.d/下面

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. chmod +x /etc/init.d/redis  

其实做成服务启动,也是调用redis-server,如果想让它在后台作为daemon运行,那么

需要修改redis.conf,将 daemonize no 改为 daemonize yes

4,设定开机启动服务
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. sudo chkconfig redis on  
5,启动,停止redis
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. service redis start   #或者 /etc/init.d/redis start  
  2. service redis stop   #或者 /etc/init.d/redis stop  
6,测试redis
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. redis-cli   
  2. redis 127.0.0.1:6379> set foo 123  
  3. OK  
  4. redis 127.0.0.1:6379> get foo  
  5. "123"  
  6. redis 127.0.0.1:6379> exit  
0 0
原创粉丝点击