Redis主从复制
2021-06-21 本文已影响0人
一个小运维
一主一从结构
一主一从结构.png- 默认redis都是主服务器,所以无需配置
[root@redis1 ~]# redis-cli
127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_replid:a839f0060f5e8d9894a1fafeeea973c3c52e5e71
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
- 配置redis2(192.168.1.12)为redis1的从服务器
通过命令进行配置(临时生效)
[root@redis2 ~]# redis-cli
127.0.0.1:6379> SLAVEOF 192.168.1.11 6379
OK
127.0.0.1:6379> INFO replication
# Replication
role:slave
master_host:192.168.1.11
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7a4326738b63379fc5def4177a1ba6fff33d0a98
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
- 永久生效
[root@redis2 ~]# vim /etc/redis/6379.conf
slaveof 192.168.1.11 6379
配置带验证的主从复制
基于以上示例【一主一从结构】,配置认证
一主一从结构.png- 配置主服务器redis1的连接密码为
guodong.com
# 设置密码
[root@redis1 ~]# vim +501 /etc/redis/6379.conf
requirepass guodong.com
# 修改服务脚本
[root@redis1 ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -p $REDISPORT -a guodong.com shutdown
# 重启服务
[root@redis1 ~]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
- 配置从服务器
# 修改配置文件,设置主服务器连接密码
[root@redis2 ~]# vim +289 /etc/redis/6379.conf
masterauth guodong.com
# 重启服务
[root@redis2 ~]# service redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
- 在从服务器本机连接服务,查看复制信息
[root@redis2 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave # 角色为从服务器
master_host:192.168.1.11 # 主服务器地址
master_port:6379 # 主服务器端口
master_link_status:up # 到主服务器的连接状态
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:70
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:94e2bea7fc81fa71b1193df29b0984190c01bacc
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:70
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:70
哨兵服务
-
监视master服务器
-
发现master宕机后,将从服务器升级为主服务器
配置哨兵服务 -
配置哨兵服务
# 在redis服务器(如redis1)上,拷贝哨兵程序到哨兵服务器
[root@redis1 ~]# scp -r /usr/local/redis 192.168.1.19:/usr/local
# 在sentinel1上,将redis命令目录添加至PATH环境变量
[root@sentinel1 ~]# echo 'export PATH=$PATH:/usr/local/redis/bin' >> /etc/bashrc
[root@sentinel1 ~]# source /etc/bashrc
# 创建哨兵配置文件
[root@sentinel1 ~]# vim /etc/sentinel.conf
sentinel monitor redis1 192.168.1.11 6379 1 # 监视主服务器
bind 0.0.0.0 # 哨兵服务运行地址
sentinel auth-pass redis1 guodong.com # 连接主服务器的密码
- 启动哨兵服务
[root@sentinel1 ~]# redis-sentinel /etc/sentinel.conf
30557:X 12 May 10:12:44.026 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30557:X 12 May 10:12:44.026 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=30557, just started
30557:X 12 May 10:12:44.026 # Configuration loaded
30557:X 12 May 10:12:44.028 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.8 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 30557
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
30557:X 12 May 10:12:44.033 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
30557:X 12 May 10:12:44.049 # Sentinel ID is 40460aaa4df5543a000bf8f464c6698a712d2697
30557:X 12 May 10:12:44.049 # +monitor master redis1 192.168.1.11 6379 quorum 1
30557:X 12 May 10:12:44.051 * +slave slave 192.168.1.12:6379 192.168.1.12 6379 @ redis1 192.168.1.11 6379
- 测试配置
# 停止主服务器redis1的redis服务
[root@redis1 ~]# service redis_6379 stop
Stopping ...
Redis stopped
# 在redis2上查看redis服务状态
[root@redis2 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master # 已经变为主服务器
connected_slaves:0
master_replid:da33c07e16dc1b90d14a20004e9f5d9b3a7b9081
master_replid2:94e2bea7fc81fa71b1193df29b0984190c01bacc
master_repl_offset:10054
second_repl_offset:8936
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:10054
- 启动redis1,查看redis1信息
[root@redis1 ~]# service redis_6379 start
Starting Redis server...
[root@redis1 ~]# redis-cli -a guodong.com
127.0.0.1:6379> info replication
# Replication
role:slave # 角色为从服务器
master_host:192.168.1.12 # 主服务器地址
master_port:6379 # 主服务器端口
master_link_status:up # 到主服务器的连接状态
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:62014
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:da33c07e16dc1b90d14a20004e9f5d9b3a7b9081
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:62014
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:61430
repl_backlog_histlen:585