Redis设置开机自启和设置密码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42969074/article/details/84927944

Redis安装参考博客(很详细):https://blog.csdn.net/gyshun/article/details/79297107

然后设置Redis开机自启,之前编辑redis.conf,将daemonize改为yes,但是重启后发现没用,后来查了很多资料才解决:

编写开机自启动脚本:

vi /etc/init.d/redis

将下面的代码复制进去, 其中要注意修改自己的Redis路径。

#!/bin/sh
# chkconfig: 2345 10 90  
# description: Start and Stop redis   

REDISPORT=6379
# 自己的redis-server路径(需要自己更改)
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
# 自己的redis.conf 路径(需要自己更改)
CONF="/usr/local/redis/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
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    *)
        echo "Please use start or stop or restart as first argument"
        ;;
esac

 保存然后退出。

然后给其设置权限:

chmod 777 /etc/init.d/redis

启动redis

service redis start

打开redis命令:service redis start

关闭redis命令:service redis stop

设为开机启动:chkconfig redis on

设为开机关闭:chkconfig redis off

然后重启:

reboot

查看Redis进程:

ps -ef | grep redis

成功启动,下次开机也会正常启动。 

顺便给Redis设置一下密码:

修改redis.conf里面的配置:

找到#requirpass foobared ,去掉注释,把foobared改成想要的密码即可,例如123456.

修改完成后重启Redis即可, 

关闭redis命令:service redis stop

打开redis命令:service redis start

下面测试:第一次登陆需要密码

输入密码:

auth 123456

然后成功:

猜你喜欢

转载自blog.csdn.net/qq_42969074/article/details/84927944