redis对接其它软件

redis对接其它软件

shell脚本访问redis

bash 复制代码
[root@xhde ~ 09:42:21]# vim write_redis.sh
[root@xhde ~ 09:43:51]# ./write_redis.sh
成功连接到 Redis,开始插入 100 条数据...
已插入: item:1 -> value_1781055834987064976_5580
已插入: item:2 -> value_1781055834991329071_12997
已插入: item:3 -> value_1781055834994563554_19895
已插入: item:4 -> value_1781055834997589747_23784
已插入: item:5 -> value_1781055835000482677_16269
已插入: item:6 -> value_1781055835003753074_22876
已插入: item:7 -> value_1781055835007514318_14386
已插入: item:8 -> value_1781055835011883128_29469
已插入: item:9 -> value_1781055835014400247_9038
已插入: item:10 -> value_1781055835017371241_3801
已插入: item:11 -> value_1781055835021071998_20215
已插入: item:12 -> value_1781055835023795356_20133
已插入: item:13 -> value_1781055835027412935_23083
已插入: item:14 -> value_1781055835031837142_26053
已插入: item:15 -> value_1781055835037187773_31551
已插入: item:16 -> value_1781055835041312388_8714
已插入: item:17 -> value_1781055835043998980_22695
已插入: item:18 -> value_1781055835046757351_26824
已插入: item:19 -> value_1781055835052017645_10161
已插入: item:20 -> value_1781055835055835594_3841
已插入: item:21 -> value_1781055835059005143_7958
已插入: item:22 -> value_1781055835061809235_27062
已插入: item:23 -> value_1781055835064295005_23133
已插入: item:24 -> value_1781055835066682432_20187
已插入: item:25 -> value_1781055835069013028_3820
已插入: item:26 -> value_1781055835071224101_16445
已插入: item:27 -> value_1781055835074005360_1640
已插入: item:28 -> value_1781055835076786295_13295
已插入: item:29 -> value_1781055835079974458_19284
已插入: item:30 -> value_1781055835083647078_1058
已插入: item:31 -> value_1781055835086669547_23755
已插入: item:32 -> value_1781055835090417169_17967
已插入: item:33 -> value_1781055835093991936_22271
已插入: item:34 -> value_1781055835097362603_13375
已插入: item:35 -> value_1781055835100598832_21406
已插入: item:36 -> value_1781055835104742131_27362
已插入: item:37 -> value_1781055835108914047_25866
已插入: item:38 -> value_1781055835111930386_10152
已插入: item:39 -> value_1781055835115275580_26701
已插入: item:40 -> value_1781055835118302093_7413
已插入: item:41 -> value_1781055835121026914_20124
已插入: item:42 -> value_1781055835124121652_10779
已插入: item:43 -> value_1781055835126983852_2238
已插入: item:44 -> value_1781055835129895033_18555
已插入: item:45 -> value_1781055835134259854_14004
已插入: item:46 -> value_1781055835139909307_8711
已插入: item:47 -> value_1781055835142934239_14288
已插入: item:48 -> value_1781055835146743053_28622
已插入: item:49 -> value_1781055835149056445_31734
已插入: item:50 -> value_1781055835151645335_7733
已插入: item:51 -> value_1781055835157620688_18078
已插入: item:52 -> value_1781055835165648299_31570
已插入: item:53 -> value_1781055835172399767_8342
已插入: item:54 -> value_1781055835175722322_7862
已插入: item:55 -> value_1781055835179247436_30044
已插入: item:56 -> value_1781055835183551466_12030
已插入: item:57 -> value_1781055835186249618_20904
已插入: item:58 -> value_1781055835190257624_8513
已插入: item:59 -> value_1781055835193714018_28402
已插入: item:60 -> value_1781055835196237355_31192
已插入: item:61 -> value_1781055835199295641_10011
已插入: item:62 -> value_1781055835203466710_8292
已插入: item:63 -> value_1781055835205922034_14672
已插入: item:64 -> value_1781055835209722720_2795
已插入: item:65 -> value_1781055835212415278_8847
已插入: item:66 -> value_1781055835214984820_992
已插入: item:67 -> value_1781055835217494737_10539
已插入: item:68 -> value_1781055835219850983_26086
已插入: item:69 -> value_1781055835223416364_8360
已插入: item:70 -> value_1781055835226507108_13215
已插入: item:71 -> value_1781055835229569461_22937
已插入: item:72 -> value_1781055835231793772_436
已插入: item:73 -> value_1781055835234860044_11398
已插入: item:74 -> value_1781055835237179802_23681
已插入: item:75 -> value_1781055835242575745_16638
已插入: item:76 -> value_1781055835246617325_11176
已插入: item:77 -> value_1781055835249108358_16992
已插入: item:78 -> value_1781055835251557102_29550
已插入: item:79 -> value_1781055835254593108_24091
已插入: item:80 -> value_1781055835257456023_31890
已插入: item:81 -> value_1781055835260196854_3249
已插入: item:82 -> value_1781055835262481590_22261
已插入: item:83 -> value_1781055835265017357_16040
已插入: item:84 -> value_1781055835269438737_14504
已插入: item:85 -> value_1781055835272876170_23645
已插入: item:86 -> value_1781055835276702033_8195
已插入: item:87 -> value_1781055835279574017_32455
已插入: item:88 -> value_1781055835282031676_23263
已插入: item:89 -> value_1781055835285360179_14592
已插入: item:90 -> value_1781055835288963930_2576
已插入: item:91 -> value_1781055835292193084_21721
已插入: item:92 -> value_1781055835295447036_20668
已插入: item:93 -> value_1781055835297708972_8880
已插入: item:94 -> value_1781055835301094881_678
已插入: item:95 -> value_1781055835304558128_11031
已插入: item:96 -> value_1781055835309480279_16454
已插入: item:97 -> value_1781055835312572727_32752
已插入: item:98 -> value_1781055835315366287_14888
已插入: item:99 -> value_1781055835320123462_18899
已插入: item:100 -> value_1781055835324985176_3655
所有数据插入完成。
[root@xhde ~ 09:43:55]# 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 keyspace
# Keyspace
db0:keys=100,expires=0,avg_ttl=0

图像工具连接redis

bash 复制代码
[root@xhde ~ 09:49:12]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor >
   Active: active (running) since Wed 2026-06-10 09:33:42 CST; 15min ago
     Docs: man:firewalld(1)
 Main PID: 807 (firewalld)
    Tasks: 2 (limit: 23002)
   Memory: 51.9M
   CGroup: /system.slice/firewalld.service
           └─807 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork >

Jun 10 09:33:42 xhde.cloud systemd[1]: Starting firewalld - dynamic firewall d>
Jun 10 09:33:42 xhde.cloud systemd[1]: Started firewalld - dynamic firewall da>
Jun 10 09:33:43 xhde.cloud firewalld[807]: WARNING: AllowZoneDrifting is enabl>
lines 1-13/13 (END)
^C
[root@xhde ~ 09:49:28]# systemctl stop firewalld.service
[root@xhde ~ 09:50:09]# systemctl disabled firewalld.service
Unknown operation disabled.
[root@xhde ~ 09:50:17]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor >
   Active: inactive (dead) since Wed 2026-06-10 09:50:09 CST; 12s ago
     Docs: man:firewalld(1)
  Process: 807 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS >
 Main PID: 807 (code=exited, status=0/SUCCESS)

Jun 10 09:33:42 xhde.cloud systemd[1]: Starting firewalld - dynamic firewall d>
Jun 10 09:33:42 xhde.cloud systemd[1]: Started firewalld - dynamic firewall da>
Jun 10 09:33:43 xhde.cloud firewalld[807]: WARNING: AllowZoneDrifting is enabl>
Jun 10 09:50:09 xhde.cloud systemd[1]: Stopping firewalld - dynamic firewall d>
Jun 10 09:50:09 xhde.cloud systemd[1]: firewalld.service: Succeeded.
Jun 10 09:50:09 xhde.cloud systemd[1]: Stopped firewalld - dynamic firewall da>
[root@xhde ~ 09:45:03]# vim /etc/redis.conf
[root@xhde ~ 09:45:27]# systemctl restart redis

程序连接redis

pycharm连接

bash 复制代码
(.venv) PS C:\Users\PycharmProjects\PythonProject3> pip install redis;
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting redis
  Downloading https://mirrors.aliyun.com/pypi/packages/e9/97/9f22a33c475cda519f20aba6babb340fb2f2254a02fb947816960d1e669a/redis-7.0.1-py3-none-any.whl (339 kB)
Collecting async-timeout>=4.0.3 (from redis)
  Downloading https://mirrors.aliyun.com/pypi/packages/fe/ba/e2081de779ca30d473f21f5b30e0e737c438205440784c7dfc81efc2b029/async_timeout-5.0.1-py3-none-any.whl (6.2 kB)
Installing collected packages: async-timeout, redis
Successfully installed async-timeout-5.0.1 redis-7.0.1                                                                                         

[notice] A new release of pip is available: 25.1.1 -> 26.0.1                                                                                   
[notice] To update, run: python.exe -m pip install --upgrade pip 
import redis
r = redis.Redis(host='192.168.108.172',port=6379,password='123456',db=0)
print(r)

<redis.client.Redis(<redis.connection.ConnectionPool(<redis.connection.Connection(db=0,username=None,password=123456,socket_timeout=None,encoding=utf-8,encoding_errors=strict,decode_responses=False,retry_on_error=[],retry=<redis.retry.Retry object at 0x0000024E54909E50>,health_check_interval=0,client_name=None,lib_name=redis-py,lib_version=7.0.1,redis_connect_func=None,credential_provider=None,protocol=2,host=192.168.108.172,port=6379,socket_connect_timeout=None,socket_keepalive=None,socket_keepalive_options=None)>)>)>

进程已结束,退出代码为 0

设置和获取值

bash 复制代码
import redis
r = redis.Redis(host='192.168.108.141',port=6379,db=0)
r.set('color1','red')
value= r.get('color1')
print(value)

b'red'

进程已结束,退出代码为 0

字符串操作

bash 复制代码
import redis
r = redis.Redis(host='192.168.108.141',port=6379,db=0)
r.set('int1',10)
#自增
restult=r.incr('int1')#自减
restult=r.decr('int1')
print(restult)

10

进程已结束,退出代码为 0

列表操作

bash 复制代码
import redis
r = redis.Redis(host='192.168.108.141',port=6379,db=0)
r.lpush('mylist','nihao')
r.lpush('mylist','xuewu')
print(r.lrange('mylist',0,-1))
[b'xuewu', b'nihao']

进程已结束,退出代码为 0

哈希表操作

bash 复制代码
import redis
r = redis.Redis(host='192.168.108.141',port=6379,db=0)
r.hset('student','name','lisi')
r.hset('student','age','18')
print(r.hget('student','name'))
print(r.hget('student','age'))
print(r.hgetall('student'))
b'lisi'
b'18'
{b'name': b'lisi', b'age': b'18'}

进程已结束,退出代码为 0

主从复制

Redis 主从复制(Master-Slave Replication 是 Redis 实现高可用、读写分离和数据冗余的核心机制

之一。

主从复制特点:

一个master可以有多个slave

一个slave只能有一个master

数据流向是从master到slave单向的

master可读可写

slave只读

master节点

bash 复制代码
[root@m1 ~ 11:14:59]# cd /usr/local/redis-6.2.14/
[root@m1 redis-6.2.14 11:24:30]# vim redis.conf
[root@m1 redis-6.2.14 11:25:06]# redis-c
redis-check-aof  redis-cli
redis-check-rdb
[root@m1 redis-6.2.14 11:25:06]# redis-cli shutdown
Could not connect to Redis at 127.0.0.1:6379: Connection refused
[root@m1 redis-6.2.14 11:26:55]# redis-server redis.conf &
[1] 4002
[root@m1 redis-6.2.14 11:27:40]# 4002:C 10 Jun 2026 11:27:40.768 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4002:C 10 Jun 2026 11:27:40.768 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=4002, just started
4002:C 10 Jun 2026 11:27:40.768 # Configuration loaded
4002:M 10 Jun 2026 11:27:40.768 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4002:M 10 Jun 2026 11:27:40.768 * monotonic clock: POSIX clock_gettime
                _._                
           _.-``__ ''-._           
      _.-``    `.  `_.  ''-._           Redis 6.2.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._ 
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4002
  `-._    `-._  `-./  _.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-' 
      `-._    `-.__.-'    _.-'     
          `-._        _.-'         
              `-.__.-'             

4002:M 10 Jun 2026 11:27:40.769 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4002:M 10 Jun 2026 11:27:40.769 # Server initialized
4002:M 10 Jun 2026 11:27:40.769 # 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.
4002:M 10 Jun 2026 11:27:40.769 * Ready to accept connections
4002:M 10 Jun 2026 11:27:41.754 * Replica 192.168.108.12:6379 asks for synchronization
4002:M 10 Jun 2026 11:27:41.754 * Full resync requested by replica 192.168.108.12:6379
4002:M 10 Jun 2026 11:27:41.754 * Replication backlog created, my new replication IDs are '5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b' and '0000000000000000000000000000000000000000'
4002:M 10 Jun 2026 11:27:41.754 * Starting BGSAVE for SYNC with target: disk
4007:C 10 Jun 2026 11:27:41.788 * DB saved on disk
4007:C 10 Jun 2026 11:27:41.788 * RDB: 4 MB of memory used by copy-on-write
4002:M 10 Jun 2026 11:27:41.795 * Background saving started by pid 4007
4002:M 10 Jun 2026 11:27:41.795 * Background saving terminated with success
4002:M 10 Jun 2026 11:27:41.796 * Synchronization with replica 192.168.108.12:6379 succeeded
4002:M 10 Jun 2026 11:27:41.796 * Replica 192.168.108.11:6379 asks for synchronization
4002:M 10 Jun 2026 11:27:41.796 * Full resync requested by replica 192.168.108.11:6379
4002:M 10 Jun 2026 11:27:41.796 * Starting BGSAVE for SYNC with target: disk
4002:M 10 Jun 2026 11:27:41.797 * Background saving started by pid 4008
4008:C 10 Jun 2026 11:27:41.798 * DB saved on disk
4008:C 10 Jun 2026 11:27:41.799 * RDB: 4 MB of memory used by copy-on-write
4002:M 10 Jun 2026 11:27:41.896 * Background saving terminated with success
4002:M 10 Jun 2026 11:27:41.896 * Synchronization with replica 192.168.108.11:6379 succeeded
[root@m1 redis-6.2.14 11:27:40]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.108.12,port=6379,state=online,offset=70,lag=1
slave1:ip=192.168.108.11,port=6379,state=online,offset=70,lag=1
master_failover_state:no-failover
master_replid:5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70

slave1节点

bash 复制代码
[root@s1 ~ 11:15:10]# cd /usr/local/redis-6.2.14/
[root@s1 redis-6.2.14 11:24:30]# vim redis.conf
[root@s1 redis-6.2.14 11:25:05]# vim redis.conf
[root@s1 redis-6.2.14 11:26:16]# redis-c
redis-check-aof  redis-cli
redis-check-rdb
[root@s1 redis-6.2.14 11:26:16]# redis-cli shutdown
Could not connect to Redis at 127.0.0.1:6379: Connection refused
[root@s1 redis-6.2.14 11:26:55]# redis-server redis.conf &
[1] 3588
[root@s1 redis-6.2.14 11:27:40]# 3588:C 10 Jun 2026 11:27:40.645 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3588:C 10 Jun 2026 11:27:40.645 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=3588, just started
3588:C 10 Jun 2026 11:27:40.645 # Configuration loaded
3588:S 10 Jun 2026 11:27:40.646 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3588:S 10 Jun 2026 11:27:40.646 * monotonic clock: POSIX clock_gettime
                _._                
           _.-``__ ''-._           
      _.-``    `.  `_.  ''-._           Redis 6.2.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._ 
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3588
  `-._    `-._  `-./  _.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-' 
      `-._    `-.__.-'    _.-'     
          `-._        _.-'         
              `-.__.-'             

3588:S 10 Jun 2026 11:27:40.646 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3588:S 10 Jun 2026 11:27:40.646 # Server initialized
3588:S 10 Jun 2026 11:27:40.646 # 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.
3588:S 10 Jun 2026 11:27:40.647 * Ready to accept connections
3588:S 10 Jun 2026 11:27:40.648 * Connecting to MASTER 192.168.108.10:6379
3588:S 10 Jun 2026 11:27:40.648 * MASTER <-> REPLICA sync started
3588:S 10 Jun 2026 11:27:40.648 # Error condition on socket for SYNC: Connection refused
3588:S 10 Jun 2026 11:27:41.654 * Connecting to MASTER 192.168.108.10:6379
3588:S 10 Jun 2026 11:27:41.654 * MASTER <-> REPLICA sync started
3588:S 10 Jun 2026 11:27:41.654 * Non blocking connect for SYNC fired the event.
3588:S 10 Jun 2026 11:27:41.684 * Master replied to PING, replication can continue...
3588:S 10 Jun 2026 11:27:41.685 * Partial resynchronization not possible (no cached master)
3588:S 10 Jun 2026 11:27:41.686 * Full resync from master: 5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b:0
3588:S 10 Jun 2026 11:27:41.785 * MASTER <-> REPLICA sync: receiving 176 bytes from master to disk
3588:S 10 Jun 2026 11:27:41.785 * MASTER <-> REPLICA sync: Flushing old data
3588:S 10 Jun 2026 11:27:41.785 * MASTER <-> REPLICA sync: Loading DB in memory
3588:S 10 Jun 2026 11:27:41.786 * Loading RDB produced by version 6.2.14
3588:S 10 Jun 2026 11:27:41.786 * RDB age 0 seconds
3588:S 10 Jun 2026 11:27:41.786 * RDB memory usage when created 1.85 Mb
3588:S 10 Jun 2026 11:27:41.786 # Done loading RDB, keys loaded: 0, keys expired: 0.
3588:S 10 Jun 2026 11:27:41.786 * MASTER <-> REPLICA sync: Finished with success
[root@s1 redis-6.2.14 11:27:40]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.108.10
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:98
slave_repl_offset:98
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98

slave2节点

bash 复制代码
[root@s2 ~ 11:15:57]# cd /usr/local/redis-6.2.14/
[root@s2 redis-6.2.14 11:24:30]# vim redis.conf
[root@s2 redis-6.2.14 11:25:06]# vim redis.conf
[root@s2 redis-6.2.14 11:26:16]# redis-c
redis-check-aof  redis-cli
redis-check-rdb
[root@s2 redis-6.2.14 11:26:16]# redis-cli shutdown
Could not connect to Redis at 127.0.0.1:6379: Connection refused
[root@s2 redis-6.2.14 11:26:55]# redis-server redis.conf &
[1] 3594
[root@s2 redis-6.2.14 11:27:40]# 3594:C 10 Jun 2026 11:27:40.723 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3594:C 10 Jun 2026 11:27:40.723 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=3594, just started
3594:C 10 Jun 2026 11:27:40.723 # Configuration loaded
3594:S 10 Jun 2026 11:27:40.723 * Increased maximum number of open files to 10032 (it was originally set to 1024).
3594:S 10 Jun 2026 11:27:40.723 * monotonic clock: POSIX clock_gettime
                _._                
           _.-``__ ''-._           
      _.-``    `.  `_.  ''-._           Redis 6.2.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._ 
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3594
  `-._    `-._  `-./  _.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-' 
      `-._    `-.__.-'    _.-'     
          `-._        _.-'         
              `-.__.-'             

3594:S 10 Jun 2026 11:27:40.724 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3594:S 10 Jun 2026 11:27:40.724 # Server initialized
3594:S 10 Jun 2026 11:27:40.724 # 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.
3594:S 10 Jun 2026 11:27:40.724 * Ready to accept connections
3594:S 10 Jun 2026 11:27:40.725 * Connecting to MASTER 192.168.108.10:6379
3594:S 10 Jun 2026 11:27:40.725 * MASTER <-> REPLICA sync started
3594:S 10 Jun 2026 11:27:40.725 # Error condition on socket for SYNC: Connection refused
3594:S 10 Jun 2026 11:27:41.731 * Connecting to MASTER 192.168.108.10:6379
3594:S 10 Jun 2026 11:27:41.731 * MASTER <-> REPLICA sync started
3594:S 10 Jun 2026 11:27:41.731 * Non blocking connect for SYNC fired the event.
3594:S 10 Jun 2026 11:27:41.732 * Master replied to PING, replication can continue...
3594:S 10 Jun 2026 11:27:41.732 * Partial resynchronization not possible (no cached master)
3594:S 10 Jun 2026 11:27:41.774 * Full resync from master: 5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b:0
3594:S 10 Jun 2026 11:27:41.774 * MASTER <-> REPLICA sync: receiving 176 bytes from master to disk
3594:S 10 Jun 2026 11:27:41.774 * MASTER <-> REPLICA sync: Flushing old data
3594:S 10 Jun 2026 11:27:41.775 * MASTER <-> REPLICA sync: Loading DB in memory
3594:S 10 Jun 2026 11:27:41.775 * Loading RDB produced by version 6.2.14
3594:S 10 Jun 2026 11:27:41.775 * RDB age 0 seconds
3594:S 10 Jun 2026 11:27:41.775 * RDB memory usage when created 1.83 Mb
3594:S 10 Jun 2026 11:27:41.775 # Done loading RDB, keys loaded: 0, keys expired: 0.
3594:S 10 Jun 2026 11:27:41.775 * MASTER <-> REPLICA sync: Finished with success
[root@s2 redis-6.2.14 11:27:40]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.108.10
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_read_repl_offset:98
slave_repl_offset:98
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:5d0c52b2cbd4beda3882d7b4dde529ffc0fe6c9b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98

验证

bash 复制代码
127.0.0.1:6379> set name my
OK

bash 复制代码
127.0.0.1:6379> get name
"my"

删除主从同步:

在从节点执行REPLICAOF NO ONE 或 SLAVEOF NO ONE指令可以取消主从复制

取消复制会断开和master的连接而不再有主从复制关联,但不会清除slave上已有的数据

bash 复制代码
127.0.0.1:6379> REPLICAOF NO ONE

redis集群的哨兵模式

每一台执行

bash 复制代码
[root@m1 redis-6.2.14 13:33:44]# cp sentinel.conf sentinel.conf.bak
[root@m1 redis-6.2.14 13:34:14]# vim sentinel.conf
[root@m1 redis-6.2.14 13:35:13]# vim sentinel.conf
[root@m1 redis-6.2.14 13:36:22]# redis-sentinel sentinel.conf &
[2] 53347
[root@m1 redis-6.2.14 13:36:36]# 53347:X 10 Jun 2026 13:36:36.279 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
53347:X 10 Jun 2026 13:36:36.279 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=53347, just started
53347:X 10 Jun 2026 13:36:36.279 # Configuration loaded
53347:X 10 Jun 2026 13:36:36.279 * Increased maximum number of open files to 10032 (it was originally set to 1024).
53347:X 10 Jun 2026 13:36:36.279 * monotonic clock: POSIX clock_gettime
                _._                
           _.-``__ ''-._           
      _.-``    `.  `_.  ''-._           Redis 6.2.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._ 
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 53347
  `-._    `-._  `-./  _.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-' 
      `-._    `-.__.-'    _.-'     
          `-._        _.-'         
              `-.__.-'             

53347:X 10 Jun 2026 13:36:36.281 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
53347:X 10 Jun 2026 13:36:36.282 # Sentinel ID is b037a7e5e5177b6ae8ffc2b715d67ec93990d9a1
53347:X 10 Jun 2026 13:36:36.282 # +monitor master mymaster 192.168.108.10 6379 quorum 2
53347:X 10 Jun 2026 13:36:36.283 * +slave slave 192.168.108.12:6379 192.168.108.12 6379 @ mymaster 192.168.108.10 6379
53347:X 10 Jun 2026 13:36:36.284 * +slave slave 192.168.108.11:6379 192.168.108.11 6379 @ mymaster 192.168.108.10 6379
53347:X 10 Jun 2026 13:36:38.288 * +sentinel sentinel 9dc608ba43353e5281bca84a0621df75dec21432 192.168.108.12 26379 @ mymaster 192.168.108.10 6379
53347:X 10 Jun 2026 13:36:38.366 * +sentinel sentinel 838134900149d9b4eeccad957bf429df2659528a 192.168.108.11 26379 @ mymaster 192.168.108.1[root@m1 redis-6.2.14 13:36:36]# 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.108.10:6379,slaves=2,sentinels=3
127.0.0.1:26379>

总结 :哨兵模式是 Redis 实现 自动高可用 的标准方案,适用于对可用性要求高、但无需数据分片的

业务场景。对于超大规模集群,应考虑 Redis Cluster

redis集群

每一台操作

bash 复制代码
[root@redis1 ~ 14:25:00]# yum install tcl gcc gcc-c++ -y
Loaded plugins: fastestmirror,
              : langpacks
Loading mirror speeds from cached hostfile
Package 1:tcl-8.5.13-8.el7.x86_64 already installed and latest version
Package gcc-4.8.5-44.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Nothing to do
[root@redis1 ~ 14:27:36]# cd /usr/local/redis-6.2.14/
[root@redis1 redis-6.2.14 14:27:52]# vim redis.conf
[root@redis1 redis-6.2.14 14:30:32]
[root@redis1 redis-6.2.14 14:32:34][root@redis1 redis-6.2.14 14:32:34]m redis-server redis.conf &
[1] 4012
[root@redis1 redis-6.2.14 14:34:56]# 4012:C 10 Jun 2026 14:34:56.254 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4012:C 10 Jun 2026 14:34:56.254 # Redis version=6.2.14, bits=64, commit=00000000, modified=0, pid=4012, just started
4012:C 10 Jun 2026 14:34:56.254 # Configuration loaded
4012:M 10 Jun 2026 14:34:56.256 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4012:M 10 Jun 2026 14:34:56.256 * monotonic clock: POSIX clock_gettime
4012:M 10 Jun 2026 14:34:56.259 * No cluster configuration found, I'm 8a70f84f32678e7ad08dfaab22902468cf435fdc
                _._                
           _.-``__ ''-._           
      _.-``    `.  `_.  ''-._           Redis 6.2.14 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._ 
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4012
  `-._    `-._  `-./  _.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           https://redis.io
  `-._    `-._`-.__.-'_.-'    _.-' 
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-' 
      `-._    `-.__.-'    _.-'     
          `-._        _.-'         
              `-.__.-'             

4012:M 10 Jun 2026 14:34:56.262 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4012:M 10 Jun 2026 14:34:56.262 # Server initialized
4012:M 10 Jun 2026 14:34:56.262 # 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.
4012:M 10 Jun 2026 14:34:56.263 * R[root@redis1 redis-6.2.14 14:34:56][root@redis1 redis-6.2.14 14:34:56]# netstat -anpt | grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      4012/redis-server 0
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      4012/redis-server 0
tcp6       0      0 ::1:6379                :::*                    LISTEN      4012/redis-server 0
tcp6       0      0 ::1:16379               :::*                    LISTEN      4012/redis-server 0

任意一台

bash 复制代码
[root@redis1 redis-6.2.14 14:53:40]# redis-cli --cluster create 192.168.108.21:6379 192.168.108.22:6379 192.168.108.23:6379 192.168.108.24:6379 192.168.108.25:6379 192.168.108.26:6379 --cluster-replicas 1
>>> 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.108.25:6379 to 192.168.108.21:6379
Adding replica 192.168.108.26:6379 to 192.168.108.22:6379
Adding replica 192.168.108.24:6379 to 192.168.108.23:6379
M: 8a70f84f32678e7ad08dfaab22902468cf435fdc 192.168.108.21:6379
   slots:[0-5460] (5461 slots) master
M: 267daa0bee0a879532b185d4489f72a1669489e4 192.168.108.22:6379
   slots:[5461-10922] (5462 slots) master
M: d448a4f87c7c63f446d4e2108659c8d57764016d 192.168.108.23:6379
   slots:[10923-16383] (5461 slots) master
S: 1c62d05fdc1544ab5c14865140235fa395fd157c 192.168.108.24:6379
   replicates d448a4f87c7c63f446d4e2108659c8d57764016d
S: 60491f5fa3ef93d86761303ad1bd8df39860f4fa 192.168.108.25:6379
   replicates 8a70f84f32678e7ad08dfaab22902468cf435fdc
S: 3af42e5bd300d05878ad5ef9a176e931a41a1773 192.168.108.26:6379
   replicates 267daa0bee0a879532b185d4489f72a1669489e4
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
4012:M 10 Jun 2026 14:54:51.480 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster
4012:M 10 Jun 2026 14:54:51.485 # IP address for this node updated to 192.168.108.21
Waiting for the cluster to join
.
4012:M 10 Jun 2026 14:54:53.498 * Replica 192.168.108.25:6379 asks for synchronization
4012:M 10 Jun 2026 14:54:53.498 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'beace0febcd315cf33f4a7e88d91c29a28fbb816', my replication IDs are 'e04c95ea8802307c5ef32d57344e655313604cae' and '0000000000000000000000000000000000000000')
4012:M 10 Jun 2026 14:54:53.498 * Replication backlog created, my new replication IDs are '2cde4941398d065350e921d343c4a1d44fada937' and '0000000000000000000000000000000000000000'
4012:M 10 Jun 2026 14:54:53.498 * Starting BGSAVE for SYNC with target: disk
4204:C 10 Jun 2026 14:54:53.533 * DB saved on disk
4204:C 10 Jun 2026 14:54:53.534 * RDB: 2 MB of memory used by copy-on-write
4012:M 10 Jun 2026 14:54:53.534 * Background saving started by pid 4204
>>> Performing Cluster Check (using node 192.168.108.21:6379)
M: 8a70f84f32678e7ad08dfaab22902468cf435fdc 192.168.108.21:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 3af42e5bd300d05878ad5ef9a176e931a41a1773 192.168.108.26:6379
   slots: (0 slots) slave
   replicates 267daa0bee0a879532b185d4489f72a1669489e4
M: 267daa0bee0a879532b185d4489f72a1669489e4 192.168.108.22:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1c62d05fdc1544ab5c14865140235fa395fd157c 192.168.108.24:6379
   slots: (0 slots) slave
   replicates d448a4f87c7c63f446d4e2108659c8d57764016d
M: d448a4f87c7c63f446d4e2108659c8d57764016d 192.168.108.23:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 60491f5fa3ef93d86761303ad1bd8df39860f4fa 192.168.108.25:6379
   slots: (0 slots) slave
   replicates 8a70f84f32678e7ad08dfaab22902468cf435fdc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis1 redis-6.2.14 14:54:53]# 4012:M 10 Jun 2026 14:54:53.557 * Background saving terminated with success
4012:M 10 Jun 2026 14:54:53.557 * Synchronization with replica 192.168.108.25:6379 succeeded
4012:M 10 Jun 2026 14:54:56.476 # Cluster state changed: ok

查看

bash 复制代码
[root@redis1 ~ 14:56:33]# redis-cli
127.0.0.1:6379> CLUSTER NODES
3af42e5bd300d05878ad5ef9a176e931a41a1773 192.168.108.26:6379@16379 slave 267daa0bee0a879532b185d4489f72a1669489e4 0 1781074612235 2 connected
267daa0bee0a879532b185d4489f72a1669489e4 192.168.108.22:6379@16379 master - 0 1781074610000 2 connected 5461-10922
1c62d05fdc1544ab5c14865140235fa395fd157c 192.168.108.24:6379@16379 slave d448a4f87c7c63f446d4e2108659c8d57764016d 0 1781074610224 3 connected
d448a4f87c7c63f446d4e2108659c8d57764016d 192.168.108.23:6379@16379 master - 0 1781074611229 3 connected 10923-16383
8a70f84f32678e7ad08dfaab22902468cf435fdc 192.168.108.21:6379@16379 myself,master - 0 1781074608000 1 connected 0-5460
60491f5fa3ef93d86761303ad1bd8df39860f4fa 192.168.108.25:6379@16379 slave 8a70f84f32678e7ad08dfaab22902468cf435fdc 0 1781074611000 1 connected
127.0.0.1:6379> cluster info
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:1
cluster_stats_messages_ping_sent:140
cluster_stats_messages_pong_sent:136
cluster_stats_messages_sent:276
cluster_stats_messages_ping_received:131
cluster_stats_messages_pong_received:140
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:276

测试

21

bash 复制代码
127.0.0.1:6379> set name my
(error) MOVED 5798 192.168.108.22:6379
127.0.0.1:6379> set name 'my'
(error) MOVED 5798 192.168.108.22:6379
127.0.0.1:6379> get name
(error) MOVED 5798 192.168.108.22:6379
127.0.0.1:6379> exit
[root@redis1 ~ 14:59:56]# redis-cli -c
127.0.0.1:6379> set age 18
OK

22

bash 复制代码
[root@redis2 ~ 14:59:09]# redis-cli
127.0.0.1:6379> set name 'my'
OK
127.0.0.1:6379> get name
"my"
127.0.0.1:6379> get age
(error) MOVED 741 192.168.108.21:6379

23

bash 复制代码
[root@redis3 ~ 15:00:48]# redis-cli -c
127.0.0.1:6379> get age
-> Redirected to slot [741] located at 192.168.108.21:6379
"18"

模拟故障

bash 复制代码
[root@redis1 redis-6.2.14]# init 0
# redis 2
127.0.0.1:6379> cluster info
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:7
cluster_my_epoch:2
cluster_stats_messages_ping_sent:1486
cluster_stats_messages_pong_sent:1419
cluster_stats_messages_meet_sent:1
cluster_stats_messages_fail_sent:5
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:2912
cluster_stats_messages_ping_received:1419
cluster_stats_messages_pong_received:1487
cluster_stats_messages_fail_received:1
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:2908

查看节点信息

bash 复制代码
# redis2
127.0.0.1:6379> cluster nodes
90507dea1ca425cfe6ca993f8c453b2377ded5f0 192.168.108.23:6379@16379 master - 0
1770091176000 3 connected 10923-16383
18b45e542ebbf318fd9a92cfe2adcde4c255c72b 192.168.108.25:6379@16379 master - 0
1770091177432 7 connected 0-5460
f3cb00b07a7836666bd3fd391040ff308ad35685 192.168.108.22:6379@16379 myself,master
- 0 1770091175000 2 connected 5461-10922
8f5e04a03ae0984d843dab8fb9cd0cc4c4537624 192.168.108.26:6379@16379 slave
f3cb00b07a7836666bd3fd391040ff308ad35685 0 1770091174000 2 connected
95fd02ecaf3fb34822fd56ed7ae61b05d782d892 192.168.108.24:6379@16379 slave
90507dea1ca425cfe6ca993f8c453b2377ded5f0 0 1770091176422 3 connected
fcbf04a3649f4e9e6b381861a94393c31eb67e67 192.168.108.21:6379@16379 master,fail -
1770091143695 1770091140000 1 connected

挂掉Radisson5测试

bash 复制代码
[root@redis5 redis-6.2.14]# init 0
[root@redis2 redis-6.2.14]# redis-cli -c -h 192.168.108.22 -p 6379 cluster nodes
90507dea1ca425cfe6ca993f8c453b2377ded5f0 192.168.108.23:6379@16379 master - 0
1770092343225 3 connected 10923-16383
18b45e542ebbf318fd9a92cfe2adcde4c255c72b 192.168.108.25:6379@16379 master,fail -
1770092246338 1770092243211 7 connected 0-5460
f3cb00b07a7836666bd3fd391040ff308ad35685 192.168.108.22:6379@16379 myself,master
- 0 1770092341000 2 connected 5461-10922
8f5e04a03ae0984d843dab8fb9cd0cc4c4537624 192.168.108.26:6379@16379 slave
f3cb00b07a7836666bd3fd391040ff308ad35685 0 1770092342000 2 connected
95fd02ecaf3fb34822fd56ed7ae61b05d782d892 192.168.108.24:6379@16379 slave
90507dea1ca425cfe6ca993f8c453b2377ded5f0 0 1770092345259 3 connected
fcbf04a3649f4e9e6b381861a94393c31eb67e67 192.168.108.21:6379@16379 master,fail -
1770091143695 1770091140000 1 connected
[root@redis2 redis-6.2.14]# redis-cli -c -h 192.168.108.22 -p 6379 cluster info
cluster_state:fail #集群状态fail
cluster_slots_assigned:16384
cluster_slots_ok:10923
cluster_slots_pfail:0
cluster_slots_fail:5461
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:2
cluster_stats_messages_ping_sent:2545
cluster_stats_messages_pong_sent:2417
cluster_stats_messages_meet_sent:1
cluster_stats_messages_fail_sent:5
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:4969
cluster_stats_messages_ping_received:2417
cluster_stats_messages_pong_received:2545
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:4965

恢复集群监控

把两台挂掉的虚拟机开机,设置redis服务开启

bash 复制代码
# 集群
[root@redis2 redis-6.2.14]# redis-cli -c -h 192.168.108.22 -p 6379 cluster info
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:7
cluster_my_epoch:2
cluster_stats_messages_ping_sent:3130
cluster_stats_messages_pong_sent:2952
cluster_stats_messages_meet_sent:1
cluster_stats_messages_fail_sent:5
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_sent:6089
cluster_stats_messages_ping_received:2952
cluster_stats_messages_pong_received:3130
cluster_stats_messages_fail_received:3
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:6086
[root@redis2 redis-6.2.14]# redis-cli -c -h 192.168.108.22 -p 6379 cluster nodes
90507dea1ca425cfe6ca993f8c453b2377ded5f0 192.168.108.23:6379@16379 master - 0
1770092977782 3 connected 10923-16383
18b45e542ebbf318fd9a92cfe2adcde4c255c72b 192.168.108.25:6379@16379 master - 0
1770092978816 7 connected 0-5460
f3cb00b07a7836666bd3fd391040ff308ad35685 192.168.108.22:6379@16379 myself,master
- 0 1770092977000 2 connected 5461-10922
8f5e04a03ae0984d843dab8fb9cd0cc4c4537624 192.168.108.26:6379@16379 slave
f3cb00b07a7836666bd3fd391040ff308ad35685 0 1770092977000 2 connected
95fd02ecaf3fb34822fd56ed7ae61b05d782d892 192.168.108.24:6379@16379 slave
90507dea1ca425cfe6ca993f8c453b2377ded5f0 0 1770092976765 3 connected
fcbf04a3649f4e9e6b381861a94393c31eb67e67 192.168.108.21:6379@16379 slave
18b45e542ebbf318fd9a92cfe2adcde4c255c72b 0 1770092977000 7 connected

容器部署redis

docker容器安装

bash 复制代码
[root@localhost ~]# docker run --name redis -p 6379:6379 -d -v /data/redis:/data
redis
[root@localhost ~]# docker exec redis redis-cli info server
# Server
redis_version:8.6.0
redis_git_sha1:00000000
redis_git_dirty:1
redis_build_id:a7d515010e105f80
redis_mode:standalone
os:Linux 4.18.0-553.6.1.el8.x86_64 x86_64
arch_bits:64
monotonic_clock:POSIX clock_gettime
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:14.2.0
process_id:1
process_supervised:no
run_id:a2ee2aa49069ff42d23ca8d46f82d3ac61f1d385
tcp_port:6379
server_time_usec:1770950461439156
uptime_in_seconds:109
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9342781
executable:/data/redis-server
config_file:
io_threads_active:0
listener0:name=tcp,bind=*,bind=-::*,port=6379
[root@localhost ~]#
[root@localhost ~]# docker exec redis redis-cli set name wang
OK
[root@localhost ~]# docker exec redis redis-cli set age 18
OK
[root@localhost ~]# docker exec redis redis-cli get name
wang
[root@localhost ~]# docker exec redis redis-cli get age
18
[root@localhost ~]# docker exec redis redis-cli save
OK
[root@localhost ~]# ls /data/redis/ -1
dump.rdb

e:standalone

os:Linux 4.18.0-553.6.1.el8.x86_64 x86_64

arch_bits:64

monotonic_clock:POSIX clock_gettime

multiplexing_api:epoll

atomicvar_api:c11-builtin

gcc_version:14.2.0

process_id:1

process_supervised:no

run_id:a2ee2aa49069ff42d23ca8d46f82d3ac61f1d385

tcp_port:6379

server_time_usec:1770950461439156

uptime_in_seconds:109

uptime_in_days:0

hz:10

configured_hz:10

lru_clock:9342781

executable:/data/redis-server

config_file:

io_threads_active:0

listener0:name=tcp,bind=,bind=-:: ,port=6379

root@localhost \~#

root@localhost \~# docker exec redis redis-cli set name wang

OK

root@localhost \~# docker exec redis redis-cli set age 18

OK

root@localhost \~# docker exec redis redis-cli get name

wang

root@localhost \~# docker exec redis redis-cli get age

18

root@localhost \~# docker exec redis redis-cli save

OK

root@localhost \~# ls /data/redis/ -1

dump.rdb

复制代码
[外链图片转存中...(img-oAIkoPJ0-1782203380891)]

![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=H%3A%5C%E6%8A%A5%E5%91%8A%5Cimgs%5Cimage-20260610153706603.png&pos_id=img-9XbbUKO5-1782203380892)