您的位置:首页 > 数据库 > Redis

Centos下配置Redis开机启动

2017-02-04 10:14 676 查看
1、设置redis.conf中daemonize为yes,确保守护进程开启。

2、编写开机自启动脚本

vi /etc/init.d/redis


脚本内容如下:

[cpp] view
plain copy

 





#!/bin/bash  

#chkconfig: 2345 80 90  

# Simple Redis init.d script conceived to work on Linux systems  

# as it does use of the /proc filesystem.    

# description: Start and Stop redis

PATH=/usr/local/bin:/sbin:/usr/bin:/bin     

# redis端口号  

REDISPORT=6379    

# redis-server所在目录的绝对路径  

EXEC=/usr/apk/redis-2.8.17/redis-server     

# redis-cli所在目录的绝对路径  

REDIS_CLI=/usr/apk/redis-2.8.17/redis-cli     

   

PIDFILE=/var/run/redis.pid     

# redis.conf所在目录的绝对路径  

CONF="/usr/apk/redis-2.8.17/redis.conf"    

AUTH="nginx"    

  

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 exists, process is not running."    

                else    

                        PID=$(cat $PIDFILE)     

                        echo "Stopping..."    

                       $REDIS_CLI -p $REDISPORT  SHUTDOWN      

                        sleep 2    

                       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  

写完后保存退出VI

3、设置权限

chmod 755 /etc/init.d/redis


4,设定开机启动服务

[html] view
plain copy

 





sudo chkconfig redis on  

5,启动,停止redis

[html] view
plain copy

 





service redis start   #或者 /etc/init.d/redis start  

service redis stop   #或者 /etc/init.d/redis stop  

启动成功会提示如下信息:

Starting Redis server...
Redis is running...


使用redis-cli测试:

[root@rk ~]# /usr/redisbin/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> exit


6、关机重启测试

reboot


然后在用redis-cli测试即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: