[CentOS7]redis设置开机启动,设置密码

简介

上篇文章介绍了如何安装redis,但每次重启服务器之后redis不会自启,这里将介绍如何进行自启设置,以及如何设置redis的密码,进行密码验证登陆。

上篇文章: Centos7安装Redis

步骤

1、设置redis.conf中daemonize为yes,确保后台进行开启。

2、编写开机自启动脚本

vi /etc/init.d/redis

3、将下面脚本添加redis文件中

 

 
#!/bin/bash
# chkconfig: 2345 10 90  
# description: Start and Stop redis    
REDISPORT=6379  
EXEC=/usr/local/bin/redis-server   
REDIS_CLI=/usr/local/bin/redis-cli 
PIDFILE=/var/run/redis_6379.pid  
CONF="/etc/redis/6379.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 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
 

4、设置权限

chmod 755 /etc/init.d/redis

5、启动测试

/etc/init.d/redis start

6、设置开机自启动

chkconfig redis on

7、重启服务器测试

reboot

8、设置密码

vi /etc/redis/6379.conf

修改为:

requirepass root  #注意行前空格去掉

9、重启redis

b.开启Redis服务操作通过/etc/init.d/redis_6379 start命令,也可通过(service redis_6379 start)
c.关闭Redis服务操作通过/etc/init.d/redis_6379 stop命令,也可通过(service redis_6379 stop)
posted @ 2018-11-16 14:10  wolfy  阅读(11030)  评论(2编辑  收藏  举报