redis的疑难杂症

WuYiLong原创大约 2 分钟redis哨兵

前言

关于如何部署可以查看我上一篇文章redis的主从和哨兵模式

一主二从一哨兵

测试主从是否复制

先启动,如下控制台显示

[root@localhost redis]# ps -ef | grep redis


oot      2356  2353  0 3月20 ?       00:00:00 runsv redis
root      2362  2356  0 3月20 ?       00:00:00 svlogd -tt /var/log/gitlab/redis
root      2375  2353  0 3月20 ?       00:00:00 runsv redis-exporter
root      2403  2375  0 3月20 ?       00:00:00 svlogd -tt /var/log/gitlab/redis-exporter
root     15354     1  0 17:22 ?        00:00:02 ./redis-server 0.0.0.0:6378
root     15447     1  0 17:23 ?        00:00:02 ./redis-server 0.0.0.0:6377
root     15797     1  0 17:26 ?        00:00:03 ./redis-server *:26379 [sentinel]
root     16124 25960  0 17:29 pts/2    00:00:00 ./redis-cli -h 192.168.1.114 -p 6377
root     16288     1  0 17:30 ?        00:00:02 ./redis-server 0.0.0.0:6379
  • master
192.168.1.114:6377> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.114,port=6378,state=online,offset=117961,lag=1
slave1:ip=192.168.1.114,port=6379,state=online,offset=117961,lag=1
master_replid:02a11393ed0a7e5c2222fe4ee07bf1cdae60173b
master_replid2:34e21434e9ecedfae94ccc33d59b7087e0aa665c
master_repl_offset:118104
second_repl_offset:9380
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:118062

----------------------------------------------
192.168.1.114:6377> set name zhangsan
OK
  • slave1
192.168.1.114:6379> info replication
# Replication
role:slave
master_host:192.168.1.114
master_port:6377
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:123507
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:02a11393ed0a7e5c2222fe4ee07bf1cdae60173b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:123507
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:37105
repl_backlog_histlen:86403

---------------------------------------------------
192.168.1.114:6379> get name
"zhangsan"
  • slave0
192.168.1.114:6378> info replication
# Replication
role:slave
master_host:192.168.1.114
master_port:6377
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:129182
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:02a11393ed0a7e5c2222fe4ee07bf1cdae60173b
master_replid2:34e21434e9ecedfae94ccc33d59b7087e0aa665c
master_repl_offset:129182
second_repl_offset:9380
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:129182


---------------------------
192.168.1.114:6378> get name
"zhangsan"

是否故障迁移

  • 杀死进程
[root@localhost redis]# kill -9 15447
  • 原master
192.168.1.114:6377> set d
Could not connect to Redis at 192.168.1.114:6377: Connection refused
not connected>
  • slave1 to master
192.168.1.114:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.114,port=6378,state=online,offset=174011,lag=0
master_replid:2fe8e72a0235e318faa0a376e5b76c3f39e23ae0
master_replid2:02a11393ed0a7e5c2222fe4ee07bf1cdae60173b
master_repl_offset:174011
second_repl_offset:157079
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:37105
repl_backlog_histlen:136907

是否重启的master会成为slave并启动主从功能

  • 重启6377端口
./redis-start.sh 6377

-----------------------------------
ocalhost:9121 --redis.addr=unix:///var/opt/gitlab/redis/redis.socket
root     21351     1  0 18:11 ?        00:00:00 ./redis-server 0.0.0.0:6377
root     21361 29821  0 18:11 pts/4    00:00:00 grep --color=auto redis
  • 启动6377客户端
192.168.1.114:6377> info replication
# Replication
role:slave
master_host:192.168.1.114
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:196790
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2fe8e72a0235e318faa0a376e5b76c3f39e23ae0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196790
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:187434
repl_backlog_histlen:9357
  • 查看6379端口
192.168.1.114:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.114,port=6378,state=online,offset=201164,lag=1
slave1:ip=192.168.1.114,port=6377,state=online,offset=201164,lag=1
master_replid:2fe8e72a0235e318faa0a376e5b76c3f39e23ae0
master_replid2:02a11393ed0a7e5c2222fe4ee07bf1cdae60173b
master_repl_offset:201307
second_repl_offset:157079
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:37105
repl_backlog_histlen:164203

已经成功的实现故障转移。

ps: 如果是第一次master出现故障迁移,重启时虽然会成为新master的salve,但是master_link_status是down,意思就是说,master无法同步slave,导致这个的原因是没有设置masterauth的从库密码认证. 执行下面语句即可解决:

config masterauth "123456"

一主二从三哨兵

打开sentinel.conf末尾自动生成如下,则说明部署成功

# Generated by CONFIG REWRITE
sentinel known-replica mymaster 192.168.1.114 6377
sentinel known-replica mymaster 192.168.1.114 6378
sentinel known-sentinel mymaster 192.168.1.114 26378 77f2e3c07806986f0cf1348ab55ef63cb837805b
sentinel known-sentinel mymaster 192.168.1.114 26377 77f2e3c07806986f0cf1348ab55ef63cb837805a
sentinel current-epoch 15
上次编辑于:
贡献者: wuyilong