redis主从+哨兵服务

主机规划

主机名IP地址角色
sentinel1192.168.124.5redis主
sentinel2192.168.124.6redis从
sentinel3192.168.124.7redis从
sentinel1192.168.124.5哨兵1
sentinel2192.168.124.6哨兵2
sentinel3192.168.124.7哨兵3

安装环境

系统版本:Red Hat Enterprise Linux Server release 6.5
数据库版本:redis-4.0.8

主从搭建

点击——>主从搭建过程参考

查看状态

[root@sentinel1 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.5:6379          0.0.0.0:*                   LISTEN      64520/redis-server 
[root@sentinel2 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.6:6379          0.0.0.0:*                   LISTEN      62781/redis-server
[root@sentinel3 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.7:6379          0.0.0.0:*                   LISTEN      62324/redis-server

哨兵模式搭建(哨兵一)

[root@sentinel1 redis-4.0.8]# vim sentinel.conf
bind 0.0.0.0		##表示本机所有接口
port 26379			##哨兵端口
sentinel monitor mymaster 192.168.124.5 6379 2	##监听地址
:wq

哨兵模式搭建(哨兵二)

[root@sentinel2 redis-4.0.8]# vim sentinel.conf
bind 0.0.0.0		##表示本机所有接口
port 26379			##哨兵端口
sentinel monitor mymaster 192.168.124.5 6379 2	##监听地址
sentinel auth-pass mymaster 123456		##密码
:wq

哨兵模式搭建(哨兵三)

[root@sentinel3 redis-4.0.8]# vim sentinel.conf
bind 0.0.0.0		##表示本机所有接口
port 26379			##哨兵端口
sentinel monitor mymaster 192.168.124.5 6379 2	##监听地址
sentinel auth-pass mymaster 123456		##密码
:wq

本文是带密码验证的主从,所以sentinel auth-pass是可选项

启动哨兵(哨兵一)

[root@sentinel1 redis-4.0.8]# redis-sentinel sentinel.conf
...
63505:X 06 May 19:44:26.698 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set t
o the lower value of 128.63505:X 06 May 19:44:26.700 # Sentinel ID is b6b70b66a513661b07746ac6b0ff5f1b72aaad62
63505:X 06 May 19:44:26.700 # +monitor master mymaster 192.168.124.5 6379 quorum 2
63505:X 06 May 19:44:26.701 * +slave slave 192.168.124.6:6379 192.168.124.6 6379 @ mymaster 192.168.124.5 6379

启动哨兵(哨兵二)

[root@sentinel2 redis-4.0.8]# redis-sentinel sentinel.conf
...
60706:X 06 May 19:44:51.026 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set t
o the lower value of 128.60706:X 06 May 19:44:51.029 # Sentinel ID is 1c31490ed84a4f6324831b664b06509729ed88f3
60706:X 06 May 19:44:51.029 # +monitor master mymaster 192.168.124.5 6379 quorum 2
60706:X 06 May 19:44:51.030 * +slave slave 192.168.124.6:6379 192.168.124.6 6379 @ mymaster 192.168.124.5 6379
60706:X 06 May 19:44:51.046 * +sentinel sentinel b6b70b66a513661b07746ac6b0ff5f1b72aaad62 192.168.124.5 26379 @ mymaster 192.168.124.5
 6379

启动哨兵(哨兵三)

[root@sentinel2 redis-4.0.8]# redis-sentinel sentinel.conf
...
60706:X 06 May 19:44:51.026 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set t
o the lower value of 128.60706:X 06 May 19:44:51.029 # Sentinel ID is 1c31490ed84a4f6324831b664b06509729ed88f3
60706:X 06 May 19:44:51.029 # +monitor master mymaster 192.168.124.5 6379 quorum 2
60706:X 06 May 19:44:51.030 * +slave slave 192.168.124.6:6379 192.168.124.6 6379 @ mymaster 192.168.124.5 6379
60706:X 06 May 19:44:51.030 * +slave slave 192.168.124.7:6379 192.168.124.7 6379 @ mymaster 192.168.124.5 6379
60706:X 06 May 19:44:51.046 * +sentinel sentinel b6b70b66a513661b07746ac6b0ff5f1b72aaad62 192.168.124.7 26379 @ mymaster 192.168.124.5
 6379

查看状态(哨兵一)

[root@sentinel1 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.5:6379          0.0.0.0:*                   LISTEN      64520/redis-server  
tcp        0      0 0.0.0.0:26379               0.0.0.0:*                   LISTEN      63505/redis-sentine

查看状态(哨兵二)

[root@sentinel2 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.6:6379          0.0.0.0:*                   LISTEN      62781/redis-server  
tcp        0      0 0.0.0.0:26379               0.0.0.0:*                   LISTEN      60706/redis-sentine

查看状态(哨兵三)

[root@sentinel3 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.7:6379          0.0.0.0:*                   LISTEN      62781/redis-server  
tcp        0      0 0.0.0.0:26379               0.0.0.0:*                   LISTEN      60321/redis-sentine

验证

[root@sentinel1 ~]# redis-cli -h 192.168.124.5 -p 6379 
192.168.124.5:6379>info replication
# Replication
role:master					##主
connected_slaves:2			##从的个数
slave0:ip=192.168.124.6,port=6379,state=online,offset=479424,lag=1
slave0:ip=192.168.124.7,port=6379,state=online,offset=479424,lag=1
master_replid:b5f57551f94e53d2eb6888d38e1967786617b1ce
master_replid2:834c0d7d8b2acf37cae31cd0c2998bb7c43e21d9
master_repl_offset:479565
second_repl_offset:225731
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:204207
repl_backlog_histlen:275359
192.168.124.5:6379> 
[root@sentinel2 ~]# redis-cli -h 192.168.124.6 -p 6379
192.168.124.6:6379> info replication
# Replication
role:slave						##从
master_host:192.168.124.5		##主的地址
master_port:6379				##端口
master_link_status:up			##状态
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:490111
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b5f57551f94e53d2eb6888d38e1967786617b1ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:490111
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:326005
repl_backlog_histlen:164107
[root@sentinel3 ~]# redis-cli -h 192.168.124.7 -p 6379
192.168.124.7:6379> info replication
# Replication
role:slave						##从
master_host:192.168.124.5		##主的地址
master_port:6379				##端口
master_link_status:up			##状态
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:490111
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b5f57551f94e53d2eb6888d38e1967786617b1ce
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:490111
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:326005
repl_backlog_histlen:164107
  • 关闭redis主
[root@sentinel1 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
##哨兵显示主挂了,把192.168.124.6提升为主
63505:X 06 May 20:47:08.342 * +slave slave 192.168.124.5:6379 192.168.124.5 6379 @ mymaster 192.168.124.6 6379
63505:X 06 May 20:47:18.344 # +sdown slave 192.168.124.5:6379 192.168.124.5 6379 @ mymaster 192.168.124.6 6379
  • 查看是否提升为主
192.168.124.6:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:2942ecd77b7bf35b1f9eee3f584ca1c3fbf62102
master_replid2:b5f57551f94e53d2eb6888d38e1967786617b1ce
master_repl_offset:530825
second_repl_offset:513037
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:326005
repl_backlog_histlen:204821
192.168.124.6:6379> 
  • 接下来启动之前挂掉的主机
[root@sentinel1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@sentinel1 ~]# netstat -lnput | grep redis
tcp        0      0 192.168.124.5:6379          0.0.0.0:*                   LISTEN      68838/redis-server  
tcp        0      0 0.0.0.0:26379               0.0.0.0:*                   LISTEN      63505/redis-sentine
  • 查看状态
##哨兵显示192.168.124.5成为192.168.124.6的从
63505:X 06 May 20:47:18.344 # +sdown slave 192.168.124.5:6379 192.168.124.5 6379 @ mymaster 192.168.124.6 6379
63505:X 06 May 20:51:36.674 # -sdown slave 192.168.124.5:6379 192.168.124.5 6379 @ mymaster 192.168.124.6 6379
  • 查看各个数据库的状态
[root@sentinel1 ~]# redis-cli -h 192.168.124.5 -p 6379
192.168.124.5:6379> info replication
# Replication
role:slave			##接替为从
master_host:192.168.124.6
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:570383
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2942ecd77b7bf35b1f9eee3f584ca1c3fbf62102
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:570383
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:551271
repl_backlog_histlen:19113
[root@sentinel2 ~]# redis-cli -h 192.168.124.6 -p 6379
192.168.124.6:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.124.5,port=6379,state=online,offset=585342,lag=0
slave0:ip=192.168.124.7,port=6379,state=online,offset=585342,lag=0
master_replid:2942ecd77b7bf35b1f9eee3f584ca1c3fbf62102
master_replid2:b5f57551f94e53d2eb6888d38e1967786617b1ce
master_repl_offset:585342
second_repl_offset:513037
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:326005
repl_backlog_histlen:259338
192.168.124.6:6379> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值