redis主从复制

1.redis支持多实例的功能,一台机器上,可以运行多个单个的redis数据库

  1. 环境准备,运行3redis数据库,达到 1 2从的配置
  2.  
  3. 主库 6379.conf
  4. port 6379
  5. daemonize yes
  6. pidfile /data/6379/redis.pid
  7. loglevel notice
  8. logfile "/data/6379/redis.log"
  9. dbfilename dump.rdb
  10. dir /data/6379
  11.  
  12. 从库 6380
  13. port 6380
  14. daemonize yes
  15. pidfile /data/6380/redis.pid
  16. loglevel notice
  17. logfile "/data/6380/redis.log"
  18. dbfilename dump.rdb
  19. dir /data/6380
  20. slaveof 127.0.0.1 6379
  21.  
  22. 从库 6381
  23. port 6381
  24. daemonize yes
  25. pidfile /data/6381/redis.pid
  26. loglevel notice
  27. logfile "/data/6381/redis.log"
  28. dbfilename dump.rdb
  29. dir /data/6381
  30. slaveof 127.0.0.1 6379
  31.  
  32. [root@mcw01 ~/msRedis]$ ls
  33. [root@mcw01 ~/msRedis]$ tree /data/
  34. /data/
  35. ├── 6379
  36. ├── 6380
  37. └── 6381
  38.  
  39. 3 directories, 0 files
  40. [root@mcw01 ~/msRedis]$ vim 6379.conf
  41. [root@mcw01 ~/msRedis]$ vim 6380.conf
  42. [root@mcw01 ~/msRedis]$ vim 6381.conf
  43. [root@mcw01 ~/msRedis]$ cat 6379.conf
  44. port 6379
  45. daemonize yes
  46. pidfile /data/6379/redis.pid
  47. loglevel notice
  48. logfile "/data/6379/redis.log"
  49. dbfilename dump.rdb
  50. dir /data/6379
  51. [root@mcw01 ~/msRedis]$ cat 6380.conf
  52. port 6380
  53. daemonize yes
  54. pidfile /data/6380/redis.pid
  55. loglevel notice
  56. logfile "/data/6380/redis.log"
  57. dbfilename dump.rdb
  58. dir /data/6380
  59. [root@mcw01 ~/msRedis]$ redis-server 6379.conf
  60. [root@mcw01 ~/msRedis]$ redis-server 6380.conf #创建三个配置文件,并启动redis服务
  61. [root@mcw01 ~/msRedis]$ redis-server 6381.conf
  62. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
  63. root 25270 1 0 11:43 ? 00:00:00 redis-server *:6379
  64. root 25275 1 0 11:43 ? 00:00:00 redis-server *:6380
  65. root 25280 1 0 11:43 ? 00:00:00 redis-server *:6381
  66. [root@mcw01 ~/msRedis]$ redis-cli
  67. 127.0.0.1:6379> keys *
  68. (empty list or set)
  69. 127.0.0.1:6379> set name1 mcw1
  70. OK
  71. 127.0.0.1:6379> keys *
  72. 1) "name1"
  73. 127.0.0.1:6379>
  74. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 #6379上创建的key,在6380和6381上都看不到,三者没有关系
  75. 127.0.0.1:6380> get name1
  76. (nil)
  77. 127.0.0.1:6380> keys *
  78. (empty list or set)
  79. 127.0.0.1:6380>
  80. [root@mcw01 ~/msRedis]$ redis-cli -p 6381
  81. 127.0.0.1:6381> keys *
  82. (empty list or set)
  83. 127.0.0.1:6381>
  84. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 get name1 #连接进入的后面加命令,像MySQL一样的免交互执行命令
  85. "mcw1"
  86. [root@mcw01 ~/msRedis]$ tree /data/ #查看目录结构
  87. /data/
  88. ├── 6379
  89.    ├── redis.log
  90.    └── redis.pid
  91. ├── 6380
  92.    ├── redis.log
  93.    └── redis.pid
  94. └── 6381
  95. ├── redis.log
  96. └── redis.pid
  97.  
  98. 3 directories, 6 files
  99. [root@mcw01 ~/msRedis]$

2.开启主从复制功能

  1. redis-cli info #查看数据库信息,查出所有
  2. redis-cli info replication #快速查看复制信息。只查info里的Replication部分
  3.  
  4. 6380 6381数据库上 ,配置主从信息,通过参数形式修改配置,临时生效,注意要写入配置文件
  5.  
  6. redis-cli -p 6380 slaveof 127.0.0.1 6379
  7. redis-cli -p 6381 slaveof 127.0.0.1 6379
  8.  
  9. 此时检查6379的复制信息,以及6380 6381的复制信息
  10.  
  11. redis-cli -p 6380 info replication
  12. redis-cli -p 6381 info replication
  13.  
  14. 主从复制是 读写分离的,master可写, slave只读
  15.  
  16. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info
  17. # Server
  18. redis_version:4.0.10
  19. redis_git_sha1:00000000
  20. redis_git_dirty:0
  21. redis_build_id:279e3e51d6e7969b
  22. redis_mode:standalone
  23. os:Linux 3.10.0-693.el7.x86_64 x86_64
  24. arch_bits:64
  25. multiplexing_api:epoll
  26. atomicvar_api:atomic-builtin
  27. gcc_version:4.8.5
  28. process_id:25270
  29. run_id:29d570833f783120fd9e884ea8d4108384abaeae
  30. tcp_port:6379
  31. uptime_in_seconds:466
  32. uptime_in_days:0
  33. hz:10
  34. lru_clock:2371478
  35. executable:/root/msRedis/redis-server
  36. config_file:/root/msRedis/6379.conf
  37.  
  38. # Clients
  39. connected_clients:1
  40. client_longest_output_list:0
  41. client_biggest_input_buf:0
  42. blocked_clients:0
  43.  
  44. # Memory
  45. used_memory:849456
  46. used_memory_human:829.55K
  47. used_memory_rss:7688192
  48. used_memory_rss_human:7.33M
  49. used_memory_peak:849456
  50. used_memory_peak_human:829.55K
  51. used_memory_peak_perc:100.12%
  52. used_memory_overhead:836206
  53. used_memory_startup:786504
  54. used_memory_dataset:13250
  55. used_memory_dataset_perc:21.05%
  56. total_system_memory:1911832576
  57. total_system_memory_human:1.78G
  58. used_memory_lua:37888
  59. used_memory_lua_human:37.00K
  60. maxmemory:0
  61. maxmemory_human:0B
  62. maxmemory_policy:noeviction
  63. mem_fragmentation_ratio:9.05
  64. mem_allocator:jemalloc-4.0.3
  65. active_defrag_running:0
  66. lazyfree_pending_objects:0
  67.  
  68. # Persistence
  69. loading:0
  70. rdb_changes_since_last_save:1
  71. rdb_bgsave_in_progress:0
  72. rdb_last_save_time:1646538180
  73. rdb_last_bgsave_status:ok
  74. rdb_last_bgsave_time_sec:-1
  75. rdb_current_bgsave_time_sec:-1
  76. rdb_last_cow_size:0
  77. aof_enabled:0
  78. aof_rewrite_in_progress:0
  79. aof_rewrite_scheduled:0
  80. aof_last_rewrite_time_sec:-1
  81. aof_current_rewrite_time_sec:-1
  82. aof_last_bgrewrite_status:ok
  83. aof_last_write_status:ok
  84. aof_last_cow_size:0
  85.  
  86. # Stats
  87. total_connections_received:3
  88. total_commands_processed:5
  89. instantaneous_ops_per_sec:0
  90. total_net_input_bytes:131
  91. total_net_output_bytes:10197
  92. instantaneous_input_kbps:0.00
  93. instantaneous_output_kbps:0.00
  94. rejected_connections:0
  95. sync_full:0
  96. sync_partial_ok:0
  97. sync_partial_err:0
  98. expired_keys:0
  99. expired_stale_perc:0.00
  100. expired_time_cap_reached_count:0
  101. evicted_keys:0
  102. keyspace_hits:1
  103. keyspace_misses:0
  104. pubsub_channels:0
  105. pubsub_patterns:0
  106. latest_fork_usec:0
  107. migrate_cached_sockets:0
  108. slave_expires_tracked_keys:0
  109. active_defrag_hits:0
  110. active_defrag_misses:0
  111. active_defrag_key_hits:0
  112. active_defrag_key_misses:0
  113.  
  114. # Replication
  115. role:master
  116. connected_slaves:0
  117. master_replid:e3784eb64085052e7107d06d1fc605ae6dbb5b59
  118. master_replid2:0000000000000000000000000000000000000000
  119. master_repl_offset:0
  120. second_repl_offset:-1
  121. repl_backlog_active:0
  122. repl_backlog_size:1048576
  123. repl_backlog_first_byte_offset:0
  124. repl_backlog_histlen:0
  125.  
  126. # CPU
  127. used_cpu_sys:0.32
  128. used_cpu_user:0.11
  129. used_cpu_sys_children:0.00
  130. used_cpu_user_children:0.00
  131.  
  132. # Cluster
  133. cluster_enabled:0
  134.  
  135. # Keyspace
  136. db0:keys=1,expires=0,avg_ttl=0
  137. [root@mcw01 ~/msRedis]$
  138. [root@mcw01 ~/msRedis]$
  139. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication #现在6379是master,但是连接的从为0
  140. # Replication
  141. role:master
  142. connected_slaves:0
  143. master_replid:e3784eb64085052e7107d06d1fc605ae6dbb5b59
  144. master_replid2:0000000000000000000000000000000000000000
  145. master_repl_offset:0
  146. second_repl_offset:-1
  147. repl_backlog_active:0
  148. repl_backlog_size:1048576
  149. repl_backlog_first_byte_offset:0
  150. repl_backlog_histlen:0
  151. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info keyspace #info加上后面这个keyspace,就是info里被注释的英文
  152. # Keyspace
  153. db0:keys=1,expires=0,avg_ttl=0
  154. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info cluster
  155. # Cluster
  156. cluster_enabled:0
  157. [root@mcw01 ~/msRedis]$
  158.  
  159. 开启主从复制功能
  160.  
  161. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 slaveof 127.0.0.1 6379 #开启6380为6379的从
  162. OK
  163. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 slaveof 127.0.0.1 6379 #开启6381为6379的从
  164. OK
  165. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication #查看主6379的信息,角色是主,连接的从有两个,从0和从1的ip端口等信息
  166. # Replication
  167. role:master
  168. connected_slaves:2
  169. slave0:ip=127.0.0.1,port=6381,state=online,offset=28,lag=0
  170. slave1:ip=127.0.0.1,port=6380,state=online,offset=28,lag=0
  171. master_replid:99b8b1b5d61e13152f4025821626574ed7f92ac9
  172. master_replid2:0000000000000000000000000000000000000000
  173. master_repl_offset:28
  174. second_repl_offset:-1
  175. repl_backlog_active:1
  176. repl_backlog_size:1048576
  177. repl_backlog_first_byte_offset:1
  178. repl_backlog_histlen:28
  179. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #查看从6380的复制信息。角色是从,主的ip端口是什么,主的连接状态,是否只读等信息
  180. # Replication
  181. role:slave
  182. master_host:127.0.0.1
  183. master_port:6379
  184. master_link_status:up
  185. master_last_io_seconds_ago:2
  186. master_sync_in_progress:0
  187. slave_repl_offset:56
  188. slave_priority:100
  189. slave_read_only:1
  190. connected_slaves:0
  191. master_replid:99b8b1b5d61e13152f4025821626574ed7f92ac9
  192. master_replid2:0000000000000000000000000000000000000000
  193. master_repl_offset:56
  194. second_repl_offset:-1
  195. repl_backlog_active:1
  196. repl_backlog_size:1048576
  197. repl_backlog_first_byte_offset:1
  198. repl_backlog_histlen:56
  199. [root@mcw01 ~/msRedis]$
  200.  
  201. 再来查看刚刚主上创建的数据,现在已经复制到从库上去了
  202. [root@mcw01 ~/msRedis]$ redis-cli -p 6379
  203. 127.0.0.1:6379> keys *
  204. 1) "name1"
  205. 127.0.0.1:6379>
  206. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 keys *
  207. (error) ERR wrong number of arguments for 'keys' command
  208. [root@mcw01 ~/msRedis]$ redis-cli -p 6380
  209. 127.0.0.1:6380> keys *
  210. 1) "name1"
  211. 127.0.0.1:6380>
  212. [root@mcw01 ~/msRedis]$ redis-cli -p 6381
  213. 127.0.0.1:6381> keys *
  214. 1) "name1"
  215. 127.0.0.1:6381>
  216. [root@mcw01 ~/msRedis]$
  217. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 #在主上创建一个新的数据,也很快同步到从上去了,说明主从复制正常运行
  218. 127.0.0.1:6379> set name2 mcw2
  219. OK
  220. 127.0.0.1:6379>
  221. [root@mcw01 ~/msRedis]$ redis-cli -p 6381
  222. 127.0.0.1:6381> keys *
  223. 1) "name1"
  224. 2) "name2"
  225. 127.0.0.1:6381>
  226. 127.0.0.1:6381> set name3 mcw3 #然后在从上写入一个数据,发现从是只读的。这也是实现了读写分离的
  227. (error) READONLY You can't write against a read only slave.
  228. 127.0.0.1:6381>
  229. [root@mcw01 ~/msRedis]$
  230.  
  231. 如果命令行设置了主从复制了,但是没有写进配置文件,需要记得把执行过的命令加进去,这样即使重启redis,也会自动加载上。
  232. slaveof 127.0.0.1 6379

3.模拟主从复制故障,手动切换master-slave身份

  1. 1.杀死6379进程 ,干掉主库
  2.  
  3. 2.手动切换 6381为新的主库,需要先关闭它的从库身份
  4. redis-cli -p 6381 slaveof no one
  5.  
  6. 3.修改6380的新主库是 6381
  7. redis-cli -p 6380 slaveof 127.0.0.1 6381
  8.  
  9. 1.杀死6379进程 ,干掉主库
  10. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
  11. root 25270 1 0 11:43 ? 00:00:02 redis-server *:6379
  12. root 25275 1 0 11:43 ? 00:00:02 redis-server *:6380
  13. root 25280 1 0 11:43 ? 00:00:02 redis-server *:6381
  14. [root@mcw01 ~/msRedis]$ kill 25270
  15. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
  16. root 25275 1 0 11:43 ? 00:00:02 redis-server *:6380
  17. root 25280 1 0 11:43 ? 00:00:02 redis-server *:6381
  18. [root@mcw01 ~/msRedis]$
  19. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #虽然主库已经杀掉了,但是现在还是连接的6379
  20. # Replication
  21. role:slave
  22. master_host:127.0.0.1
  23. master_port:6379
  24. master_link_status:down
  25. ..........
  26. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
  27. # Replication
  28. role:slave
  29. master_host:127.0.0.1
  30. master_port:6379
  31. master_link_status:down
  32. master_last_io_seconds_ago:-1
  33. master_sync_in_progress:0
  34. ........
  35. [root@mcw01 ~/msRedis]$
  36.  
  37. 2.手动切换 6381为新的主库,需要先关闭它的从库身份
  38. redis-cli -p 6381 slaveof no one
  39.  
  40. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 slaveof no one #
  41. OK
  42. [root@mcw01 ~/msRedis]$ redis-cli -p 6381
  43. 127.0.0.1:6381> keys *
  44. 1) "name1"
  45. 2) "name2"
  46. 127.0.0.1:6381> set name3 mcw3 #取消它从库身份,就可以写入数据了
  47. OK
  48. 127.0.0.1:6381>
  49. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication #查看自己的复制信息,是主了,现在还没有从
  50. # Replication
  51. role:master
  52. connected_slaves:0
  53. ........
  54. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #查看6380,现在的从不是新的主6381的从,需要手动修改
  55. # Replication
  56. role:slave
  57. master_host:127.0.0.1
  58. master_port:6379
  59. master_link_status:down
  60. ......
  61. [root@mcw01 ~/msRedis]$
  62.  
  63. 3.修改6380的新主库是 6381
  64. redis-cli -p 6380 slaveof 127.0.0.1 6381
  65.  
  66. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 slaveof 127.0.0.1 6381
  67. OK
  68. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #上面是修改新的主,下面是查看已经修改成功,连接状态up
  69. # Replication
  70. role:slave
  71. master_host:127.0.0.1
  72. master_port:6381
  73. master_link_status:up
  74. .........
  75. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 #从上面可以看到刚刚主上创建的name3了
  76. 127.0.0.1:6380> keys *
  77. 1) "name2"
  78. 2) "name3"
  79. 3) "name1"
  80. 127.0.0.1:6380>
  81. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 #主上创建一条数据name4
  82. 127.0.0.1:6381> set name4 mcw4
  83. OK
  84. 127.0.0.1:6381>
  85. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 #从上能查到新建的数据集name4.并且从不能写入数据,是只读的。
  86. 127.0.0.1:6380> keys *
  87. 1) "name2"
  88. 2) "name3"
  89. 3) "name1"
  90. 4) "name4"
  91. 127.0.0.1:6380> set name5 mcw5
  92. (error) READONLY You can't write against a read only slave.
  93. 127.0.0.1:6380>
  94. [root@mcw01 ~/msRedis]$

哨兵搭建

redis哨兵高可用

redis-sentinel功能

环境准备:redis主从复制集群部署

  1. 三个redis数据库实例 ,配置好 1 2从的配置
  2.  
  3. 6379.conf
  4. port 6379
  5. daemonize yes
  6. logfile "6379.log"
  7. dbfilename "dump-6379.rdb"
  8. dir "/var/redis/data/"
  9.  
  10. 6380.conf
  11. port 6380
  12. daemonize yes
  13. logfile "6380.log"
  14. dbfilename "dump-6380.rdb"
  15. dir "/var/redis/data/"
  16. slaveof 127.0.0.1 6379
  17.  
  18. 6381.conf
  19. port 6381
  20. daemonize yes
  21. logfile "6381.log"
  22. dbfilename "dump-6381.rdb"
  23. dir "/var/redis/data/"
  24. slaveof 127.0.0.1 6379
  25.  
  26. [root@mcw01 ~/msRedis]$ ps -ef|grep redis
  27. root 25275 1 0 11:43 ? 00:00:08 redis-server *:6380
  28. root 25280 1 0 11:43 ? 00:00:07 redis-server *:6381
  29. root 45901 25154 0 13:51 pts/0 00:00:00 grep --color=auto redis
  30. [root@mcw01 ~/msRedis]$ kill 25275
  31. [root@mcw01 ~/msRedis]$ kill 25280
  32. [root@mcw01 ~/msRedis]$
  33. [root@mcw01 ~/msRedis]$
  34. [root@mcw01 ~/msRedis]$ ps -ef|grep redis
  35. root 45903 25154 0 13:51 pts/0 00:00:00 grep --color=auto redis
  36. [root@mcw01 ~/msRedis]$
  37. [root@mcw01 ~/msRedis]$ ls
  38. 6379.conf 6380.conf 6381.conf
  39. [root@mcw01 ~/msRedis]$ vim 6380.conf
  40. [root@mcw01 ~/msRedis]$ vim 6381.conf
  41. [root@mcw01 ~/msRedis]$ cat 6379.conf
  42. port 6379
  43. daemonize yes
  44. pidfile /data/6379/redis.pid
  45. loglevel notice
  46. logfile "/data/6379/redis.log"
  47. dbfilename dump.rdb
  48. dir /data/6379
  49. [root@mcw01 ~/msRedis]$ cat 6380.conf #我这里直接写进配置文件,这样用配置文件启动redis,就不需要执行相关命令了
  50. port 6380
  51. daemonize yes
  52. pidfile /data/6380/redis.pid
  53. loglevel notice
  54. logfile "/data/6380/redis.log"
  55. dbfilename dump.rdb
  56. dir /data/6380
  57. slaveof 127.0.0.1 6379
  58. [root@mcw01 ~/msRedis]$ redis-server 6379.conf
  59. [root@mcw01 ~/msRedis]$ redis-server 6380.conf
  60. [root@mcw01 ~/msRedis]$ redis-server 6381.conf
  61. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis #启动三个服务
  62. root 45949 1 0 13:53 ? 00:00:00 redis-server *:6379
  63. root 45954 1 0 13:53 ? 00:00:00 redis-server *:6380
  64. root 45960 1 0 13:53 ? 00:00:00 redis-server *:6381
  65. [root@mcw01 ~/msRedis]$ redis-cli -p 6379 info replication #查看主从复制情况,一主两从正常
  66. # Replication
  67. role:master
  68. connected_slaves:2
  69. slave0:ip=127.0.0.1,port=6380,state=online,offset=84,lag=1
  70. slave1:ip=127.0.0.1,port=6381,state=online,offset=84,lag=0
  71. ........
  72. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication
  73. # Replication
  74. role:slave
  75. master_host:127.0.0.1
  76. master_port:6379
  77. master_link_status:up
  78. .......
  79. [root@mcw01 ~/msRedis]$

环境准备:redis三个redis哨兵进程部署

  1. 三个redis哨兵进程,指定好,检测着谁
  2. 也是准备三个配置文件,内容如下
  3.  
  4. sentinel-26379.conf
  5. port 26379
  6. dir /var/redis/data/
  7. logfile "26379.log"
  8.  
  9. // 当前Sentinel节点监控 192.168.182.130:6379 这个主节点
  10. // 2代表判断主节点失败至少需要2个Sentinel节点节点同意
  11. // mymaster是主节点的别名
  12. sentinel monitor s21ms 0.0.0.0 6379 2
  13.  
  14. //每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒30s且没有回复,则判定不可达
  15. sentinel down-after-milliseconds s21ms 20000
  16.  
  17. //当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,
  18. 原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1
  19. sentinel parallel-syncs mymaster 1
  20.  
  21. //故障转移超时时间为180000毫秒
  22. sentinel failover-timeout mymaster 180000
  23.  
  24. #三个哨兵的配置文件,一模一样,仅仅是端口的区别
  25. #三个哨兵的配置文件,一模一样,仅仅是端口的区别
  26. #三个哨兵的配置文件,一模一样,仅仅是端口的区别
  27. #三个哨兵的配置文件,一模一样,仅仅是端口的区别
  28. sentinel-26380.conf
  29.  
  30. sentinel-26381.conf
  31.  
  32. 2.分别启动 三个redis数据库, 以及三个 哨兵进程 ,注意 ,哨兵第一次启动后,会修改配置文件,如果错了,得删除配置文件,重新写
  33. 2.分别启动 三个redis数据库, 以及三个 哨兵进程 ,注意 ,哨兵第一次启动后,会修改配置文件,如果错了,得删除配置文件,重新写
  34. 2.分别启动 三个redis数据库, 以及三个 哨兵进程 ,注意 ,哨兵第一次启动后,会修改配置文件,如果错了,得删除配置文件,重新写
  35. 2.分别启动 三个redis数据库, 以及三个 哨兵进程 ,注意 ,哨兵第一次启动后,会修改配置文件,如果错了,得删除配置文件,重新写
  36.  
  37. 配置文件在这里,这里用的是127.0.0.1
  38. sentinel-26379.conf
  39. port 26379
  40. dir /var/redis/data/
  41. logfile "26379.log"
  42. sentinel monitor s21ms 127.0.0.1 6379 2
  43. sentinel down-after-milliseconds s21ms 20000
  44. sentinel parallel-syncs s21ms 1
  45. sentinel failover-timeout s21ms 180000
  46. #加一个后台运行
  47. daemonize yes
  48.  
  49. #仅仅是端口的不同
  50. sentinel-26380.conf
  51.  
  52. sentinel-26381.conf
  53.  
  54. #启动
  55. 1244 redis-sentinel sentinel-26379.conf
  56. 1245 redis-sentinel sentinel-26380.conf
  57. 1246 redis-sentinel sentinel-26381.conf
  58.  
  59. sentinel-26379.conf
  60. port 26379
  61. dir /data/sentinel
  62. logfile "26379.log"
  63. sentinel monitor mcw 127.0.0.1 6379 2
  64. sentinel down-after-milliseconds mcw 20000
  65. sentinel parallel-syncs mcw 1
  66. sentinel failover-timeout mcw 180000
  67. daemonize yes
  68.  
  69. [root@mcw01 ~/msRedis]$ ss -lntup|grep 6379
  70. tcp LISTEN 0 511 *:6379 *:* users:(("redis-server",pi
  71. tcp LISTEN 0 511 :::6379 :::* users:(("redis-server",pi
  72. [root@mcw01 ~/msRedis]$ ss -anp|grep 6379
  73. tcp LISTEN 0 511 *:6379 *:* users:(("redis-server",pi
  74. tcp ESTAB 0 0 127.0.0.1:6379 127.0.0.1:39093 users:(("redis-
  75. tcp ESTAB 0 0 127.0.0.1:6379 127.0.0.1:39091 users:(("redis-
  76. tcp ESTAB 0 0 127.0.0.1:39093 127.0.0.1:6379 users:(("redis-
  77. tcp ESTAB 0 0 127.0.0.1:39091 127.0.0.1:6379 users:(("redis-
  78. tcp LISTEN 0 511 :::6379 :::* users:(("redis-server",pi
  79. [root@mcw01 ~/msRedis]$
  80. [root@mcw01 ~/msRedis]$ ls /data/63
  81. 6379/ 6380/ 6381/
  82. [root@mcw01 ~/msRedis]$ mkdir /data/sentinel
  83. [root@mcw01 ~/msRedis]$ vim 26379.conf
  84. [root@mcw01 ~/msRedis]$ sed "s#26379#26380#g" 26379.conf >26380.conf
  85. [root@mcw01 ~/msRedis]$ sed "s#26379#26381#g" 26379.conf >26381.conf
  86. [root@mcw01 ~/msRedis]$ cat 26379.conf
  87. port 26379
  88. dir /data/sentinel
  89. logfile "26379.log"
  90. sentinel monitor mcw 127.0.0.1 6379 2
  91. sentinel down-after-milliseconds mcw 20000
  92. sentinel parallel-syncs mcw 1
  93. sentinel failover-timeout mcw 180000
  94. daemonize yes
  95. [root@mcw01 ~/msRedis]$ cat 26380.conf
  96. port 26380
  97. dir /data/sentinel
  98. logfile "26380.log"
  99. sentinel monitor mcw 127.0.0.1 6379 2
  100. sentinel down-after-milliseconds mcw 20000
  101. sentinel parallel-syncs mcw 1
  102. sentinel failover-timeout mcw 180000
  103. daemonize yes
  104. [root@mcw01 ~/msRedis]$
  105. [root@mcw01 ~/msRedis]$ ls
  106. 26379.conf 26380.conf 26381.conf 6379.conf 6380.conf 6381.conf
  107. [root@mcw01 ~/msRedis]$ mv 26379.conf sentinel-26379.conf #起个方便看的名字把
  108. [root@mcw01 ~/msRedis]$ mv 26380.conf sentinel-26380.conf
  109. [root@mcw01 ~/msRedis]$ mv 26381.conf sentinel-26381.conf
  110. [root@mcw01 ~/msRedis]$ ls
  111. 6379.conf 6380.conf 6381.conf sentinel-26379.conf sentinel-26380.conf sentinel-26381.conf
  112. [root@mcw01 ~/msRedis]$
  113.  
  114. 报错:
  115. [root@mcw01 ~/msRedis]$ redis-server sentinel-26379.conf
  116.  
  117. *** FATAL CONFIG FILE ERROR ***
  118. Reading the configuration file, at line 4
  119. >>> 'sentinel monitor mcw 127.0.0.1 6379 2'
  120. sentinel directive while not in sentinel mode
  121. [root@mcw01 ~/msRedis]$
  122.  
  123. 部署哨兵模式的时候,使用的命令不对造成的,正确的命令如下:
  124. ./redis-server.sh sentinel.conf --sentinel
  125.  
  126. 为了避免把后面的--sentinel竟然给忘了类似的事情发生,建议使用redis-sentinel sentinel.conf代替redis-server命令
  127.  
  128. [root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26379.conf
  129. [root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26380.conf
  130. [root@mcw01 ~/msRedis]$ redis-sentinel sentinel-26381.conf
  131. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis #启动了三个哨兵
  132. root 45949 1 0 13:53 ? 00:00:01 redis-server *:6379
  133. root 45954 1 0 13:53 ? 00:00:02 redis-server *:6380
  134. root 45960 1 0 13:53 ? 00:00:02 redis-server *:6381
  135. root 46061 1 1 14:27 ? 00:00:00 redis-sentinel *:26379 [sentinel]
  136. root 46066 1 0 14:28 ? 00:00:00 redis-sentinel *:26380 [sentinel]
  137. root 46071 1 0 14:28 ? 00:00:00 redis-sentinel *:26381 [sentinel]
  138. [root@mcw01 ~/msRedis]$ ls
  139. 6379.conf 6380.conf 6381.conf sentinel-26379.conf sentinel-26380.conf sentinel-26381.conf
  140. [root@mcw01 ~/msRedis]$ cat 6379.conf
  141. port 6379
  142. daemonize yes
  143. pidfile /data/6379/redis.pid
  144. loglevel notice
  145. logfile "/data/6379/redis.log"
  146. dbfilename dump.rdb
  147. dir /data/6379
  148. [root@mcw01 ~/msRedis]$ cat sentinel-26379.conf #查看配置,启动后配置被重写了,添加了部分配置
  149. port 26379
  150. dir "/data/sentinel"
  151. logfile "26379.log"
  152. sentinel myid 750ea6253069388e5897651086d50af8fc604a7f
  153. sentinel monitor mcw 127.0.0.1 6379 2
  154. sentinel down-after-milliseconds mcw 20000
  155. sentinel config-epoch mcw 0
  156. daemonize yes
  157. # Generated by CONFIG REWRITE
  158. sentinel leader-epoch mcw 0
  159. sentinel known-slave mcw 127.0.0.1 6380
  160. sentinel known-slave mcw 127.0.0.1 6381
  161. sentinel known-sentinel mcw 127.0.0.1 26380 3098a36a80a4d1f1b3f01f9a332c8abdf21ee56a
  162. sentinel known-sentinel mcw 127.0.0.1 26381 50aa98a452708c83334286a9a8e2f06cef1f9fd2
  163. sentinel current-epoch 0
  164. [root@mcw01 ~/msRedis]$

3.验证哨兵是否正常

  1. redis-cli -p 26379 info sentinel
  2.  
  3. master0:name=s21ms,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
  4. 哨兵的名字,状态,监控的地址,监控的从的数量,以及哨兵的个数。查看到哨兵的信息,就说明哨兵的部署是正常的
  5.  
  6. [root@mcw01 ~/msRedis]$ redis-cli -p 26379 info sentinel
  7. # Sentinel
  8. sentinel_masters:1
  9. sentinel_tilt:0
  10. sentinel_running_scripts:0
  11. sentinel_scripts_queue_length:0
  12. sentinel_simulate_failure_flags:0
  13. master0:name=mcw,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
  14. [root@mcw01 ~/msRedis]$

4.干掉主库 ,检查主从切换状态

  1. 1253 kill -9 12749
  2. 1254 ps -ef|grep redis
  3. 1255 redis-cli -p 6380 info replication
  4. 1256 redis-cli -p 6381 info replication
  5. 1257 redis-cli -p 6380 info replication
  6. 1258 redis-cli -p 6381 info replication
  7.  
  8. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
  9. root 45949 1 0 13:53 ? 00:00:02 redis-server *:6379
  10. root 45954 1 0 13:53 ? 00:00:03 redis-server *:6380
  11. root 45960 1 0 13:53 ? 00:00:02 redis-server *:6381
  12. root 46061 1 0 14:27 ? 00:00:02 redis-sentinel *:26379 [sentinel]
  13. root 46066 1 0 14:28 ? 00:00:01 redis-sentinel *:26380 [sentinel]
  14. root 46071 1 0 14:28 ? 00:00:01 redis-sentinel *:26381 [sentinel]
  15. [root@mcw01 ~/msRedis]$ kill 45949 #杀掉主服务
  16. [root@mcw01 ~/msRedis]$ ps -ef|grep -v grep |grep redis
  17. root 45954 1 0 13:53 ? 00:00:03 redis-server *:6380
  18. root 45960 1 0 13:53 ? 00:00:03 redis-server *:6381
  19. root 46061 1 0 14:27 ? 00:00:02 redis-sentinel *:26379 [sentinel]
  20. root 46066 1 0 14:28 ? 00:00:02 redis-sentinel *:26380 [sentinel]
  21. root 46071 1 0 14:28 ? 00:00:02 redis-sentinel *:26381 [sentinel]
  22. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #迅速查看80和81,发现主从复制链接down了,但是还是指向79的
  23. # Replication
  24. role:slave
  25. master_host:127.0.0.1
  26. master_port:6379
  27. master_link_status:down
  28. .......
  29. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
  30. # Replication
  31. role:slave
  32. master_host:127.0.0.1
  33. master_port:6379
  34. master_link_status:down
  35. ........
  36. [root@mcw01 ~/msRedis]$
  37. [root@mcw01 ~/msRedis]$ redis-cli -p 6380 info replication #过一会再看,大约20秒。发现81已经自动成为主了,80已经成为81的从了,实现了自动故障转移
  38. # Replication
  39. role:slave
  40. master_host:127.0.0.1
  41. master_port:6381
  42. master_link_status:up
  43. .......
  44. [root@mcw01 ~/msRedis]$ redis-cli -p 6381 info replication
  45. # Replication
  46. role:master
  47. connected_slaves:1
  48. slave0:ip=127.0.0.1,port=6380,state=online,offset=166686,lag=0
  49. ........
  50. [root@mcw01 ~/msRedis]$

redis-cluster搭建

环境准备

  1. 1.准备好6redis节点的配置文件
  2.  
  3. redis-7000.conf
  4. port 7000
  5. daemonize yes
  6. dir "/opt/redis/data"
  7. logfile "7000.log"
  8. dbfilename "dump-7000.rdb
  9.  
  10. cluster-enabled yes #开启集群模式
  11. cluster-config-file nodes-7000.conf  #集群内部的配置文件
  12. cluster-require-full-coverage no  
  13. #redis cluster需要16384个slot都正常的时候才能对外提供服务,换句话说,只要任何一个slot异常那么整个cluster不对外提供服务。 因此生产环境一般为no
  14.  
  15. #6个配置文件,仅仅是端口的区别
  16. redis-7000.conf
  17. port 7000
  18. daemonize yes
  19. dir "/opt/redis/data"
  20. logfile "7000.log"
  21. dbfilename "dump-7000.rdb"
  22. cluster-enabled yes
  23. cluster-config-file nodes-7000.conf
  24. cluster-require-full-coverage no
  25.  
  26. redis-7001.conf
  27. redis-7002.conf
  28. redis-7003.conf
  29. redis-7004.conf
  30. redis-7005.conf
  31.  
  32. [root@mcw01 ~/msRedis]$ cd cluster/
  33. [root@mcw01 ~/msRedis/cluster]$ ls
  34. 7000.conf 7001.conf 7002.conf 7003.conf 7004.conf
  35. [root@mcw01 ~/msRedis/cluster]$ cat 7000.conf
  36. port 7000
  37. daemonize yes
  38. dir "/opt/redis/data"
  39. logfile "7000.log"
  40. dbfilename "dump-7000.rdb
  41. cluster-enabled yes
  42. cluster-config-file nodes-7000.conf
  43. cluster-require-full-coverage no
  44. [root@mcw01 ~/msRedis/cluster]$ sed "s#7000#7005#g" 7000.conf >7005.conf
  45. [root@mcw01 ~/msRedis/cluster]$ ls
  46. 7000.conf 7001.conf 7002.conf 7003.conf 7004.conf 7005.conf
  47. [root@mcw01 ~/msRedis/cluster]$ cat 7005.conf
  48. port 7005
  49. daemonize yes
  50. dir "/opt/redis/data"
  51. logfile "7005.log"
  52. dbfilename "dump-7005.rdb
  53. cluster-enabled yes
  54. cluster-config-file nodes-7005.conf
  55. cluster-require-full-coverage no
  56. [root@mcw01 ~/msRedis/cluster]$

2.根据配置文件启动6个节点

  1. 1288 redis-server 7000.conf
  2. 1290 redis-server 7001.conf
  3. 1291 redis-server 7002.conf
  4. 1292 redis-server 7003.conf
  5. 1293 redis-server 7004.conf
  6. 1294 redis-server 7005.conf
  7.  
  8. 如下,6个集群的结点 7000-7005已经起来了,并且显示是cluster
  9. [root@mcw01 ~/msRedis/cluster]$ ls
  10. 7000.conf 7001.conf 7002.conf 7003.conf 7004.conf 7005.conf
  11. [root@mcw01 ~/msRedis/cluster]$ mkdir /opt/redis/data
  12. mkdir: cannot create directory ‘/opt/redis/data’: No such file or directory
  13. [root@mcw01 ~/msRedis/cluster]$ mkdir -p /opt/redis/data
  14. [root@mcw01 ~/msRedis/cluster]$ ps -ef|grep -v grep|grep redis
  15. root 45954 1 0 13:53 ? 00:00:06 redis-server *:6380
  16. root 45960 1 0 13:53 ? 00:00:06 redis-server *:6381
  17. root 46061 1 0 14:27 ? 00:00:07 redis-sentinel *:26379 [sentinel]
  18. root 46066 1 0 14:28 ? 00:00:07 redis-sentinel *:26380 [sentinel]
  19. root 46071 1 0 14:28 ? 00:00:07 redis-sentinel *:26381 [sentinel]
  20. [root@mcw01 ~/msRedis/cluster]$
  21. [root@mcw01 ~/msRedis/cluster]$ redis-se
  22. redis-sentinel redis-server
  23. [root@mcw01 ~/msRedis/cluster]$ redis-server 7000.conf
  24. [root@mcw01 ~/msRedis/cluster]$ redis-server 7001.conf
  25. [root@mcw01 ~/msRedis/cluster]$ redis-server 7002.conf
  26. [root@mcw01 ~/msRedis/cluster]$ redis-server 7003.conf
  27. [root@mcw01 ~/msRedis/cluster]$ redis-server 7004.conf
  28. [root@mcw01 ~/msRedis/cluster]$ redis-server 7005.conf
  29. [root@mcw01 ~/msRedis/cluster]$ ps -ef|grep -v grep|grep redis
  30. root 45954 1 0 13:53 ? 00:00:06 redis-server *:6380
  31. root 45960 1 0 13:53 ? 00:00:06 redis-server *:6381
  32. root 46061 1 0 14:27 ? 00:00:08 redis-sentinel *:26379 [sentinel]
  33. root 46066 1 0 14:28 ? 00:00:07 redis-sentinel *:26380 [sentinel]
  34. root 46071 1 0 14:28 ? 00:00:07 redis-sentinel *:26381 [sentinel]
  35. root 46293 1 0 15:17 ? 00:00:00 redis-server *:7000 [cluster]
  36. root 46298 1 0 15:17 ? 00:00:00 redis-server *:7001 [cluster]
  37. root 46303 1 0 15:18 ? 00:00:00 redis-server *:7002 [cluster]
  38. root 46308 1 0 15:18 ? 00:00:00 redis-server *:7003 [cluster]
  39. root 46313 1 0 15:18 ? 00:00:00 redis-server *:7004 [cluster]
  40. root 46318 1 1 15:18 ? 00:00:00 redis-server *:7005 [cluster]
  41. [root@mcw01 ~/msRedis/cluster]$

3.分配redis slot 槽位

  1. - 手动写c语言 分配
  2. - 使用ruby大神 写的一个redis模块,自动分配
  3. 现在登录上去,添加一个数据,显示槽位没有被提供。意思是,7000这个节点没有分配槽位,而数据是放到槽位里的。
  4. [root@mcw01 ~/msRedis/cluster]$ redis-cli -p 7000
  5. 127.0.0.1:7000> keys *
  6. (empty list or set)
  7. 127.0.0.1:7000> set name1 mcw1
  8. (error) CLUSTERDOWN Hash slot not served
  9. 127.0.0.1:7000>
  10. [root@mcw01 ~/msRedis/cluster]$

4.配置ruby脚本环境

  1. 1.yum安装最简单
  2. yum install ruby
  3.  
  4. 2.自动配置好 PATH环境变量
  5. rubygem的环境变量
  6.  
  7. 3.下载ruby操作redis的模块
  8. wget http://rubygems.org/downloads/redis-3.3.0.gem
  9.  
  10. 4.ruby的包管理工具 gem 安装这个模块
  11.  
  12. gem install -l redis-3.3.0.gem
  13.  
  14. rubypython一样的解释型语言,gem相当于pip
  15.  
  16. [root@mcw01 ~/msRedis/cluster]$ wget http://rubygems.org/downloads/redis-3.3.0.gem
  17. --2022-03-06 16:10:28-- http://rubygems.org/downloads/redis-3.3.0.gem
  18. Resolving rubygems.org (rubygems.org)... 151.101.193.227, 151.101.1.227, 151.101.65.227, ...
  19. Connecting to rubygems.org (rubygems.org)|151.101.193.227|:80... failed: Connection timed out.
  20. Connecting to rubygems.org (rubygems.org)|151.101.1.227|:80... connected.
  21. HTTP request sent, awaiting response... 200 OK
  22. Length: 92160 (90K) [application/octet-stream]
  23. Saving to: redis-3.3.0.gem
  24.  
  25. 100%[===============================================================>] 92,160 --.-K/s in 0.08s
  26.  
  27. 2022-03-06 16:10:31 (1.12 MB/s) - redis-3.3.0.gem saved [92160/92160]
  28.  
  29. [root@mcw01 ~/msRedis/cluster]$ ls
  30. 7000.conf 7001.conf 7002.conf 7003.conf 7004.conf 7005.conf redis-3.3.0.gem
  31. [root@mcw01 ~/msRedis/cluster]$ gem install -l redis-3.3.0.gem
  32. Successfully installed redis-3.3.0
  33. Parsing documentation for redis-3.3.0
  34. Installing ri documentation for redis-3.3.0
  35. 1 gem installed

5.通过ruby一键分配redis-cluster集群的槽位

  1. 找到机器上的redis-trib.rb命令,用绝对命令创建
  2. 开启集群,分配槽位
  3. /opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  4.  
  5. 如下已经分配好槽位了
  6. [root@mcw01 ~/msRedis/cluster]$ /opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
  7. >>> Creating cluster
  8. >>> Performing hash slots allocation on 6 nodes...
  9. Using 3 masters: #三个主,三个复制的从
  10. 127.0.0.1:7000
  11. 127.0.0.1:7001
  12. 127.0.0.1:7002
  13. Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
  14. Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
  15. Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
  16. >>> Trying to optimize slaves allocation for anti-affinity
  17. [WARNING] Some slaves are in the same host as their master
  18. M: 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb 127.0.0.1:7000 #三个主上分别分配了槽位区间
  19. slots:0-5460 (5461 slots) master
  20. M: 102d887feee9b5d2db0284ecff83d893f4736aef 127.0.0.1:7001
  21. slots:5461-10922 (5462 slots) master
  22. M: c2ba927b42526110a7f840643a1c653057c6b811 127.0.0.1:7002
  23. slots:10923-16383 (5461 slots) master
  24. S: 22bcee4dc0439600aae268b9de093db39d73e2cf 127.0.0.1:7003
  25. replicates c2ba927b42526110a7f840643a1c653057c6b811
  26. S: fb7ed8949b7ab36351aabe72a9cb1ae8f859d6d8 127.0.0.1:7004
  27. replicates 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb
  28. S: cc0da6e5df726d65df53849dd92e2edd6ef66760 127.0.0.1:7005
  29. replicates 102d887feee9b5d2db0284ecff83d893f4736aef
  30. Can I set the above configuration? (type 'yes' to accept): yes #接受分配
  31. >>> Nodes configuration updated
  32. >>> Assign a different config epoch to each node
  33. >>> Sending CLUSTER MEET messages to join the cluster
  34. Waiting for the cluster to join.....
  35. >>> Performing Cluster Check (using node 127.0.0.1:7000)
  36. M: 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb 127.0.0.1:7000
  37. slots:0-5460 (5461 slots) master
  38. 1 additional replica(s)
  39. M: 102d887feee9b5d2db0284ecff83d893f4736aef 127.0.0.1:7001
  40. slots:5461-10922 (5462 slots) master
  41. 1 additional replica(s)
  42. S: 22bcee4dc0439600aae268b9de093db39d73e2cf 127.0.0.1:7003
  43. slots: (0 slots) slave
  44. replicates c2ba927b42526110a7f840643a1c653057c6b811
  45. S: fb7ed8949b7ab36351aabe72a9cb1ae8f859d6d8 127.0.0.1:7004
  46. slots: (0 slots) slave
  47. replicates 2d2654b26e43f6245b41dc2cae44f7bb6ec846bb
  48. M: c2ba927b42526110a7f840643a1c653057c6b811 127.0.0.1:7002
  49. slots:10923-16383 (5461 slots) master
  50. 1 additional replica(s)
  51. S: cc0da6e5df726d65df53849dd92e2edd6ef66760 127.0.0.1:7005
  52. slots: (0 slots) slave
  53. replicates 102d887feee9b5d2db0284ecff83d893f4736aef
  54. [OK] All nodes agree about slots configuration.
  55. >>> Check for open slots...
  56. >>> Check slots coverage...
  57. [OK] All 16384 slots covered. #分配槽位成功
  58. [root@mcw01 ~/msRedis/cluster]$

6.分配好集群后,可以向集群内写入数据了

  1. redis-cli -c 指定集群模式,登录数据库
  2.  
  3. 登录redis写入数据,发现槽位分配,且重定向之后,集群搭建成功
  4.  
  5. 集群模式登录redis
  6. [root@mcw01 ~/msRedis/cluster]$ redis-cli -c -p 7000
  7. 127.0.0.1:7000> keys *
  8. (empty list or set)
  9. 127.0.0.1:7000> set name1 mcw1 #7000上设置数据,分配的槽位是12933,它在10923-16383之间,也就是7002服务上
  10. -> Redirected to slot [12933] located at 127.0.0.1:7002
  11. OK
  12. 127.0.0.1:7002> keys * #添加之后,自动切换到7002服务了
  13. 1) "name1"
  14. 127.0.0.1:7002> get name1
  15. "mcw1"
  16. 127.0.0.1:7002>
  17. [root@mcw01 ~/msRedis/cluster]$ redis-cli -c -p 7000 #退出重新登录7000服务
  18. 127.0.0.1:7000> keys * #key不在7000上
  19. (empty list or set)
  20. 127.0.0.1:7000> get name1 #7000上能get到其它节点上的数据,数据存储在其它节点上的槽位,
  21. -> Redirected to slot [12933] located at 127.0.0.1:7002
  22. "mcw1"
  23. 127.0.0.1:7002> keys * #get到之后,命令行切换到这个数据所在节点上
  24. 1) "name1"
  25. 127.0.0.1:7002>
  26. [root@mcw01 ~/msRedis/cluster]$

redis主从复制,哨兵以及集群搭建部署的更多相关文章

  1. Redis主从哨兵和集群搭建

    主从配置 哨兵配置 集群配置 1.主从: 国王和丞相,国王权力大(读写),丞相权利小(读) 2.哨兵: 国王和王子,国王死了(主服务挂掉),王子继位(从服务变主服务) 3.集群: 国王和国王,一个国王 ...

  2. redis sentinel哨兵模式集群搭建教程

    1.环境说明 我们将使用192.168.220.128.192.168.220.129两台机器搭建sentinel交叉主从为例 当前我们已在192.168.220.128上按redis安装教程安装了r ...

  3. Redis单机安装以及集群搭建

    今天主要来看一下Redis的安装以及集群搭建(我也是第一次搭建). 环境:CentOS 7.1,redis-5.0.7 一.单机安装 1.将Redis安装包放置服务器并解压 2.进入redis安装目录 ...

  4. Redis 3.0.2集群搭建以及相关问题汇总

    Redis3 正式支持了 cluster,是为了解决构建redis集群时的诸多不便 (1)像操作单个redis一样操作key,不用操心key在哪个节点上(2)在线动态添加.删除redis节点,不用停止 ...

  5. 转:Redis 3.2.1集群搭建

    Redis 3.2.1集群搭建   一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2) ...

  6. Redis 5.0.5集群搭建

    Redis 5.0.5集群搭建 一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2):s ...

  7. 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  8. 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  9. 分布式缓存技术redis系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

随机推荐

  1. 扩容新生代为什么能够提高GC的效率

    扩容新生代为什么能够提高GC的效率 该文章默认读者对JVM的基础有所了解 在学习JVM的时候,遇到了个人感觉比较有意思的问题,通过视频学习整理了一下. 先来上图: 大部分情况下,对象都会进入Eden区 ...

  2. yum搭建私有仓库远程版

    目录 一:yum安装 1.简介 1.安装 2.卸载 3.更新 4.yum安装的生命周期 二:yum搭建私有仓库(本地版) 1.下载必须的软件包(准备配置) 2.创建软件仓库 3.下载对应的软件 4.初 ...

  3. Nginx搭建游戏

    目录 一:Nginx搭建<小游戏> 1.上传<象棋游戏>代码 2.编辑配置文件(尾部必须要加 .conf<文件>) 3.测试配置文件是否正常 4.重启Nginx 5 ...

  4. Protobuf 动态加载 .pb 文件并操作 Message

    之前写了<Protobuf 动态加载 .proto 文件并操作 Message>.除了直接读取 .proto 文件之外,还有一种类似的方法.先把 .proto 文件编译成 .pb 文件,再 ...

  5. 被mybatis一级缓存坑了

    目录 背景 场景 原因 解法 参考 背景 项目中出现了这样一个问题,就是select出来的数据和数据库里的数据不一样,就非常的奇怪,发现原来是mybatis的缓存导致的,经过查询资料发现这是mybat ...

  6. python 小兵(2)

    while 条件: 结构体 if=条件: 等于 while 条件: 结构体 else: print(int(Ture))    1 print(int(False))   0 切片顾头不顾尾 prin ...

  7. Ubuntu 18.04 安装教程

    准备材料 Ubuntu安装U盘 足够的硬盘空间 未初始化的硬盘需要提前初始化 注意事项 Ubuntu安装盘的制作请参考我的另外一个博客,里面写清楚了怎么制作Ubuntu安装盘,步骤非常简单 请将要拿给 ...

  8. Luogu_P1613跑路

    跳转链接 题目大意 题目中要求的是从1号点到n号点所需要的最短时间, 一秒可以走 \(2^k\) 个距离 给定的有向图的边边权都是1. 问题分析 由于一秒可以走 \(2^k\) 个距离,因此题目转化为 ...

  9. Codeforces Round #740 Div. 2

    题目跳转链接 A. Simply Strange Sort 题意 定义一个函数\(f_{i}\) : 如果\(a_i \ge a_{i+1}\) swap(\(a_i\) \(a_{i+1}\)) 定 ...

  10. git init和git init –bare的区别:

    感谢原文作者:ljchlx 原文链接:https://blog.csdn.net/ljchlx/article/details/21805231 git init 和 git init –bare 的 ...