redis 在linux中安装

一、下载地址

https://download.redis.io/releases/redis-6.2.6.tar.gz

二、安装

wget https://download.redis.io/releases/redis-6.2.6.tar.gz

cd redis-6.2.6.tar.gz

make

make PREFIX=/usr/local/redis install

mkdir /usr/local/redis/etc

cp redis.conf /usr/local/redis/etc/

三、启动

启动:./redis-server /usr/local/redis/etc/redis.conf

进入:./redis-cli -h 127.0.0.1 -p 6379

查看密码:config get requirepass

设置密码:config set requirepass  yourpassword

密码验证:auth yourpassword

四、开机自启动

进入redis.conf文件,把daemonize no改成daemonize yes(允许后台启动)

编写脚本:vim /etc/init.d/redis

#! /bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:    2345 80 90
# description:  Redis is a persistent key-value database


### BEGIN INIT INFO
# Provides:          redis
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Should-Start:        $local_fs
# Should-Stop:        $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description:    redis-server daemon
# Description:        redis-server daemon
### END INIT INFO



REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
#这个根据实际情况填写
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/etc/redis.conf"
#AUTH=""  有设置密码填写AUTH
case "$1" in
    start)
        if [ -f "$PIDFILE" ]; then
            echo "$PIDFILE exists, process is already running or crashed"
        else
            echo -n "Starting Redis server..."
            $EXEC $CONF
            if [ "$?"="0" ]; then
                echo " done"
            else
                echo " failed"
            fi
        fi
        ;;
    stop)
        if [ ! -f "$PIDFILE" ]; then
            echo "$PIDFILE does not exist, process is not running"
        else
            PID=$(cat $PIDFILE)
            echo "Stopping  Redis server..."
            $REDIS_CLI -p $REDISPORT shutdown
            if [ "$?"="0" ]; then
                echo " done"
            else
                echo " failed"
            fi
        fi
        ;;
    restart)
        ${0} stop
        ${0} start
        ;;
    kill)
        echo "force kill redis server..."
        killall redis-server
        if [ "$?"="0" ]; then
            echo " done"
        else
            echo " failed"
        fi
        ;;
    status)
        if [ -f "$PIDFILE" ]; then
            echo "Redis server is running."
        else
            echo "Redis server is stopped."
        fi
        ;;
  *)
    echo "Usage: /etc/init.d/redis {start|stop|restart|status|kill}" >&2
        exit 1
esac

赋予文件权限

chmod +x /etc/init.d/redis

将文件加入系统服务列表

chkconfig --add redis

开启自启动

chkconfig redis on


版权声明:本文为Jifei5201314原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。