Redis主从
- 复制2份redis.windows.conf配置文件,分别命名为redis.windows6380.conf和redis.windows6381.conf
- 修改配置
port 6380
bind 127.0.0.1
slaveof 127.0.0.1 6379
port 6381
bind 127.0.0.1
slaveof 127.0.0.1 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哨兵
- 和上面主从先一样启动6379、6380、6381三个服务
- 然后创建sentinel26379.conf文件,然后复制两份sentinel26380.conf、sentinel26381.conf
sentinel26379.conf文件内容
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000
sentinel26380.conf文件内容
port 26380
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 15000
sentinel26381.conf文件内容
port 26381
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
- 启动哨兵服务
redis-server.exe sentinel26379.conf --sentinel
redis-server.exe sentinel26380.conf --sentinel
redis-server.exe sentinel26381.conf --sentinel
- 关闭6379服务,查看主从变化
6379没关闭之前,在6380服务看master是6379
127.0.0.1:6380> info 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
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
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)
- 安装ruby环境
双击下载的“rubyinstaller-2.7.2-1-x64.exe”安装即可,同样,为了操作方便,也是建议安装在盘符根目录下,如: C:\Ruby27-x64 ,安装会默认把ruby添加到path环境变量
- 安装Ruby环境下Redis的驱动
将下载的"Ruby环境下Redis的驱动文件(redis-3.2.2.gem)"拷贝到Ruby安装根目录(C:\Ruby27-x64)下。
然后执行安装命令如下:
gem install --local C:/Ruby27-x64/redis-3.2.2.gem
- 将下载的“创建Redis集群的ruby脚本文件redis-trib.rb”文件拷贝到Redis安装根目录(D:\install\redis)下。
- 新建配置文件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份
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
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
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
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
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
3 logfile "Logs/redis6380_log.txt"
4 syslog-enabled yes
5 syslog-ident redis6380
6 appendonly yes
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
- 启动上面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
- 执行创建集群命令
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.
- 查看集群
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.
- 执行redis命令
如集群连接需要加-c,不然get key的时候会报错 (error) MOVED 6340 127.0.0.1:6381
redis-cli -c -h 127.0.0.1 -p 6380
redis目录结构
其他指令
- 查询Redis存储的所有数据:
info Keyspace
- 查看所有key:
keys*
- 删除单个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
|