Windows单机实现主从复制、哨兵、集群

Redis主从

  1. 复制2份redis.windows.conf配置文件,分别命名为redis.windows6380.conf和redis.windows6381.conf
  2. 修改配置
# redis.windows6380.conf如下:
port 6380
bind 127.0.0.1
slaveof 127.0.0.1 6379 # 设置master服务器为6379,如果不写这一段,也可以启动服务后,在连上服务器后执行slaveof 127.0.0.1 6379

# redis.windows6381.conf如下:
port 6381
bind 127.0.0.1
slaveof 127.0.0.1 6379 # 设置master服务器为6379

在这里插入图片描述
3. 启动服务
在redis安装目录下执行命令
redis-server.exe redis.windows.conf
redis-server.exe redis.windows6380.conf
redis-server.exe redis.windows6381.conf
在这里插入图片描述
4. 连接主Redis服务器
redis-cli -p 6379
5. 连接从Redis服务器
redis-cli -p 6380
注意只有主节点可以修改,从节点只读
在这里插入图片描述

Redis哨兵

  1. 和上面主从先一样启动6379、6380、6381三个服务
  2. 然后创建sentinel26379.conf文件,然后复制两份sentinel26380.conf、sentinel26381.conf

sentinel26379.conf文件内容

port 26379
#下面两行注释掉了,是因为低版本redis不支持这两个命令
#sentinel myid fad25e089080be8dddadd3f20e44f888b1f8d48a
#sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000

sentinel26380.conf文件内容

port 26380
#下面两行注释掉了,是因为低版本redis不支持这两个命令
#sentinel myid fad25e089080be8dddadd3f20e44f888b1f8d48b
#sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000

sentinel26381.conf文件内容

port 26381
#下面两行注释掉了,是因为低版本redis不支持这两个命令
#sentinel myid fad25e089080be8dddadd3f20e44f888b1f8d48c
#sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000

如果报下面的错,注释掉sentinel myid即可

D:\install\redis>redis-server.exe sentinel26379.conf --sentinel
Invalid argument during startup: Could not find sentinal subcommand myid
  1. 启动哨兵服务
redis-server.exe sentinel26379.conf --sentinel
redis-server.exe sentinel26380.conf --sentinel
redis-server.exe sentinel26381.conf --sentinel
  1. 关闭6379服务,查看主从变化

6379没关闭之前,在6380服务看master是6379

127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
slave_repl_offset:36508
master_link_down_since_seconds:jd
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

关闭6379之后,可以看到master自动变成6381了

127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:836
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

这个时候再重新启动6379,6379会自动变成6381的slave

D:\install\redis> redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:20949
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

Redis集群(cluster)

  1. 安装ruby环境

双击下载的“rubyinstaller-2.7.2-1-x64.exe”安装即可,同样,为了操作方便,也是建议安装在盘符根目录下,如: C:\Ruby27-x64 ,安装会默认把ruby添加到path环境变量

  1. 安装Ruby环境下Redis的驱动
    将下载的"Ruby环境下Redis的驱动文件(redis-3.2.2.gem)"拷贝到Ruby安装根目录(C:\Ruby27-x64)下。

然后执行安装命令如下:

gem install --local C:/Ruby27-x64/redis-3.2.2.gem
  1. 将下载的“创建Redis集群的ruby脚本文件redis-trib.rb”文件拷贝到Redis安装根目录(D:\install\redis)下。
  2. 新建配置文件redis.windows-service-6380.conf
port 6380      
loglevel notice    
logfile "D:/install/redis/Logs/redis6380_log.txt"       
appendonly yes
appendfilename "appendonly.6380.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6380.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

复制5份

# redis.windows-service-6381.conf
port 6381      
loglevel notice    
logfile "D:/install/redis/Logs/redis6381_log.txt"       
appendonly yes
appendfilename "appendonly.6381.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6381.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
# redis.windows-service-6382.conf
port 6382      
loglevel notice    
logfile "D:/install/redis/Logs/redis6382_log.txt"       
appendonly yes
appendfilename "appendonly.6382.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6382.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
# redis.windows-service-6383.conf
port 6383      
loglevel notice    
logfile "D:/install/redis/Logs/redis6383_log.txt"       
appendonly yes
appendfilename "appendonly.6383.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6383.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
# redis.windows-service-6384.conf
port 6384      
loglevel notice    
logfile "D:/install/redis/Logs/redis6384_log.txt"       
appendonly yes
appendfilename "appendonly.6384.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6384.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes
# redis.windows-service-6385.conf
port 6385      
loglevel notice    
logfile "D:/install/redis/Logs/redis6385_log.txt"       
appendonly yes
appendfilename "appendonly.6385.aof"   
cluster-enabled yes                                    
cluster-config-file nodes.6385.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

配置解释如下:

 1 port 6380                                 #端口号
 2 loglevel notice                           #日志的记录级别,notice是适合生产环境的
 3 logfile "Logs/redis6380_log.txt"          #指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
 4 syslog-enabled yes                        #是否使用系统日志
 5 syslog-ident redis6380                    #在系统日志的标识名
 6 appendonly yes                            #数据的保存为aof格式
 7 appendfilename "appendonly.6380.aof"      #数据保存文件
 8 cluster-enabled yes                       #是否开启集群
 9 cluster-config-file nodes.6380.conf
10 cluster-node-timeout 15000
11 cluster-slave-validity-factor 10
12 cluster-migration-barrier 1
13 cluster-require-full-coverage yes
  1. 启动上面6个redis服务
redis-server.exe redis.windows-service-6380.conf
redis-server.exe redis.windows-service-6381.conf
redis-server.exe redis.windows-service-6382.conf
redis-server.exe redis.windows-service-6383.conf
redis-server.exe redis.windows-service-6384.conf
redis-server.exe redis.windows-service-6385.conf
  1. 执行创建集群命令

redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385

D:\install\redis>redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
>>> Creating cluster
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6383: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6385: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6380
127.0.0.1:6381
127.0.0.1:6382
Adding replica 127.0.0.1:6383 to 127.0.0.1:6380
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
M: db4712669500b212790e01e0f3761b30417b1fa9 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: af81b3ec8883758a441c277b251477777cf95331 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: 59fd4bf6813665ed781e066a343717d2a0833729 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
S: bd8addd7b86d6e5d178c66b5441e5ace8b2e67ee 127.0.0.1:6383
   replicates db4712669500b212790e01e0f3761b30417b1fa9
S: 331c145eb0527a93d2e6a4d52b781b039ddf4b0a 127.0.0.1:6384
   replicates af81b3ec8883758a441c277b251477777cf95331
S: a7e8e30b8a70b4eee28523fa56e516edfbeee385 127.0.0.1:6385
   replicates 59fd4bf6813665ed781e066a343717d2a0833729
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 127.0.0.1:6380)
M: db4712669500b212790e01e0f3761b30417b1fa9 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
M: af81b3ec8883758a441c277b251477777cf95331 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
M: 59fd4bf6813665ed781e066a343717d2a0833729 127.0.0.1:6382
   slots:10923-16383 (5461 slots) master
M: bd8addd7b86d6e5d178c66b5441e5ace8b2e67ee 127.0.0.1:6383
   slots: (0 slots) master
   replicates db4712669500b212790e01e0f3761b30417b1fa9
M: 331c145eb0527a93d2e6a4d52b781b039ddf4b0a 127.0.0.1:6384
   slots: (0 slots) master
   replicates af81b3ec8883758a441c277b251477777cf95331
M: a7e8e30b8a70b4eee28523fa56e516edfbeee385 127.0.0.1:6385
   slots: (0 slots) master
   replicates 59fd4bf6813665ed781e066a343717d2a0833729
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  1. 查看集群
D:\install\redis>redis-trib.rb check 127.0.0.1:6380
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6385: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6383: OK
*** WARNING: 127.0.0.1:6385 claims to be slave of unknown node ID 59fd4bf6813665ed781e066a343717d2a0833729.
>>> Performing Cluster Check (using node 127.0.0.1:6380)
M: db4712669500b212790e01e0f3761b30417b1fa9 127.0.0.1:6380
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: a7e8e30b8a70b4eee28523fa56e516edfbeee385 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 59fd4bf6813665ed781e066a343717d2a0833729
S: 331c145eb0527a93d2e6a4d52b781b039ddf4b0a 127.0.0.1:6384
   slots: (0 slots) slave
   replicates af81b3ec8883758a441c277b251477777cf95331
M: af81b3ec8883758a441c277b251477777cf95331 127.0.0.1:6381
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: bd8addd7b86d6e5d178c66b5441e5ace8b2e67ee 127.0.0.1:6383
   slots: (0 slots) slave
   replicates db4712669500b212790e01e0f3761b30417b1fa9
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
  1. 执行redis命令

如集群连接需要加-c,不然get key的时候会报错
(error) MOVED 6340 127.0.0.1:6381

redis-cli -c -h 127.0.0.1 -p 6380

redis目录结构

在这里插入图片描述

其他指令

  1. 查询Redis存储的所有数据:
info Keyspace
  1. 查看所有key:
keys*
  1. 删除单个key
del key

参考文章

  • https://www.cnblogs.com/yy3b2007com/p/11033009.html
  • https://www.cnblogs.com/justdoyou/p/10253668.html
  • https://blog.csdn.net/baidu_27627251/article/details/112143714
  • https://zhidao.baidu.com/question/2270980991193858188.html
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Redis可以以不同的模式运行,包括单机模式、主从模式、哨兵模式和集群模式。在单机模式下,Redis只有一个实例,所有的数据都存储在这个实例中。主从模式中,有一个主节点和多个从节点,主节点负责写入数据,从节点负责复制主节点的数据。哨兵模式是在主从模式的基础上引入了哨兵节点,哨兵节点负责监控整个Redis集群的状态,并在主节点出现故障时自动将一个从节点升级为新的主节点。集群模式是在主从模式的基础上引入了分片,将数据分散存储在多个节点上,提高了存储和访问的能力。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [redis单机主从哨兵集群模式](https://blog.csdn.net/weixin_43989347/article/details/125293740)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [一文读懂Redis的四种模式,单机主从哨兵集群(*)](https://blog.csdn.net/weixin_42408447/article/details/120995478)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

走出半生仍是少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值