当前位置:   article > 正文

Redis部署和基础使用、Redis集群和高可用_redis logrotate

redis logrotate

一、总结 Redis多种安装方法和内核参数优化

1、rocky8源码安装redis

[root@Rocky8 ~]# yum -y install redis
[root@Rocky8 ~]# rpm -ql redis
/etc/logrotate.d/redis
/etc/redis-sentinel.conf
/etc/redis.conf
/etc/systemd/system/redis-sentinel.service.d
/etc/systemd/system/redis-sentinel.service.d/limit.conf
/etc/systemd/system/redis.service.d
/etc/systemd/system/redis.service.d/limit.conf
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof
/usr/bin/redis-check-rdb
/usr/bin/redis-cli
/usr/bin/redis-sentinel
/usr/bin/redis-server
/usr/lib/.build-id
/usr/lib/.build-id/19
/usr/lib/.build-id/19/e1e3e9c658ca7411675798da3adfb25b456626
/usr/lib/.build-id/46
/usr/lib/.build-id/46/bd1f92df23c67a27dfd80d946a0266614c818a
/usr/lib/.build-id/ac
/usr/lib/.build-id/ac/73b6f68fff8c23ad408fc848ff7ec5a954ef53
/usr/lib/systemd/system/redis-sentinel.service
/usr/lib/systemd/system/redis.service
/usr/lib64/redis
/usr/lib64/redis/modules
/usr/libexec/redis-shutdown
/usr/share/licenses/redis
/usr/share/licenses/redis/COPYING
/usr/share/licenses/redis/COPYING-hiredis
/usr/share/licenses/redis/COPYING-jemalloc
/usr/share/licenses/redis/COPYRIGHT-lua
/usr/share/man/man1/redis-benchmark.1.gz
/usr/share/man/man1/redis-check-aof.1.gz
/usr/share/man/man1/redis-check-rdb.1.gz
/usr/share/man/man1/redis-cli.1.gz
/usr/share/man/man1/redis-sentinel.1.gz
/usr/share/man/man1/redis-server.1.gz
/usr/share/man/man5/redis-sentinel.conf.5.gz
/usr/share/man/man5/redis.conf.5.gz
/var/lib/redis
/var/log/redis
/var/run/redis
[root@Rocky8 ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

[root@Rocky8 ~]# systemctl status redis
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since Tue 2023-08-29 13:22:13 CST; 4s ago
 Main PID: 31455 (redis-server)
    Tasks: 4 (limit: 23016)
   Memory: 6.6M
   CGroup: /system.slice/redis.service
           └─31455 /usr/bin/redis-server 127.0.0.1:6379

Aug 29 13:22:13 Rocky8.8 systemd[1]: Starting Redis persistent key-value database...
Aug 29 13:22:13 Rocky8.8 systemd[1]: Started Redis persistent key-value database.

#默认端口6379
[root@Rocky8 ~]# ss -ntl
State               Recv-Q              Send-Q                           Local Address:Port                            Peer Address:Port              Process              
LISTEN              0                   511                                  127.0.0.1:6379                                 0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:111                                  0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:22                                   0.0.0.0:*                                      
LISTEN              0                   128                                       [::]:111                                     [::]:*                                      
LISTEN              0                   128                                       [::]:22                                      [::]:*   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

2、Redis编译安装

#安装相关依赖
[root@Rocky8 ~/redis-6.2.13]# yum -y install gcc make jemalloc-devel

#如果支持systemd需要安装下面包
[root@centos8~]#yum -y install gcc jemalloc-devel systemd-devel
[root@ubuntu2004 ~]#apt -y install make gcc libjemalloc-dev libsystemd-dev

#下载编译redis包
[root@Rocky8 ~]# wget https://download.redis.io/releases/redis-6.2.13.tar.gz
[root@Rocky8 ~]# tar xf redis-6.2.13.tar.gz 
[root@Rocky8 ~]# ls
anaconda-ks.cfg  redis-6.2.13  redis-6.2.13.tar.gz
[root@Rocky8 ~]# cd redis-6.2.13/
[root@Rocky8 ~/redis-6.2.13]# ls
00-RELEASENOTES  CONDUCT       COPYING  INSTALL   MANIFESTO  redis.conf  runtest-cluster    runtest-sentinel  src    TLS.md
BUGS             CONTRIBUTING  deps     Makefile  README.md  runtest     runtest-moduleapi  sentinel.conf     tests  utils
[root@Rocky8 ~/redis-6.2.13]# make -j 2 USE_SYSTEMD=yes PREFIX=/apps/redis install

[root@Rocky8 ~/redis-6.2.13]# echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh

#前台执行
[root@Rocky8 /]# redis-server 
38014:C 29 Aug 2023 13:58:50.726 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
38014:C 29 Aug 2023 13:58:50.726 # Redis version=6.2.13, bits=64, commit=00000000, modified=0, pid=38014, just started
38014:C 29 Aug 2023 13:58:50.726 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
38014:M 29 Aug 2023 13:58:50.726 * Increased maximum number of open files to 10032 (it was originally set to 1024).
38014:M 29 Aug 2023 13:58:50.726 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.13 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 38014
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

38014:M 29 Aug 2023 13:58:50.726 # Server initialized
38014:M 29 Aug 2023 13:58:50.726 # 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.
38014:M 29 Aug 2023 13:58:50.727 * Ready to accept connections


[root@Rocky8 ~]# mkdir /apps/redis/{etc,data,log,run}
[root@Rocky8 ~]# cd redis-6.2.13/
[root@Rocky8 ~/redis-6.2.13]# cp redis.conf /apps/redis/etc/
#创建redis账号
[root@Rocky8 ~]#useradd -r -s /sbin/nologin redis
[root@Rocky8 ~]# chown redis.redis /apps/redis/ -R
[root@Rocky8 ~]# ls /apps/redis/ -l
total 0
drwxr-xr-x 2 redis redis 134 Aug 29 13:53 bin
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 data
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 etc
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 log
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 run

/*查看redis.conf文件:grep -Ev "^#|^$" etc/redis.conf 
*/

#创建相关配置文件目录
[root@Rocky8 ~]# vim /etc/sysctl.conf
vm.overcommit_memory=1   
[root@Rocky8 ~]# sysctl -p
vm.overcommit_memory = 1
#此项默认为128,redis官方建议511
[root@Rocky8 /apps/redis]# cat /proc/sys/net/core/somaxconn 
2048

#编写redis.service文件,实现后台执行
[root@Rocky8 /apps/redis]# vim /lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify #如果支持systemd可以启用此行
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000   #指定此值才支持更大的maxclients值

[Install]
WantedBy=multi-user.target
#启动redis
[root@Rocky8 /apps/redis]# systemctl daemon-reload
[root@Rocky8 /apps/redis]# systemctl enable --now redis.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@Rocky8 /apps/redis]# systemctl status redis.service 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100

3、以编译安装为例实现 redis 多实例

#生成的文件列表

[root@Rocky8 ~]# ls /apps/redis/ -l
total 4
drwxr-xr-x 2 redis redis 134 Aug 29 13:53 bin
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 data
-rw-r--r-- 1 root  root   93 Aug 29 21:48 dump.rdb
drwxr-xr-x 2 redis redis  72 Aug 29 22:46 etc
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 log
drwxr-xr-x 2 redis redis   6 Aug 29 21:35 run

[root@Rocky8 /apps/redis]# cat /apps/redis/etc/redis6379.conf |grep -Ev '^#|^$'
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

[root@Rocky8 ~]# sed 's/6379/6380/' /apps/redis/etc/redis6379.conf > /apps/redis/etc/redis6380.conf
[root@Rocky8 ~]# sed 's/6379/6381/' /apps/redis/etc/redis6379.conf > /apps/redis/etc/redis6381.conf

#生成service文件
[root@Rocky8 ~]# cat /lib/systemd/system/redis6379.service 
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis6379.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=1000000

[Install]
WantedBy=multi-user.target

[root@Rocky8 ~]# sed 's/6379/6380/' /lib/systemd/system/redis6379.service >  /lib/systemd/system/redis6380.service

[root@Rocky8 ~]# sed 's/6379/6381/' /lib/systemd/system/redis6379.service >  /lib/systemd/system/redis6381.service

[root@Rocky8 ~]# systemctl daemon-reload
[root@Rocky8 ~]# systemctl enable --now redis6379 redis6380 redis6381

[root@Rocky8 ~]# ss -ntl
State               Recv-Q              Send-Q                           Local Address:Port                            Peer Address:Port              Process              
LISTEN              0                   511                                    0.0.0.0:6379                                 0.0.0.0:*                                      
LISTEN              0                   511                                    0.0.0.0:6380                                 0.0.0.0:*                                      
LISTEN              0                   511                                    0.0.0.0:6381                                 0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:111                                  0.0.0.0:*                                      
LISTEN              0                   128                                    0.0.0.0:22                                   0.0.0.0:*                                      
LISTEN              0                   511                                      [::1]:6379                                    [::]:*                                      
LISTEN              0                   511                                      [::1]:6380                                    [::]:*                                      
LISTEN              0                   511                                      [::1]:6381                                    [::]:*                                      
LISTEN              0                   128                                       [::]:111                                     [::]:*                                      
LISTEN              0                   128                                       [::]:22                                      [::]:*            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

4、消除启动时的三个Warning提示信息(可选)

前面直接启动Redis时有三个Waring信息,可以用下面方法消除告警,但非强制消除

4.1、 Tcp backlog

描述:

WARNING: The TCP backlog setting of 511 cannot be enforced because 
/proc/sys/net/core/somaxconn is set to the lower value of 128.
  • 1
  • 2

问题阐述:

Tcp backlog 是指TCP的第三次握手服务器端收到客户端 ack确认号之后到服务器用Accept函数处理请求
前的队列长度,即全连接队列
  • 1
  • 2

修改配置文件

#vim /etc/sysctl.conf
net.core.somaxconn = 1024
#sysctl -p
  • 1
  • 2
  • 3

4.2、 overcommit_memory

描述:

WARNING overcommit_memory is set to 0! Background save may fail under low memory 
condition. 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
  • 1
  • 2
  • 3
  • 4

内核参数说明:

内核参数overcommit_memory 实现内存分配策略,可选值有三个:012
0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则内存
申请失败,并把错误返回给应用进程
1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
2 表示内核允许分配超过所有物理内存和交换空间总和的内存
  • 1
  • 2
  • 3
  • 4
  • 5

修改配置文件:

#vim /etc/sysctl.conf
vm.overcommit_memory = 1
#sysctl -p
  • 1
  • 2
  • 3

4.3、transparent hugepage

问题描述及说明:

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. 
This will create latency and memory usage issues with Redis. To fix this issue 
run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as 
root, and add it to your /etc/rc.local in order to retain the setting after a 
reboot. Redis must be restarted after THP is disabled.
警告:您在内核中启用了透明大页面(THP,不同于一般4k内存页,而为2M)支持。 这将在Redis中造成延迟
和内存使用问题。 要解决此问题,请以root 用户身份运行命令“echo never> 
/sys/kernel/mm/transparent_hugepage/enabled”,并将其添加到您的/etc/rc.local中,以便在
重启后保留设置。禁用THP后,必须重新启动Redis。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注意:ubuntu20.04, Rocky8/CentOS8 默认为 never,所以此值无需优化

修改参数值

[root@centos8 ~]#echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' 
>> /etc/rc.d/rc.local 
[root@centos8 ~]#cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@centos8 ~]#chmod +x /etc/rc.d/rc.local
#ubuntu开机配置
[root@ubuntu2004 ~]#cat /etc/rc.local 
#!/bin/bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@ubuntu2004 ~]#chmod +x /etc/rc.local
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

4.4、验证是否消除 Warning

重新启动redis 服务不再有前面的三个Waring信息

[root@Rocky8 ~]# redis-server /apps/redis/etc/redis6379.conf 
27646:C 16 Feb 2020 21:26:52.690 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
27646:C 16 Feb 2020 21:26:52.690 # Redis version=5.0.7, bits=64, 
commit=00000000, modified=0, pid=27646, just started
27646:C 16 Feb 2020 21:26:52.690 # Configuration loaded
27646:M 16 Feb 2020 21:26:52.690 * Increased maximum number of open files to 
10032 (it was originally set to 1024).
               _._                                                  
           _.-``__ ''-._                                             
     _.-``    `. `_.  ''-._           Redis 5.0.7 (00000000/0) 64 bit
 .-`` .-```. ```\/   _.,_ ''-._                                   
 (    '     ,       .-` | `,   )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._   /     _.-'   |     PID: 27646
  `-._   `-._  `-./ _.-'   _.-'                                   
 |`-._`-._    `-.__.-'   _.-'_.-'|                                  
 |    `-._`-._       _.-'_.-'   |           http://redis.io        
  `-._   `-._`-.__.-'_.-'   _.-'                                   
 |`-._`-._    `-.__.-'   _.-'_.-'|                                  
 |    `-._`-._       _.-'_.-'   |                                  
  `-._   `-._`-.__.-'_.-'   _.-'                                   
      `-._   `-.__.-'   _.-'                                       
          `-._       _.-'                                           
              `-.__.-'          
27646:M 16 Feb 2020 21:26:52.691 # Server initialized
27646:M 16 Feb 2020 21:26:52.692 * DB loaded from disk: 0.000 seconds
27646:M 16 Feb 2020 21:26:52.692 * Ready to accept connections                                    

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

二、总结 Redis 常见指令和数据类型

1、Redis常用命令

1.1、显示当前节点redis运行状态信息

[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379[1]> info
# Server
redis_version:6.2.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6a4a30c076e6cee6
redis_mode:standalone
os:Linux 4.18.0-477.10.1.el8_8.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:8.5.0
process_id:43311
process_supervised:systemd
run_id:bcc39dea395b4cc8707198ad1fb64d62bf3978e0
tcp_port:6379
server_time_usec:1693344648708184
uptime_in_seconds:149
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:15623048
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis6379.conf
io_threads_active:0

# Clients
connected_clients:1
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:16
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

# Memory
used_memory:861477728
used_memory_human:821.57M
used_memory_rss:890589184
used_memory_rss_human:849.33M
used_memory_peak:861535688
used_memory_peak_human:821.62M
used_memory_peak_perc:99.99%
used_memory_overhead:539050392
used_memory_startup:811912
used_memory_dataset:322427336
used_memory_dataset_perc:37.46%
allocator_allocated:861527376
allocator_active:861786112
allocator_resident:883892224
total_system_memory:3811012608
total_system_memory_human:3.55G
used_memory_lua:30720
used_memory_lua_human:30.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.00
allocator_frag_bytes:258736
allocator_rss_ratio:1.03
allocator_rss_bytes:22106112
rss_overhead_ratio:1.01
rss_overhead_bytes:6696960
mem_fragmentation_ratio:1.03
mem_fragmentation_bytes:29154224
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:20496
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
lazyfreed_objects:0

# Persistence
loading:0
current_cow_size:0
current_cow_size_age:0
current_fork_perc:0.00
current_save_keys_processed:0
current_save_keys_total:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1693344499
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
module_fork_in_progress:0
module_fork_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:3
instantaneous_ops_per_sec:0
total_net_input_bytes:103
total_net_output_bytes:20360
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:2
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
total_forks:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:2
dump_payload_sanitizations:0
total_reads_processed:6
total_writes_processed:5
io_threaded_reads_processed:0
io_threaded_writes_processed:0

# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:abd1c65d4601b2d9b1b416126b240063dcf4a542
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.553983
used_cpu_user:5.039465
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:0.552826
used_cpu_user_main_thread:5.038687

# Modules

# Errorstats
errorstat_ERR:count=2

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=10100002,expires=0,avg_ttl=0
db1:keys=2,expires=0,avg_ttl=0

#显示特定部分
127.0.0.1:6379[1]> info server
# Server
redis_version:6.2.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6a4a30c076e6cee6
redis_mode:standalone
os:Linux 4.18.0-477.10.1.el8_8.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:8.5.0
process_id:43311
process_supervised:systemd
run_id:bcc39dea395b4cc8707198ad1fb64d62bf3978e0
tcp_port:6379
server_time_usec:1693344695682110
uptime_in_seconds:196
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:15623095
executable:/apps/redis/bin/redis-server
config_file:/apps/redis/etc/redis6379.conf
io_threads_active:0
127.0.0.1:6379[1]> info cluster
# Cluster
cluster_enabled:0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209

1.2、SELECT

切换数据库,相当于在MySQL的 USE DBNAME 指令

[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379[1]> SELECT 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 2
127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> dbsize
(integer) 10100002
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.3、KEYS

查看当前库下的所有key,此命令慎用!

127.0.0.1:6379[15]> SELECT 0
OK
127.0.0.1:6379> KEYS *
1) "9527"
2) "9526"
3) "course"
4) "list1"
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> KEYS *
(empty list or set)
127.0.0.1:6379[1]> 
redis>MSET one 1 two 2 three 3 four 4  # 一次设置 4 个 key
OK
redis> KEYS *o*
1) "four"
2) "two"
3) "one"
redis> KEYS t??
1) "two"
redis> KEYS t[w]*
1) "two"
redis> KEYS *  # 匹配数据库内所有 key
1) "four"
2) "three"
3) "two"
4) "one"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1.4、BGSAVE

手动在后台执行RDB持久化操作

#交互式执行
127.0.0.1:6379[1]> BGSAVE
Background saving started
#非交互式执行
[root@centos8 ~]#ll /var/lib/redis/
total 4
-rw-r--r-- 1 redis redis 326 Feb 18 22:45 dump.rdb


[root@Rocky8 ~]# redis-cli -h 127.0.0.1 -a '123456' BGSAVE
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Background saving started
[root@Rocky8 ~]# ll /apps/redis/data/
total 185420
-rw-r--r-- 1 redis redis        93 Aug 30 04:25 appendonly.aof
-rw-r--r-- 1 redis redis 189855719 Aug 30 05:37 dump6379.rdb
-rw-r--r-- 1 root  root         93 Aug 29 23:16 dump6380.rdb
-rw-r--r-- 1 root  root         93 Aug 29 23:16 dump6381.rdb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1.5、DBSIZE

返回当前库下的所有key 数量

[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379[1]> SELECT 1
OK
127.0.0.1:6379[1]> dbsize
(integer) 2
127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> dbsize
(integer) 10100002
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.6、FLUSHDB

强制清空当前库中的所有key,此命令慎用!

127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> DBSIZE
(integer) 4
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> DBSIZE
(integer) 0
127.0.0.1:6379>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.7、FLUSHALL

强制清空当前Redis服务器所有数据库中的所有key,即删除所有数据,此命令慎用!

127.0.0.1:6379> FLUSHALL
OK
#生产建议修改配置使用rename-command禁用此命令
vim /etc/redis.conf
rename-command FLUSHALL ""
#rename-command 可能会在后续版本淘汰
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.8、SHUTDOWN

可用版本: >= 1.0.0
时间复杂度: O(N),其中 N 为关机时需要保存的数据库键数量。
SHUTDOWN 命令执行以下操作:
关闭Redis服务,停止所有客户端连接
如果有至少一个保存点在等待,执行 SAVE 命令
如果 AOF 选项被打开,更新 AOF 文件
关闭 redis 服务器(server)
如果持久化被打开的话, SHUTDOWN 命令会保证服务器正常关闭而不丢失任何数据。
另一方面,假如只是单纯地执行 SAVE 命令,然后再执行 QUIT 命令,则没有这一保证 —— 因为在执行
SAVE 之后、执行 QUIT 之前的这段时间中间,其他客户端可能正在和服务器进行通讯,这时如果执行 QUIT 
就会造成数据丢失
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、Redis数据类型

2.1、字符串string

字符串是一种最基本的Redis值类型。Redis字符串是二进制安全的,这意味着一个Redis字符串能包含任
意类型的数据,例如: 一张JPEG格式的图片或者一个序列化的Ruby对象。一个字符串类型的值最多能
存储512M字节的内容。Redis 中所有 key 都是字符串类型的。
  • 1
  • 2
  • 3
2.1.1 创建一个key
# set 指令可以创建一个key 并赋值, 使用格式
SET key value [EX seconds] [PX milliseconds] [NX|XX]
时间复杂度: O(1)
将字符串值 value 关联到 key 。
如果 key 已经持有其他值, SET 就覆写旧值, 无视类型。
当 SET 命令对一个带有生存时间(TTL)的键进行设置之后, 该键原有的 TTL 将被清除。
从 Redis 2.6.12 版本开始, SET 命令的行为可以通过一系列参数来修改:
EX seconds : 将键的过期时间设置为 seconds 秒。 执行 SET key value EX seconds 的效果等
同于执行 SETEX key seconds value 。
PX milliseconds : 将键的过期时间设置为 milliseconds 毫秒。 执行 SET key value PX 
milliseconds 的效果等同于执行 PSETEX key milliseconds value 。
NX : 只在键不存在时, 才对键进行设置操作。 执行 SET key value NX 的效果等同于执行 SETNX 
key value 。
XX : 只在键已经存在时, 才对键进行设置操作。


#不论key是否存在.都设置,set 指令可以创建一个key 并赋值, 使用格式
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> TYPE key1  #判断类型
string
127.0.0.1:6379> SET title ceo ex 3 #设置自动过期时间3s
OK
127.0.0.1:6379> set NAME wang
OK
127.0.0.1:6379> get NAME
"wang"
#Key大小写敏感
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name mage
OK
127.0.0.1:6379> get name
"mage"
127.0.0.1:6379> get NAME
"wang"
#key不存在,才设置,相当于add 
127.0.0.1:6379> get title
"ceo"
127.0.0.1:6379> setnx title coo  #set key value nx
(integer) 0
127.0.0.1:6379> get title
"ceo"
#key存在,才设置,相当于update
127.0.0.1:6379> get title
"ceo"
127.0.0.1:6379> set title coo xx
OK
127.0.0.1:6379> get title
"coo"
127.0.0.1:6379> get age
(nil)
127.0.0.1:6379> set age 20 xx
(nil)
127.0.0.1:6379> get age
(nil)


[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> set hongbao 100 ex 10
OK
127.0.0.1:6379> get hongbao
"100"
127.0.0.1:6379> get hongbao
"100"
127.0.0.1:6379> get hongbao
(nil)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
2.1.2、查看一个key值
127.0.0.1:6379> get key1
"value1"
#get可能查看一个key的值
127.0.0.1:6379> get name age
(error) ERR wrong number of arguments for 'get' command
  • 1
  • 2
  • 3
  • 4
  • 5
2.1.3、删除key
127.0.0.1:6379> DEL key1
(integer) 1
127.0.0.1:6379> DEL key1 key2
(integer) 2
  • 1
  • 2
  • 3
  • 4
2.1.4、批量设置多个key
127.0.0.1:6379> MSET key1 value1 key2 value2 
Ok
  • 1
  • 2
2.1.5、批量获取多个key
127.0.0.1:6379> MGET key1 key2
1) "value1"
2) "value2"
127.0.0.1:6379> KEYS n*
1) "n1"
2) "name"
127.0.0.1:6379> KEYS *
1) "k2"
2) "k1"
3) "key1"
4) "key2"
5) "n1"
6) "name"
7) "k3"
8) "title"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
2.1.6、追加key的数据
127.0.0.1:6379> APPEND key1 " append new value"
(integer) 12              #添加数据后,key1总共9个字节
127.0.0.1:6379> get key1
"value1 append new value"
  • 1
  • 2
  • 3
  • 4
2.1.7、设置新值并返回旧值
127.0.0.1:6379> set name wang
OK
#set key newvalue并返回旧的value
127.0.0.1:6379> getset name magedu
"wang"
127.0.0.1:6379> get name
"magedu"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.1.8 、返回字符串key对应值的字节数
127.0.0.1:6379> SET name wang
OK
127.0.0.1:6379> STRLEN name
(integer) 4
127.0.0.1:6379> APPEND name " xiaochun"
(integer) 13
127.0.0.1:6379> GET name
"wang xiaochun"
127.0.0.1:6379> STRLEN name  #返回字节数
(integer) 13
127.0.0.1:6379> set name 马哥教育
OK
127.0.0.1:6379> get name
"\xe9\xa9\xac\xe5\x93\xa5\xe6\x95\x99\xe8\x82\xb2"
127.0.0.1:6379> strlen name
(integer) 12
127.0.0.1:6379>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
2.1.9、判断 key 是否存在
127.0.0.1:6379> SET name wang ex 10
OK
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> EXISTS NAME #key的大小写敏感
(integer) 0
127.0.0.1:6379> EXISTS name age #返回值为1,表示存在2个key,0表示不存在
(integer) 2
127.0.0.1:6379> EXISTS name  #过几秒再看
(integer) 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
2.1.10、获取 key 的过期时长
ttl key #查看key的剩余生存时间,如果key过期后,会自动删除
-1 #返回值表示永不过期,默认创建的key是永不过期,重新对key赋值,也会从有剩余生命周期变成永不过
期
-2 #返回值表示没有此key
num #key的剩余有效期
127.0.0.1:6379> TTL key1
(integer) -1
127.0.0.1:6379> SET name wang EX 100
OK
127.0.0.1:6379> TTL name
(integer) 96
127.0.0.1:6379> TTL name
(integer) 93
127.0.0.1:6379> SET name mage #重新设置,默认永不过期
OK
127.0.0.1:6379> TTL name
(integer) -1
127.0.0.1:6379> SET name wang EX 200
OK
127.0.0.1:6379> TTL name
(integer) 198
127.0.0.1:6379> GET name
"wang"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
2.1.11、重置key的过期时长
127.0.0.1:6379> TTL name
(integer) 148
127.0.0.1:6379> EXPIRE name 1000
(integer) 1
127.0.0.1:6379> TTL name
(integer) 999
127.0.0.1:6379>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.1.12、取消key的期限
#即永不过期
127.0.0.1:6379> TTL name
(integer) 999
127.0.0.1:6379> PERSIST name
(integer) 1
127.0.0.1:6379> TTL name
(integer) -1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.1.13、数字递增
#利用INCR命令簇(INCR, DECR, INCRBY,DECRBY)来把字符串当作原子计数器使用。
127.0.0.1:6379> set num 10 #设置初始值
OK
127.0.0.1:6379> INCR num
(integer) 11
127.0.0.1:6379> get num
"11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.1.14、数字递减
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> DECR num
(integer) 9
127.0.0.1:6379> get num
"9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.1.15、数字增加
#将key对应的数字加decrement(可以是负数)。如果key不存在,操作之前,key就会被置为0。如果key的

value类型错误或者是个不能表示成数字的字符串,就返回错误。这个操作最多支持64位有符号的正型

数字。
redis> SET mykey 10
OK
redis> INCRBY mykey 5
(integer) 15
127.0.0.1:6379> get mykey
"15"
127.0.0.1:6379> INCRBY mykey -10
(integer) 5
127.0.0.1:6379> get mykey
"5"
127.0.0.1:6379> INCRBY nokey  5
(integer) 5
127.0.0.1:6379> get nokey
"5"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2.1.16、数字减少

decrby 可以减小数值(也可以增加)

127.0.0.1:6379> SET mykey 10
OK
127.0.0.1:6379> DECRBY mykey 8
(integer) 2
127.0.0.1:6379> get mykey
"2"
127.0.0.1:6379> DECRBY mykey -20
(integer) 22
127.0.0.1:6379> get mykey
"22"
127.0.0.1:6379> DECRBY nokey 3
(integer) -3
127.0.0.1:6379> get nokey
"-3"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2.2、列表list

1、Redis列表就是简单的字符串数组,按照插入顺序排序. 支持双向读写,可以添加一个元素到列表的头部(左边)或者尾部(右边),一个列表最多可以包含2^32-1=4294967295个元素,每个列表元素有下标来标识,下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,元素值可以重复,常用于存入日志等场景,此数据类型比较常用

2、列表特点
	有序
	可重复
	左右都可以操作
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.2.1、创建列表和数据

LPUSH和RPUSH都可以插入列表

LPUSH key value [value …]
时间复杂度: O(1)
将一个或多个值 value 插入到列表 key 的表头
如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表头: 比如说,对空列表
mylist 执行命令 LPUSH mylist a b c ,列表的值将是 c b a ,这等同于原子性地执行 LPUSH 
mylist a 、 LPUSH mylist b 和 LPUSH mylist c 三个命令。
如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。
当 key 存在但不是列表类型时,返回一个错误。
RPUSH key value [value …]

时间复杂度: O(1)
将一个或多个值 value 插入到列表 key 的表尾(最右边)。
如果有多个 value 值,那么各个 value 值按从左到右的顺序依次插入到表尾:比如对一个空列表 mylist 执行 RPUSH mylist a b c ,得出的结果列表为 a b c ,等同于执行命令 RPUSH mylist a 、RPUSH mylist b 、 RPUSH mylist c 。
如果 key 不存在,一个空列表会被创建并执行 RPUSH 操作。
当 key 存在但不是列表类型时,返回一个错误。

#范例:
#从左边添加数据,已添加的需向右移
127.0.0.1:6379> LPUSH name mage wang zhang  #根据顺序逐个写入name,最后的zhang会在列表
的最左侧。
(integer) 3
127.0.0.1:6379> TYPE name
list
#从右边添加数据
127.0.0.1:6379> RPUSH course linux python go
(integer) 3
127.0.0.1:6379> type course
list

实例:
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> lpush list1 c
(integer) 1
127.0.0.1:6379> lpush list1 b
(integer) 2
127.0.0.1:6379> lpush list1 a
(integer) 3
127.0.0.1:6379> lpush list1 3 2 1
(integer) 6
127.0.0.1:6379> type list
none
127.0.0.1:6379> type list1
list
127.0.0.1:6379> llen list1
(integer) 6
127.0.0.1:6379> LINDEX list1 0
"1"
127.0.0.1:6379> LINDEX list1 1
"2"
127.0.0.1:6379> LINDEX list1 2
"3"
127.0.0.1:6379> LINDEX list1 3
"a"
127.0.0.1:6379> LINDEX list1 4
"b"
127.0.0.1:6379> LINDEX list1 5
"c"
127.0.0.1:6379> LINDEX list1 6
(nil)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
2.2.2、列表追加新数据
127.0.0.1:6379> LPUSH list1 tom
(integer) 7
#从右边添加数据,已添加的向左移
127.0.0.1:6379> rpush list1 jack
(integer) 8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.2.3、获取列表长度(元素个数)
127.0.0.1:6379> llen list1
(integer) 8
  • 1
  • 2
2.2.4、获取列表指定位置元素数据
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> lpush list1 c
(integer) 1
127.0.0.1:6379> lpush list1 b
(integer) 2
127.0.0.1:6379> lpush list1 a
(integer) 3
127.0.0.1:6379> lpush list1 3 2 1
(integer) 6
127.0.0.1:6379> type list
none
127.0.0.1:6379> type list1
list
127.0.0.1:6379> llen list1
(integer) 6
127.0.0.1:6379> LINDEX list1 0
"1"
127.0.0.1:6379> LINDEX list1 1
"2"
127.0.0.1:6379> LINDEX list1 2
"3"
127.0.0.1:6379> LINDEX list1 3
"a"
127.0.0.1:6379> LINDEX list1 4
"b"
127.0.0.1:6379> LINDEX list1 5
"c"
127.0.0.1:6379> LINDEX list1 6
(nil)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

指定位置

127.0.0.1:6379> rpush list2 zhang wang li zhao
(integer) 4
127.0.0.1:6379> LRANGE list2 1 2
1) "wang"
2) "li"
127.0.0.1:6379> LRANGE list2 2 2
1) "li"
127.0.0.1:6379> LRANGE list2 0 -1
1) "zhang"
2) "wang"
3) "li"
4) "zhao"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
2.2.5、修改列表指定索引值
127.0.0.1:6379> rpush listkey a b c d e f g h i j k l m n
(integer) 14
127.0.0.1:6379> LLEN listkey
(integer) 14
127.0.0.1:6379> LRANGE listkey 0 -1
 1) "a"
 2) "b"
 3) "c"
 4) "d"
 5) "e"
 6) "f"
 7) "g"
 8) "h"
 9) "i"
10) "j"
11) "k"
12) "l"
13) "m"
14) "n"
127.0.0.1:6379> LSET listkey 2 jave
OK
127.0.0.1:6379> LRANGE listkey 0 -1
 1) "a"
 2) "b"
 3) "jave"
 4) "d"
 5) "e"
 6) "f"
 7) "g"
 8) "h"
 9) "i"
10) "j"
11) "k"
12) "l"
13) "m"
14) "n"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
2.2.6、删除列表数据
#从左边弹出
127.0.0.1:6379> LPOP listkey
"a"

#从右边弹出
127.0.0.1:6379> rPOP listkey
"n"
#一次性弹出两个值
127.0.0.1:6379> rPOP listkey 2
1) "m"
2) "l"
127.0.0.1:6379> LRANGE listkey 0 -1
 1) "b"
 2) "jave"
 3) "d"
 4) "e"
 5) "f"
 6) "g"
 7) "h"
 8) "i"
 9) "j"
10) "k"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2.3、集合set

2.3.1、创建集合
127.0.0.1:6379> SADD myfriend ming hong qiang cai
(integer) 4
127.0.0.1:6379> SADD yourfriend hou zhu ren sha
(integer) 4
127.0.0.1:6379> type myfriend
set
127.0.0.1:6379> type yourfriend
set

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
2.3.2、集合中追加数据
#追加时,只能追加不存在的数据,不能追加已经存在的数值
127.0.0.1:6379> SADD set1 v2 v3 v4
(integer) 3
127.0.0.1:6379> SADD set1 v2 #已存在的value,无法再次添加
(integer) 0
127.0.0.1:6379> TYPE set1
set
127.0.0.1:6379> TYPE set2
set
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
2.3.3、获取集合的所有数据
127.0.0.1:6379> SMEMBERS myfriend
1) "qiang"
2) "cai"
3) "hong"
4) "ming"
127.0.0.1:6379> SMEMBERS yourfriend
1) "zhu"
2) "ren"
3) "sha"
4) "hou"
5) "hong"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
2.3.4、删除集合中的元素
127.0.0.1:6379> SREM yourfriend zhu
(integer) 1
127.0.0.1:6379> SMEMBERS yourfriend
1) "ren"
2) "sha"
3) "hou"
4) "hong"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
2.3.5、取集合的交集
交集:同时属于集合A且属于集合B的元素
可以实现共同的朋友
127.0.0.1:6379> SINTER myfriend yourfriend
(empty array)
127.0.0.1:6379> SADD yourfriend hong
(integer) 1
127.0.0.1:6379> SINTER myfriend yourfriend
1) "hong"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
2.3.6、取集合的并集

并集:属于集合A或者属于集合B的元素

127.0.0.1:6379> SUNION myfriend yourfriend
1) "zhu"
2) "ming"
3) "ren"
4) "qiang"
5) "sha"
6) "cai"
7) "hong"
8) "hou"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
2.3.7、取集合的差集

差集:属于集合A但不属于集合B的元素

可以实现我的朋友的朋友

#属于集合myfriend但是不属于集合yourfriend的元素
127.0.0.1:6379> SMEMBERS myfriend
1) "qiang"
2) "cai"
3) "hong"
4) "ming"
127.0.0.1:6379> SMEMBERS yourfriend
1) "ren"
2) "sha"
3) "hou"
4) "hong"

127.0.0.1:6379> sdiff myfriend yourfriend
1) "qiang"
2) "cai"
3) "ming"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.4、有序集合sorted set

Redis有序集合和Redis集合类似,是不包含相同字符串的合集。它们的差别是,每个有序集合的成员都

关联着一个双精度浮点型的评分,这个评分用于把有序集合中的成员按最低分到最高分排序。有序集合

的成员不能重复,但评分可以重复,一个有序集合中最多的成员数为 2^32 - 1=4294967295个,经常用于

排行榜的场景

#有序集合特点:
	有序
	无重复元素
	每个元素是由score和value组成
	score 可以重复
	value 不可以重复
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.4.1、创建有序集合
127.0.0.1:6379> ZADD course 90 linux 99 go 60 python 50 cloud
(integer) 4
127.0.0.1:6379> ZRANGE course 0 -1
1) "cloud"
2) "python"
3) "linux"
4) "go"
127.0.0.1:6379> ZREVRANGE course 0 -1
1) "go"
2) "linux"
3) "python"
4) "cloud"
127.0.0.1:6379> ZRANGE course 0 -1 withscores
1) "cloud"
2) "50"
3) "python"
4) "60"
5) "linux"
6) "90"
7) "go"
8) "99"
127.0.0.1:6379> ZREVRANGE course 0 -1 withscores
1) "go"
2) "99"
3) "linux"
4) "90"
5) "python"
6) "60"
7) "cloud"
8) "50"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2.4.2、查看集合的成员个数
127.0.0.1:6379> ZADD zset1 60 ming 70 ning 65 hong 75 qiang 99 hang
(integer) 5
127.0.0.1:6379> ZCARD course
(integer) 4

127.0.0.1:6379> ZCARD zset1
(integer) 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.4.3、实现排名
127.0.0.1:6379> ZADD course 90 linux 99 go 60 python 50 cloud
(integer) 4
127.0.0.1:6379> ZRANGE course 0 -1  #正序排序后显示集合内所有的key,按score从小到大显示
1) "cloud"
2) "python"
3) "linux"
4) "go"
127.0.0.1:6379> ZREVRANGE course 0 -1 #倒序排序后显示集合内所有的key,score从大到小显示
1) "go"
2) "linux"
3) "python"
4) "cloud"
127.0.0.1:6379> ZRANGE course 0 -1 WITHSCORES  #正序显示指定集合内所有key和得分情况
1) "cloud"
2) "50"
3) "python"
4) "60"
5) "linux"
6) "90"
7) "go"
8) "99"
127.0.0.1:6379> ZREVRANGE course 0 -1 WITHSCORES  #倒序显示指定集合内所有key和得分情况
1) "go"
2) "99"
3) "linux"
4) "90"
5) "python"
6) "60"
7) "cloud"
8) "50"
127.0.0.1:6379>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2.4.4、基于索引查找数据
127.0.0.1:6379> ZRANGE course 0 2
1) "cloud"
2) "python"
3) "linux"
127.0.0.1:6379> ZRANGE course 0 10
1) "cloud"
2) "python"
3) "linux"
4) "go"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
2.4.5、查询指定数据的排名
127.0.0.1:6379> ZADD course 90 linux 99 go 60 python 50 cloud
(integer) 4

127.0.0.1:6379> ZRANGE course 0 -1 withscores
1) "cloud"
2) "50"
3) "python"
4) "60"
5) "linux"
6) "90"
7) "go"
8) "99"


127.0.0.1:6379> ZRANK course go
(integer) 3   #第4127.0.0.1:6379> ZRANK course python
(integer) 1   #第2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
2.4.6、获取分数
127.0.0.1:6379> zscore course go
"99"
  • 1
  • 2
2.4.7、删除元素
127.0.0.1:6379> ZRANGE course 0 -1
1) "cloud"
2) "python"
3) "linux"
4) "go"
127.0.0.1:6379> ZREM course python linux
(integer) 2
127.0.0.1:6379> ZRANGE course 0 -1
1) "cloud"
2) "go"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.5、哈希hash

hash 即字典, 用于保存字符串字段field和字符串值value之间的映射,即key/value做为数据部分,hash特
别适合用于存储对象场景.
一个hash最多可以包含2^32-1 个key/value键值对
#哈希特点:
	无序
	k/v 对
	适用于存放相关的数据
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.5.1、创建hash
#格式
HSET hash field value
时间复杂度: O(1)
将哈希表 hash 中域 field 的值设置为 value 。
如果给定的哈希表并不存在, 那么一个新的哈希表将被创建并执行 HSET 操作。
如果域 field 已经存在于哈希表中, 那么它的旧值将被新值 value 覆盖。

实例:
127.0.0.1:6379> HSET 1999 name dingbaohang age 24
(integer) 2
127.0.0.1:6379> type 1999
hash
127.0.0.1:6379> HGET 1999
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> HGETALL 1999
1) "name"
2) "dingbaohang"
3) "age"
4) "24"

#增加字段
127.0.0.1:6379> HSET 1999 gender male
(integer) 1
127.0.0.1:6379> HGETALL 1999
1) "name"
2) "dingbaohang"
3) "age"
4) "24"
5) "gender"
6) "male"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2.5.2、查看hash的指定field的value
127.0.0.1:6379> HGET 1999 name
"dingbaohang"
127.0.0.1:6379> HGET 1999 age
"24"
127.0.0.1:6379> HGET 1999 gender
"male"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.5.3、删除hash的指定的field/value
127.0.0.1:6379> HDEL 1999 gender
(integer) 1
127.0.0.1:6379> HGETALL 1999
1) "name"
2) "dingbaohang"
3) "age"
4) "24"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
2.5.4、批量设置hash key的多个field和value
127.0.0.1:6379> HMSET 1999 gender male city shanghai address zhuanqiao
OK
127.0.0.1:6379> HGETALL 1999
 1) "name"
 2) "dingbaohang"
 3) "age"
 4) "24"
 5) "gender"
 6) "male"
 7) "city"
 8) "shanghai"
 9) "address"
10) "zhuanqiao"	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
2.5.5、查看hash指定field的value
127.0.0.1:6379> HGETALL 1999
 1) "name"
 2) "dingbaohang"
 3) "age"
 4) "24"
 5) "gender"
 6) "male"
 7) "city"
 8) "shanghai"
 9) "address"
10) "zhuanqiao"
127.0.0.1:6379> HMGET 1999 name age
1) "dingbaohang"
2) "24"
127.0.0.1:6379> HMGET 1999 name city
1) "dingbaohang"
2) "shanghai"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
2.5.6、查看hash的所有field
127.0.0.1:6379> HMSET 1999 name shipanpan age 26 city shandong gender male
OK
127.0.0.1:6379> HGETALL 1999
 1) "name"
 2) "shipanpan"
 3) "age"
 4) "26"
 5) "gender"
 6) "male"
 7) "city"
 8) "shandong"
 9) "address"
10) "zhuanqiao"
127.0.0.1:6379> HKEYS 1999
1) "name"
2) "age"
3) "gender"
4) "city"
5) "address"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
2.5.7、查看hash 所有value
127.0.0.1:6379> HMSET 1999 name shipanpan age 26 city shandong gender male
OK
127.0.0.1:6379> HGETALL 1999
 1) "name"
 2) "shipanpan"
 3) "age"
 4) "26"
 5) "gender"
 6) "male"
 7) "city"
 8) "shandong"
 9) "address"
10) "zhuanqiao"

127.0.0.1:6379> HVALS 1999
1) "shipanpan"
2) "26"
3) "male"
4) "shandong"
5) "zhuanqiao"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
2.5.8、删除 hash
127.0.0.1:6379> DEL 1999
(integer) 1
127.0.0.1:6379> HMGET 1999 name city
1) (nil)
2) (nil)
  • 1
  • 2
  • 3
  • 4
  • 5

2.6、消息队列

消息队列: 把要传输的数据放在队列中,从而实现应用之间的数据交换
常用功能: 可以实现多个应用系统之间的解耦,异步,削峰/限流等
常用的消息队列应用: Kafka,RabbitMQ,Redis

消息队列分为两种
生产者/消费者模式: Producer/Consumer
发布者/订阅者模式: Publisher/Subscriber
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
2.6.1、生产者消费者模式
模式说明
生产者消费者模式下,多个消费者同时监听一个频道(redis用队列实现),但是生产者产生的一个消息只
能被最先抢到消息的一个消费者消费一次,队列中的消息由可以多个生产者写入,也可以有不同的消费者
取出进行消费处理.此模式应用广泛
  • 1
  • 2
  • 3
  • 4
1、生产者生成消息
127.0.0.1:6379> LPUSH channel1 message1
(integer) 1
127.0.0.1:6379> LPUSH channel1 message2
(integer) 2
127.0.0.1:6379> LPUSH channel1 message3
(integer) 3
127.0.0.1:6379> LPUSH channel1 message4
(integer) 4
127.0.0.1:6379> LPUSH channel1 message5
(integer) 5

2、获取所有消息
127.0.0.1:6379> LRANGE channel1 0 -1
1) "message5"
2) "message4"
3) "message3"
4) "message2"
5) "message1"

3、消费者消费消息
127.0.0.1:6379> LPOP channel1 
"message5"
127.0.0.1:6379> RPOP channel1 
"message1"
127.0.0.1:6379> LRANGE channel1 0 -1
1) "message4"
2) "message3"
3) "message2"
127.0.0.1:6379> RPOP channel1
"message2"
127.0.0.1:6379> RPOP channel1
"message3"
127.0.0.1:6379> RPOP channel1
"message4"

4、验证队列消息消费完成、
127.0.0.1:6379> LRANGE channel1 0 -1   #验证队列中的消息全部消费完成
(empty array)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
2.6.2、发布者订阅模式
模式说明
在发布者订阅者Publisher/Subscriber模式下,发布者Publisher将消息发布到指定的频道channel,事
先监听此channel的一个或多个订阅者Subscriber都会收到相同的消息。即一个消息可以由多个订阅者
获取到. 对于社交应用中的群聊、群发、群公告等场景适用于此模式
  • 1
  • 2
  • 3
  • 4
1、订阅者订阅频道
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1


[root@Ubuntu1804 ~]# redis-cli -h 192.168.188.88
192.168.188.88:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1


127.0.0.1:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1

2、发布者发布消息
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> PUBLISH message1 duchele!
(integer) 3

3、各个订阅者都能收到消息
127.0.0.1:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1
1) "message"
2) "message1"
3) "duchele!"

[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1
1) "message"
2) "message1"
3) "duchele!"

[root@Ubuntu1804 ~]# redis-cli -h 192.168.188.88
192.168.188.88:6379> SUBSCRIBE message1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1
1) "message"
2) "message1"
3) "duchele!"

4、订阅多个频道
#订阅指定的多个频道
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> SUBSCRIBE message1 message2 message3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1
1) "subscribe"
2) "message2"
3) (integer) 2
1) "subscribe"
2) "message3"
3) (integer) 3

#发布多个消息
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> PUBLISH message1 kaishile!
(integer) 2
127.0.0.1:6379> PUBLISH message2 qianggouzhong!
(integer) 1
127.0.0.1:6379> PUBLISH message3 jieshule!
(integer) 1

#接受消息
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> SUBSCRIBE message1 message2 message3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "message1"
3) (integer) 1
1) "subscribe"
2) "message2"
3) (integer) 2
1) "subscribe"
2) "message3"
3) (integer) 3
1) "message"
2) "message1"
3) "kaishile!"
1) "message"
2) "message2"
3) "qianggouzhong!"
1) "message"
2) "message3"
3) "jieshule!"

5、 订阅所有频道
127.0.0.1:6379> PSUBSCRIBE *  #支持通配符*

6、 订阅匹配的频道
127.0.0.1:6379> PSUBSCRIBE chann* #匹配订阅多个频道

7、取消订阅频道
[root@Rocky8 ~]# redis-cli 
127.0.0.1:6379> UNSUBSCRIBE message1
1) "unsubscribe"
2) "message1"
3) (integer) 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

三、总结 哨兵机制实现原理,并搭建主从哨兵集群。

哨兵模式原理:

主从架构和MySQL的主从复制一样,无法实现master和slave角色的自动切换,即当master出现故障时,
不能实现自动的将一个slave 节点提升为新的master节点,即主从复制无法实现自动的故障转移功能,如果
想实现转移,则需要手动修改配置,才能将 slave 服务器提升新的master节点.此外只有一个主节点支持写
操作,所以业务量很大时会导致Redis服务性能达到瓶颈
  • 1
  • 2
  • 3
  • 4

需要解决的主从复制的存在以下弊端:

1、master和slave角色的自动切换,且不能影响业务
2、提升Redis服务整体性能,支持更高并发访问
  • 1
  • 2

哨兵Sentinel工作原理

哨兵Sentinel从Redis2.6版本开始引用,Redis 2.8版本之后稳定可用。生产环境如果要使用此功能建议
使用Redis的2.8版本以上版本
  • 1
  • 2

1、实现主从复制

1.1、主节点master配置

Redis Server 默认为 master节点,如果要配置为从节点,需要指定master服务器的IP,端口及连接密码在从节点执行 REPLICAOF MASTER_IP PORT 指令可以启用主从同步复制功能,早期版本使用 SLAVEOF 指令

#主节点master配置
[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> CONFIG set requirepass 123456
OK
127.0.0.1:6379> AUTH 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.188.81,port=6379,state=online,offset=42,lag=1
master_replid:5937ff079b9d1a82c5e15033c6c0617400435b95
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:42
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:42
127.0.0.1:6379> set class n75
OK
127.0.0.1:6379> dbsize
(integer) 1
127.0.0.1:6379> GET class
"n75"
127.0.0.1:6379> set car bwm
OK
127.0.0.1:6379> set people zhang
OK
127.0.0.1:6379> info replication   #查看实现一主二从
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.188.81,port=6379,state=online,offset=1770,lag=0
slave1:ip=192.168.188.82,port=6379,state=online,offset=1770,lag=0
master_replid:5937ff079b9d1a82c5e15033c6c0617400435b95
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1770
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1770
127.0.0.1:6379> 

#查看master日志文件
[root@Rocky8 ~]#tail -f /var/log/redis/redis.log 
2248:M 31 Aug 2023 22:37:40.757 * Full resync requested by replica 192.168.188.81:6379
2248:M 31 Aug 2023 22:37:40.757 * Starting BGSAVE for SYNC with target: disk
2248:M 31 Aug 2023 22:37:40.758 * Background saving started by pid 2254
2254:C 31 Aug 2023 22:37:40.765 * DB saved on disk
2254:C 31 Aug 2023 22:37:40.766 * RDB: 0 MB of memory used by copy-on-write
2248:M 31 Aug 2023 22:37:40.829 * Background saving terminated with success
2248:M 31 Aug 2023 22:37:40.829 * Synchronization with replica 192.168.188.81:6379 succeeded
2248:M 31 Aug 2023 22:42:48.672 # Connection with replica client id #38 lost.
2248:M 31 Aug 2023 22:43:41.979 * Replica 192.168.188.81:6379 asks for synchronization
2248:M 31 Aug 2023 22:43:41.979 * Partial resynchronization request from 192.168.188.81:6379 accepted. Sending 0 bytes of backlog starting from offset 522.
2248:M 31 Aug 2023 22:51:59.191 * Replica 192.168.188.82:6379 asks for synchronization
2248:M 31 Aug 2023 22:51:59.191 * Full resync requested by replica 192.168.188.82:6379
2248:M 31 Aug 2023 22:51:59.191 * Starting BGSAVE for SYNC with target: disk
2248:M 31 Aug 2023 22:51:59.191 * Background saving started by pid 2312
2312:C 31 Aug 2023 22:51:59.193 * DB saved on disk
2312:C 31 Aug 2023 22:51:59.193 * RDB: 0 MB of memory used by copy-on-write
2248:M 31 Aug 2023 22:51:59.258 * Background saving terminated with success
2248:M 31 Aug 2023 22:51:59.259 * Synchronization with replica 192.168.188.82:6379 succeeded
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

1.2、slave节点配置

#从slave1、slave2配置
[root@Rocky8 ~]#vim /etc/redis.conf     #修改配置文件replicaof及masterauth两项

# replicaof <masterip> <masterport>
replicaof 192.168.188.8 6379 

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
masterauth 123456

[root@Rocky8 ~]#systemctl enable --now redis
[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 2
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

1.3、查看各节点状态

master节点
[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.188.81,port=6379,state=online,offset=4887218,lag=1
slave1:ip=192.168.188.82,port=6379,state=online,offset=4887218,lag=1
master_replid:928193a98ec853580ccb50c815fe1a56ce69db86
master_replid2:62675d066a34b73762026183517b71b59f47c65a
master_repl_offset:4887360
second_repl_offset:4075926
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3921482
repl_backlog_histlen:965879
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
slave1、2节点状态
[root@Rocky8 ~]#redis-cli -a 123456   #slave1状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 100005
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.188.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:4901624
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:928193a98ec853580ccb50c815fe1a56ce69db86
master_replid2:62675d066a34b73762026183517b71b59f47c65a
master_repl_offset:4901624
second_repl_offset:4075926
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3993038
repl_backlog_histlen:908587

[root@Rocky8 ~]#redis-cli -a 123456   #slave2节点状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 100005
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.188.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:4911455
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:928193a98ec853580ccb50c815fe1a56ce69db86
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4911455
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4089633
repl_backlog_histlen:821823
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2、哨兵模式实现

#哨兵模式要先实现主从复制
[root@Rocky8 ~]# vim /etc/redis.conf
replicaof 192.168.188.8 6379 
requirepass 123456
masterauth 123456

#master、slave1、slave2的sentinel配置文件
[root@Rocky8 ~]#vim /etc/redis-sentinel.conf
bind 0.0.0.0
logfile "/var/log/redis/sentinel.log"
sentinel monitor mymaster 192.168.188.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 3000

[root@Rocky8 ~]#systemctl enable --now redis-sentinel.service 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.1、验证sentinel各节点状态

2.1.1、master
[root@Rocky8 ~]#redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.188.8:6379,slaves=2,sentinels=3

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
2.1.2、slave1、2
[root@Rocky8 ~]#redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.188.8:6379,slaves=2,sentinels=3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.2、验证故障转移

2.2.1、master节点宕机
[root@Rocky8 ~]#systemctl stop redis.service
  • 1

查看日志

#观察日志切至slave1
[root@Rocky8 ~]#tail -f /var/log/redis/sentinel.log 
25407:X 12 Sep 2023 15:04:19.469 # Configuration loaded
25407:X 12 Sep 2023 15:04:19.469 * supervised by systemd, will signal readiness
25407:X 12 Sep 2023 15:04:19.470 * Running mode=sentinel, port=26379.
25407:X 12 Sep 2023 15:04:19.470 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25407:X 12 Sep 2023 15:04:19.471 # Sentinel ID is e14ebe6a10bd2f458dff58b0306f6cb6e01d1259
25407:X 12 Sep 2023 15:04:19.471 # +monitor master mymaster 192.168.188.8 6379 quorum 2
25407:X 12 Sep 2023 15:04:19.472 * +slave slave 192.168.188.81:6379 192.168.188.81 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:04:19.473 * +slave slave 192.168.188.82:6379 192.168.188.82 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:04:20.708 * +sentinel sentinel 2f94db21c4c5afc83b777081eb5d215bad6ebcf1 192.168.188.82 26379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:04:21.476 * +sentinel sentinel 9e355c1f92c9f4a43c8b3e647a9026ba7a076a14 192.168.188.81 26379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.700 # +sdown master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.763 # +odown master mymaster 192.168.188.8 6379 #quorum 2/2
25407:X 12 Sep 2023 15:17:29.763 # +new-epoch 1
25407:X 12 Sep 2023 15:17:29.763 # +try-failover master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.765 # +vote-for-leader e14ebe6a10bd2f458dff58b0306f6cb6e01d1259 1
25407:X 12 Sep 2023 15:17:29.768 # 2f94db21c4c5afc83b777081eb5d215bad6ebcf1 voted for e14ebe6a10bd2f458dff58b0306f6cb6e01d1259 1
25407:X 12 Sep 2023 15:17:29.768 # 9e355c1f92c9f4a43c8b3e647a9026ba7a076a14 voted for e14ebe6a10bd2f458dff58b0306f6cb6e01d1259 1
25407:X 12 Sep 2023 15:17:29.837 # +elected-leader master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.837 # +failover-state-select-slave master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.895 # +selected-slave slave 192.168.188.81:6379 192.168.188.81 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.896 * +failover-state-send-slaveof-noone slave 192.168.188.81:6379 192.168.188.81 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:29.958 * +failover-state-wait-promotion slave 192.168.188.81:6379 192.168.188.81 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:30.150 # +promoted-slave slave 192.168.188.81:6379 192.168.188.81 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:30.150 # +failover-state-reconf-slaves master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:30.226 * +slave-reconf-sent slave 192.168.188.82:6379 192.168.188.82 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:30.921 # -odown master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:31.242 * +slave-reconf-inprog slave 192.168.188.82:6379 192.168.188.82 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:31.242 * +slave-reconf-done slave 192.168.188.82:6379 192.168.188.82 6379 @ mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:31.331 # +failover-end master mymaster 192.168.188.8 6379
25407:X 12 Sep 2023 15:17:31.332 # +switch-master mymaster 192.168.188.8 6379 192.168.188.81 6379

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
2.2.2、查看各节点状态

原master

[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> 
  • 1
  • 2
  • 3
  • 4

81主机

[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.188.82
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:5258199
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:48e74ab4535090bae078e18f6ff7d1aa92756880
master_replid2:928193a98ec853580ccb50c815fe1a56ce69db86
master_repl_offset:5258199
second_repl_offset:5253157
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4209624
repl_backlog_histlen:1048576
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

82主机

[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.188.81,port=6379,state=online,offset=5268398,lag=1
master_replid:48e74ab4535090bae078e18f6ff7d1aa92756880
master_replid2:928193a98ec853580ccb50c815fe1a56ce69db86
master_repl_offset:5268541
second_repl_offset:5253157
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4219966
repl_backlog_histlen:1048576
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
2.2.3、原master重新加入Redis集群
[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.188.82
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:5285368
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:48e74ab4535090bae078e18f6ff7d1aa92756880
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:5285368
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5285203
repl_backlog_histlen:166
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

验证复制

#验证节点复制情况
[root@Rocky8 ~]#redis-cli -a 123456  #.82
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 100005
127.0.0.1:6379> set pan hang
OK
127.0.0.1:6379> dbsize
(integer) 100006

[root@Rocky8 ~]#redis-cli -a 123456   #.81
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 100006

[root@Rocky8 ~]#redis-cli -a 123456   #.8
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> dbsize
(integer) 100006
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

四、总结redis cluster工作原理,并搭建集群实现扩缩容。

1、redis-cluster工作原理

1、节点拓扑:Redis Cluster 由多个节点组成,每个节点都是一个独立的 Redis 实例。节点通过互相通信来共享状态信息,并协调数据分片和故障转移。

2、数据分片:Redis Cluster 使用哈希槽(hash slot)来分片数据。哈希槽是一个固定数量的逻辑容器,通常是 16384 个。每个键通过哈希算法被映射到一个哈希槽中。节点负责持有一部分哈希槽的数据,并处理相关的命令。

3、节点间通信:节点之间使用内部二进制协议进行通信。每个节点都维护一个关于集群其他节点的状态的视图。节点通过定期交换消息来保持视图一致性,并检测其他节点的可用性。

4、主从复制:每个哈希槽都会有一个主节点负责处理读写命令,并可以有零个或多个从节点用于数据复制和故障转移。主节点负责将数据同步到其从节点,并且在主节点故障时,从节点可以自动被选举为新的主节点。

5、故障检测和转移:当一个节点失效或离线时,其他节点会通过集体投票选择新的主节点来接管失效节点的哈希槽。故障转移过程包括选举、同步和配置更新等步骤,以确保集群的可用性和一致性。

6、客户端路由:客户端使用 Redis Cluster 客户端库连接到集群,并通过集群的节点进行交互。客户端根据键的哈希槽将命令路由到对应的节点,从而实现数据的读写。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、redis-cluster集群实现

2.1、准备6台主机

三主三从:
192.168.188.8
192.168.188.81
192.168.188.82
192.168.188.83
192.168.188.84
192.168.188.85
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.2、基于Redis 5 以上版本的 redis cluster 部署

1、yum安装redis并修改相关配置

[root@Rocky8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
[root@Rocky8 ~]#systemctl enable --now redis.service

2、启动并建立cluster集群

[root@Rocky8 ~]#redis-cli -a 123456 --cluster create 192.168.188.8:6379 192.168.188.81:6379 192.168.188.82:6379 192.168.188.83:6379 192.168.188.84:6379 192.168.188.85:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.188.83:6379 to 192.168.188.8:6379
Adding replica 192.168.188.84:6379 to 192.168.188.81:6379
Adding replica 192.168.188.85:6379 to 192.168.188.82:6379
M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
   slots:[0-5460] (5461 slots) master
M: 5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379
   slots:[5461-10922] (5462 slots) master
M: ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379
   slots:[10923-16383] (5461 slots) master
S: 2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379
   replicates 86792cc551f17549ca90ddc6b2c8f1006fd4c974
S: e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379
   replicates 5c425a605cb60e78111fff001bfa4fc0d831e07a
S: 4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379
   replicates ae4755464a1791e18f38b1a59af2ce3135f39fa1
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 192.168.188.8:6379)
M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379
   slots: (0 slots) slave
   replicates ae4755464a1791e18f38b1a59af2ce3135f39fa1
S: e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379
   slots: (0 slots) slave
   replicates 5c425a605cb60e78111fff001bfa4fc0d831e07a
M: ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379
   slots: (0 slots) slave
   replicates 86792cc551f17549ca90ddc6b2c8f1006fd4c974
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

3、查看集群信息

[root@Rocky8 ~]#redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:386
cluster_stats_messages_pong_sent:361
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:750
cluster_stats_messages_ping_received:357
cluster_stats_messages_pong_received:389
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:750

4、不会自动选择节点加-c集群模式链接

[root@Rocky8 ~]#redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set car abc
(error) MOVED 9461 192.168.188.81:6379
127.0.0.1:6379> get car
(error) MOVED 9461 192.168.188.81:6379
127.0.0.1:6379> quit
[root@Rocky8 ~]#redis-cli -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set car abc
-> Redirected to slot [9461] located at 192.168.188.81:6379
OK
192.168.188.81:6379> get car
"abc"

#集群中各节点状态
[root@Rocky8 ~]#cat /var/lib/redis/nodes-6379.conf 
2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379@16379 slave 86792cc551f17549ca90ddc6b2c8f1006fd4c974 0 1694532738735 10 connected
4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379@16379 slave ae4755464a1791e18f38b1a59af2ce3135f39fa1 0 1694532736723 12 connected
ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379@16379 master - 0 1694532734710 12 connected 6825-6826 10923-16383
e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379@16379 slave 5c425a605cb60e78111fff001bfa4fc0d831e07a 0 1694532733201 11 connected
5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379@16379 myself,master - 0 1694525168098 11 connected 1364 5461-6824 6827-10922
86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379@16379 master - 0 1694532737729 10 connected 0-1363 1365-5460
vars currentEpoch 12 lastVoteEpoch 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

2.3、python程序实现redis cluster访问

[root@Rocky8 ~]#yum -y install python3
[root@Rocky8 ~]#pip3 install redis-py-cluster
[root@Rocky8 ~]#vim redis_cluster_test.py 

#!/usr/bin/env python3
from rediscluster  import RedisCluster

if __name__ == '__main__':

    startup_nodes = [ 
        {"host":"192.168.188.8", "port":6379},
        {"host":"192.168.188.81", "port":6379},
        {"host":"192.168.188.82", "port":6379},
        {"host":"192.168.188.83", "port":6379},
        {"host":"192.168.188.84", "port":6379},
        {"host":"192.168.188.85", "port":6379}]
    try:
        redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True)
    except Exception as e:
        print(e)

    for i in range(0, 10000):
        redis_conn.set('key'+str(i),'value'+str(i))
        print('key'+str(i)+':',redis_conn.get('key'+str(i)))

[root@Rocky8 ~]#chmod +x redis_cluster_test.py
[root@Rocky8 ~]#./redis_cluster_test.py

#验证数据插入10000条记录
可以看出三台主节点各有数据3000多条
[root@Rocky8 ~]#redis-cli -a 123456 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3331

[root@Rocky8 ~]#redis-cli -a 123456 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3341

[root@Rocky8 ~]#redis-cli -a 123456 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3329

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

3、实现缩扩容

3.1、适用场景

扩容适用场景:
当前客户量激增,现有的Redis cluster架构已经无法满足越来越高的并发访问请求,为解决此问题,新购置两台服务器,要求将其动态添加到现有集群,但不能影响业务的正常访问。
注意: 生产环境一般建议master节点为奇数个,比如:3,5,7,以防止脑裂现象
  • 1
  • 2
  • 3

3.2、扩容

3.2.1、准备新master主机
增加Redis 新节点,需要与之前的Redis node版本和配置一致,然后分别再启动两台Redis node,应为一主一从。

#yum安装并修改redis配置文件
[root@Rocky8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-requirefull-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
[root@Rocky8 ~]#systemctl enable --now redis.service
  • 1
  • 2
  • 3
  • 4
  • 5
3.2.2、添加新master节点到集群
使用以下命令添加新节点,要添加的新redis节点IP和端口添加到的已有的集群中任意节点的IP:端口

add-node new_host:new_port existing_host:existing_port [--slave --master-id
<arg>]
#说明:
new_host:new_port #指定新添加的主机的IP和端口
existing_host:existing_port #指定已有的集群中任意节点的IP和端口


#使用redis cluser命令添加新的master节点
[root@Rocky8 ~]#redis-cli -a 123456 --cluster add-node 192.168.188.86:6379  192.168.188.8:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.188.86:6379 to cluster 192.168.188.8:6379
>>> Performing Cluster Check (using node 192.168.188.8:6379)
M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379
   slots: (0 slots) slave
   replicates ae4755464a1791e18f38b1a59af2ce3135f39fa1
S: e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379
   slots: (0 slots) slave
   replicates 5c425a605cb60e78111fff001bfa4fc0d831e07a
M: ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379
   slots: (0 slots) slave
   replicates 86792cc551f17549ca90ddc6b2c8f1006fd4c974
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.188.86:6379 to make it join the cluster.
[OK] New node added correctly.

[root@Rocky8 ~]#redis-cli -a 123456 cluster nodes  #查看新增节点状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379@16379 slave 5c425a605cb60e78111fff001bfa4fc0d831e07a 0 1694757956857 8 connected
ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379@16379 master - 0 1694757954842 3 connected 10923-16383
5201b067425cb7e6f837a33b55ec630272d293c1 192.168.188.86:6379@16379 myself,master - 0 1694703174000 0 connected
4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379@16379 slave ae4755464a1791e18f38b1a59af2ce3135f39fa1 0 1694757959875 3 connected
2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379@16379 slave 86792cc551f17549ca90ddc6b2c8f1006fd4c974 0 1694757957863 1 connected
5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379@16379 master - 0 1694757955851 8 connected 5461-10922
86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379@16379 master - 0 1694757958870 1 connected 0-5460

[root@Rocky8 ~]#redis-cli -a 123456 cluster info  #查看集群状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:8
cluster_my_epoch:0
cluster_stats_messages_ping_sent:248
cluster_stats_messages_pong_sent:259
cluster_stats_messages_meet_sent:6
cluster_stats_messages_sent:513
cluster_stats_messages_ping_received:259
cluster_stats_messages_pong_received:254
cluster_stats_messages_received:513

#重新分配槽位
[root@Rocky8 ~]#redis-cli -a 123456 --cluster reshard 192.168.188.8:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.188.8:6379)
M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
   slots:[0-1363],[1365-5460] (5460 slots) master
   1 additional replica(s)
M: 5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379
   slots:[1364],[5461-6824],[6827-10922] (5461 slots) master
   1 additional replica(s)
S: 4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379
   slots: (0 slots) slave
   replicates ae4755464a1791e18f38b1a59af2ce3135f39fa1
S: e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379
   slots: (0 slots) slave
   replicates 5c425a605cb60e78111fff001bfa4fc0d831e07a
M: ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379
   slots:[6825-6826],[10923-16383] (5463 slots) master
   1 additional replica(s)
M: ceb4d5c6e92a5f38f9cb5e82ec7135274380944c 192.168.188.86:6379
   slots: (0 slots) master
S: 2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379
   slots: (0 slots) slave
   replicates 86792cc551f17549ca90ddc6b2c8f1006fd4c974
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1363
What is the receiving node ID? 86792cc551f17549ca90ddc6b2c8f1006fd4c974
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: ceb4d5c6e92a5f38f9cb5e82ec7135274380944c
Source node #2: done

Ready to move 1363 slots.
  Source nodes:
    M: ceb4d5c6e92a5f38f9cb5e82ec7135274380944c 192.168.188.86:6379
       slots: (0 slots) master
  Destination node:
    M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
       slots:[0-1363],[1365-5460] (5460 slots) master
       1 additional replica(s)
  Resharding plan:
Do you want to proceed with the proposed reshard plan (yes/no)? yes

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
3.2.3、验证集群状态
[root@Rocky8 ~]#cat /var/lib/redis/nodes-6379.conf 
5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379@16379 master - 0 1683823411636 8 connected 6827-10922
4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379@16379 slave ae4755464a1791e18f38b1a59af2ce3135f39fa1 0 1683823411132 6 connected
e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379@16379 slave 5c425a605cb60e78111fff001bfa4fc0d831e07a 0 1683823407099 8 connected
ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379@16379 master - 0 1683823409114 3 connected 12288-16383
86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379@16379 myself,master - 0 0 1 connected 1365-5460
0f2d5e5b805b6e38d975594def99148249fd25eb 192.168.188.87:6379@16379 slave 5201b067425cb7e6f837a33b55ec630272d293c1 0 1683823410022 9 connected
5201b067425cb7e6f837a33b55ec630272d293c1 192.168.188.86:6379@16379 master - 0 1683823408108 9 connected 0-1364 5461-6826 10923-12287
2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379@16379 slave 86792cc551f17549ca90ddc6b2c8f1006fd4c974 0 1683823410123 4 connected
vars currentEpoch 9 lastVoteEpoch 8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3.3、缩容集群中节点

#查看槽位分配情况
[root@Rocky8 ~]#cat /var/lib/redis/nodes-6379.conf 
5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379@16379 master - 0 1683823411636 8 connected 6827-10922
4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379@16379 slave ae4755464a1791e18f38b1a59af2ce3135f39fa1 0 1683823411132 6 connected
e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379@16379 slave 5c425a605cb60e78111fff001bfa4fc0d831e07a 0 1683823407099 8 connected
ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379@16379 master - 0 1683823409114 3 connected 12288-16383
86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379@16379 myself,master - 0 0 1 connected 1365-5460
0f2d5e5b805b6e38d975594def99148249fd25eb 192.168.188.87:6379@16379 slave 5201b067425cb7e6f837a33b55ec630272d293c1 0 1683823410022 9 connected
5201b067425cb7e6f837a33b55ec630272d293c1 192.168.188.86:6379@16379 master - 0 1683823408108 9 connected 0-1364 5461-6826 10923-12287
2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379@16379 slave 86792cc551f17549ca90ddc6b2c8f1006fd4c974 0 1683823410123 4 connected
vars currentEpoch 9 lastVoteEpoch 8

#释放0-1364槽位
[root@Rocky8 ~]#redis-cli -a 123456 --cluster reshard 192.168.188.8:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.188.8:6379)
M: 86792cc551f17549ca90ddc6b2c8f1006fd4c974 192.168.188.8:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 5c425a605cb60e78111fff001bfa4fc0d831e07a 192.168.188.81:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 4d1b44cb817de8f88c767e0c32b279e2afbabba2 192.168.188.85:6379
   slots: (0 slots) slave
   replicates ae4755464a1791e18f38b1a59af2ce3135f39fa1
S: e4162d73be9dbdd4c086149766a3a532bf5aca01 192.168.188.84:6379
   slots: (0 slots) slave
   replicates 5c425a605cb60e78111fff001bfa4fc0d831e07a
M: ae4755464a1791e18f38b1a59af2ce3135f39fa1 192.168.188.82:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 0f2d5e5b805b6e38d975594def99148249fd25eb 192.168.188.87:6379
   slots: (0 slots) slave
   replicates 5201b067425cb7e6f837a33b55ec630272d293c1
M: 5201b067425cb7e6f837a33b55ec630272d293c1 192.168.188.86:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 2e4b7972d1b0ab02b6e816ef522dd71290055ce9 192.168.188.83:6379
   slots: (0 slots) slave
   replicates 86792cc551f17549ca90ddc6b2c8f1006fd4c974
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1364
What is the receiving node ID? 86792cc551f17549ca90ddc6b2c8f1006fd4c974
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 5201b067425cb7e6f837a33b55ec630272d293c1
Source node #2: done


.。。。。。。。。
    Moving slot 1357 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1358 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1359 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1360 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1361 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1362 from 5201b067425cb7e6f837a33b55ec630272d293c1
    Moving slot 1363 from 5201b067425cb7e6f837a33b55ec630272d293c1
Do you want to proceed with the proposed reshard plan (yes/no)? yes



#释放5461-6826槽位
[root@Rocky8 ~]#redis-cli -a 123456 --cluster reshard 192.168.188.8:6379
#释放10923-12287槽位
[root@Rocky8 ~]#redis-cli -a 123456 --cluster reshard 192.168.188.8:6379


#移除集群节点
[root@Rocky8 ~]#redis-cli -a 123456 --cluster del-node 192.168.188.8:6379 1b067425cb7e6f837a33b55ec630272d293c1  #删除后从节点会找新主
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 5201b067425cb7e6f837a33b55ec630272d293c1 from cluster 192.168.188.8:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

#删除从节点
[root@Rocky8 ~]#redis-cli -a 123456 --cluster del-node 192.168.188.8:6379 0f2d5e5b805b6e38d975594def99148249fd25eb
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 0f2d5e5b805b6e38d975594def99148249fd25eb from cluster 192.168.188.8:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/682422
推荐阅读
相关标签
  

闽ICP备14008679号