Centos7安装并配置redis

一、下载redis-5.0.5安装包

# wget http://download.redis.io/releases/redis-5.0.5.tar.gz

二、解压压缩包至指定目录

# mkdir -p /home/mark/tools
# tar zxvf redis-5.0.5.tar.gz -C /home/mark/tools/

三、转到redis解压目录

# cd redis-5.0.5/

四、编译安装

# make MALLOC=libc
将/redis-5.0.5/src目录下的文件添加至/usr/local/bin目录
# cd src/ && make install

五、设置redis开机自启

修改redis.conf文件
    daemonize no 改为 daemonize yes
    # sed -i ‘s/daemonize no/daemonize yes/g‘ ../redis.conf
1、在/etc目录下新建redis目录
    # mkdir -p /etc/redis
2、将/redis-5.0.5/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf 
    # cp /home/mark/tools/redis-5.0.5/redis.conf /etc/redis/6379.conf
3、将redis的启动脚本复制一份放到/etc/init.d目录下
    # cp /home/mark/tools/redis-5.0.5/utils/redis_init_script /etc/init.d/redisd
4、设置开机自启
    # systemctl enable redisd
5、开启服务&查看服务状态
    # systemctl start redisd
    # systemctl status redisd.service
    ● redisd.service - LSB: Redis data structure server
       Loaded: loaded (/etc/rc.d/init.d/redisd; bad; vendor preset: disabled)
       Active: active (running) since 二 2020-06-09 17:50:19 CST; 7min ago
         Docs: man:systemd-sysv-generator(8)
       CGroup: /system.slice/redisd.service
               └─10153 /usr/local/bin/redis-server 127.0.0.1:6379

六、设置客户端登录密码

编辑redis.conf文件,搜索requirepass关键字,去掉注释后,把foobared替换为自己的密码即可;
# vim ../redis.conf
requirepass 123456

七、更改完客户端登录密码,Redis服务停止报错解决方案

1、修改redis服务脚本,加入如下所示的红色授权信息即可:
    vi /etc/init.d/redis

    $CLIEXEC -a "youpassword" -p $REDISPORT shutdown

2、或添加一个密码变量,直接引用如下:
    #!/bin/sh
    #
    # Simple Redis init.d script conceived to work on Linux systems
    # as it does use of the /proc filesystem.

    ### BEGIN INIT INFO
    # Provides:     redis_6379
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Redis data structure server
    # Description:          Redis data structure server. See https://redis.io
    ### END INIT INFO

    REDISPORT=6379
    EXEC=/usr/local/bin/redis-server
    CLIEXEC=/usr/local/bin/redis-cli
    RESDISPASSWORD=123456            ###定义密码变量

    PIDFILE=/var/run/redis_${REDISPORT}.pid
    CONF="/etc/redis/${REDISPORT}.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 -a $RESDISPASSWORD -p $REDISPORT shutdown
                                ###引用变量
                    while [ -x /proc/${PID} ]
                    do
                        echo "Waiting for Redis to shutdown ..."
                        sleep 1
                    done
                    echo "Redis stopped"
            fi
            ;;
        *)
            echo "Please use start or stop as first argument"
            ;;
重新加载服务配置文件
# systemctl daemon-reload

相关推荐