1.下载:

官网下载:Download | Redis

把安装包上传至服务器:

2.安装:

  1. 解压redis:

    [root@node202 ~]# cd /usr/local/soft/
    [root@node202 soft]# tar -zxvf redis-7.0.11.tar.gz 
    
  2. 安装:

    进入redis-7.0.11文件夹:

    [root@node202 soft]# cd redis-7.0.11/
    #安装必要类库
    [root@node202 redis-7.0.11]# yum -y install gcc automake autoconf libtool make
    [root@node202 redis-7.0.11]# make & make install
    
  3. 启动:

    [root@node202 redis-7.0.11]# cd src
    [root@node202 src]# ./redis-server /usr/local/soft/redis-7.0.11/redis.conf 
    
    打印启动日志:
    
    8789:C 11 May 2023 01:53:49.076 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    8789:C 11 May 2023 01:53:49.076 # Redis version=7.0.11, bits=64, commit=00000000, modified=0, 	pid=8789, just started
    8789:C 11 May 2023 01:53:49.076 # Configuration loaded
    8789:M 11 May 2023 01:53:49.077 * Increased maximum number of open files to 10032 (it was 	originally set to 1024).
    8789:M 11 May 2023 01:53:49.077 * monotonic clock: POSIX clock_gettime
                    _._                                                  
               _.-``__ ''-._                                             
          _.-``    `.  `_.  ''-._           Redis 7.0.11 (00000000/0) 64 bit
     .-`` .-```.  ```\/    _.,_ ''-._                                  
    (    '      ,       .-`  | `,    )     Running in standalone mode
     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
     |    `-._   `._    /     _.-'    |     PID: 8789
      `-._    `-._  `-./  _.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |           https://redis.io       
      `-._    `-._`-.__.-'_.-'    _.-'                                   
     |`-._`-._    `-.__.-'    _.-'_.-'|                                  
     |    `-._`-._        _.-'_.-'    |                                  
      `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
    
    8789:M 11 May 2023 01:53:49.078 # WARNING: The TCP backlog setting of 511 cannot be 	enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    8789:M 11 May 2023 01:53:49.078 # Server initialized
    8789:M 11 May 2023 01:53:49.078 # WARNING Memory overcommit must be enabled! Without it, a 	background save or replication may fail under low memory condition. Being disabled, it can can also 	cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To 	fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 	'sysctl vm.overcommit_memory=1' for this to take effect.
    8789:M 11 May 2023 01:53:49.078 * Ready to accept connections
    
  4. 使用:

    [root@node202 src]# ./redis-cli 
    127.0.0.1:6379> set foo bar
    OK
    127.0.0.1:6379> get foo
    "bar"
    127.0.0.1:6379> 
    

3.配置

  1. 创建一个用于存储 Redis 配置文件目录(/etc/redis)和数据目录(/var/redis):

    mkdir -p /etc/redis
    mkdir -p /var/redis
    
  2. 复制配置文件:

    cp /usr/local/soft/redis-7.0.11/redis.conf /etc/redis/
    
  3. 复制编辑启动脚本:

    cp /usr/local/soft/redis-7.0.11/utils/redis_init_script /etc/init.d/redis
    
    vim /etc/init.d/redis
    #!/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
    
    PIDFILE=/var/run/redis_${REDISPORT}.pid
    CONF="/etc/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
    
  4. 设置开机自启动:

    chmod +x /etc/init.d/redis  #给脚本设置权限
    
    chkconfig --add redis  #添加redis服务
    
    chkconfig redis on  #设置redis服务开机启动
    

完结。

解决服务器Redis无法连接问题

找到你的redis配置文件,进行以下步骤修改。(本人的在/etc/redis.conf,如果找不到,直接创建一个,然后度娘一个默认的redis配置文件粘贴上去即可,启动时使用命令redis-cli +文件路径,下文会讲)

修改bind,默认为bind 127.0.0.1,将其注释(前面加个#),如果没有找到bind 127.0.0.1或已经注释,跳过此步。

# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
# bind 127.0.0.1

关闭保护模式,默认为protected-mode yes,将yes修改为no,如果没有找到protected-mode yes,可以随意另起一行添加protected-mode no;或已经修改为protected-mode no,跳过此步。

# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

如果有需求,设置密码(没有需求则跳过),添加一行requirepass 123456,作用是设置连接密码为123456,如有需求可以修改密码

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
requirepass 123456

05-11 23:12