Redis-主从

主从复制过程

  • 1.从服务器开始连接主服务器时,会向主服务器发送一个SYNC同步命令
  • 2.主服务器接收到命令后,执行BGSAVE,异步的将写命令保存到一个缓冲区里
  • 3.主服务器执行完BGSAVE之后,就.rdb文件(BOF的持久化文件)发送给从服务器,从服务器从该文件恢复数据到内存中
  • 4.主服务器还会以Redis命令协议的格式,将写命令缓冲区中积累的所有内容发送给从服务器,从服务器通过执行命令将数据恢复到内存中
1.Redis安装
  • 1)下载解压,不需要编译
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
cd redis-4.0.8
make && make install
  • 2)结束之后在目录/usr/local/bin会生成如下文件
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-server
-rwxr-xr-x. 1 root root 2.4M Mar 29 10:55 redis-benchmark
-rwxr-xr-x. 1 root root 2.5M Mar 29 10:55 redis-cli
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-check-rdb
-rwxr-xr-x. 1 root root 5.5M Mar 29 10:55 redis-check-aof
lrwxrwxrwx. 1 root root   12 Mar 29 10:55 redis-sentinel -> redis-server

也可以安装到指定目录make install PREFIX=/usr/local/redis

  • 3)从源码目录拷贝单独的配置文件
mkdir -p /usr/local/redis/etc
cp /root/redis-4.0.8/*.conf /usr/local/redis/etc
  • 4)配置连接地址和密码
vi /usr/local/redis/etc/redis.conf
#bind 127.0.0.1
requirepass foobared
daemonize yes
#protected-mode yes
dir /usr/local/redis/work
#slaveof 172.16.3.140 6379
# master不需要设置slaveof,只有slave才需要
  • 5)启动脚本
cp /root/redis-4.0.8/utils/redis_init_script /etc/init.d/redis
  • 6)启动验证
/etc/init.d/redis start
[root@haproxy02 init.d]# redis-cli -h 172.16.3.141 -p 6379 -a foobared
172.16.3.141:6379> 

https://blog.csdn.net/zgf19930504/article/details/51850594

2.Redis主从复制

master: 172.16.3.140
slave01: 172.16.3.141
slave02: 172.16.3.142

1)永久主从配置
  • 在从库进行配置
vi /usr/local/redis/etc/redis.conf
#slaveof <masterip> <masterport>  # 将当前slave指向master的IP和主库端口
#masterauth <master password>     # 指定连接mster时的密码
#bind 127.0.0.1
slaveof 172.16.3.140 6379
requirepass foobared
masterauth foobared
daemonize yes
#protected-mode yes
dir /usr/local/redis/work
slave-serve-statle-data yes      # 当redis master出故障时,是否可以继续请求slave
slave-read-only yes              # 设置slave只读
repl-ping-slave-period 10        # slave每隔10秒,ping一次master  
slave-priority 100               # slave成为master的优先级,不同从库设置不同
2)临时主从配置,重启失效

需要授权

  • 在从库使用redis-cli客户端配置slaveof <masterip> <masterport>
redis-cli -h 172.16.3.141 -p 6379 -a foobared
slaveof 172.16.3.140 6379
#
[root@haproxy02 bin]# redis-cli -h 172.16.3.141 -p 6379 -a foobared
172.16.3.141:6379> slaveof 172.16.3.140 6379
OK
#
redis-cli -h 172.16.3.142 -p 6379 -a foobared
slaveof 172.16.3.140 6379
#
[root@mycat01 bin]# redis-cli -h 172.16.3.142 -p 6379 -a foobared 
172.16.3.142:6379> slaveof 172.16.3.140 6379
OK
3.查看主从配置情况
  • 1)主库info replication
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4e7f0f8fd5262d3a05167a6392644b2ea6c8473d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:56
  • 2)主库info replication
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1522295362
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:79fb7497f701f546e3951f79f73467806fa435bd
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
  • 3)主库info relication
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:126
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4e7f0f8fd5262d3a05167a6392644b2ea6c8473d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
4.确认同步
  • 1)主库set key值
172.16.3.140:6379> select 15
OK
172.16.3.140:6379[15]> set k1 v1
OK
  • 2)slave01 get key值
172.16.3.141:6379> get k1
(nil)
172.16.3.141:6379> select 15
OK
172.16.3.141:6379[15]> get k1
"v1"
  • 3)slave02 get key值
172.16.3.142:6379> select 15
OK
172.16.3.142:6379[15]> get k1
"v1"
  • 4)down掉slave02重新get key值
[root@mycat01 ~]# ps -ef|grep redis
root     12428     1  0 12:03 ?        00:00:00 /usr/local/redis/bin/redis-server *:6379
root     12434  8465  0 12:04 pts/1    00:00:00 redis-cli -h 172.16.3.142 -p 6379 -a foobared
root     12457 12436  0 12:09 pts/2    00:00:00 grep --color=auto redis
[root@mycat01 ~]# kill -9 12428
[root@mycat01 ~]# ps -ef|grep redis
root     12434  8465  0 12:04 pts/1    00:00:00 redis-cli -h 172.16.3.142 -p 6379 -a foobared
root     12460 12436  0 12:09 pts/2    00:00:00 grep --color=auto redis
[root@mycat01 ~]# /etc/init.d/redis start
Starting redis (via systemctl):
172.16.3.142:6379> select 15
OK
172.16.3.142:6379[15]> get k1
"v1"
5.人肉手工切slave为master
  • 1)当master挂了
[root@haproxy01 etc]# ps -ef|grep redis
root      7860     1  0 13:31 ?        00:00:01 /usr/local/redis/bin/redis-server *:6379
root     10123 15648  0 13:44 pts/1    00:00:00 grep --color=auto redis
[root@haproxy01 etc]# kill -9 7860
[root@haproxy01 etc]# ps -ef|grep redis
root     10156 15648  0 13:44 pts/1    00:00:00 grep --color=auto redis
  • 2)从库上的状态master_link_status:down
172.16.3.141:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1146
master_link_down_since_seconds:8
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8e08f00217d957d8365018f714737abfe271bcd0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1146
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:1132
  • 3)在从库slave01上取消slave角色slaveof no one
172.16.3.141:6379> slaveof no one
OK
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:4d3e73fa16b05139e7cb1b440c788ccd13200ca5
master_replid2:8e08f00217d957d8365018f714737abfe271bcd0
master_repl_offset:1146
second_repl_offset:1147
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:1132
  • 4)从库slave02指向先的master
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.140
master_port:6379
master_link_status:down  # 原来的master挂了
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1146
master_link_down_since_seconds:207
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8e08f00217d957d8365018f714737abfe271bcd0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1146
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:1104
172.16.3.142:6379> slaveof 172.16.3.141 6379
OK
172.16.3.142:6379> auth foobared
OK
172.16.3.142:6379> info replication
# Replication
role:slave
master_host:172.16.3.141
master_port:6379
master_link_status:up  #已经指向到新的master
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1174
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4d3e73fa16b05139e7cb1b440c788ccd13200ca5
master_replid2:8e08f00217d957d8365018f714737abfe271bcd0
master_repl_offset:1174
second_repl_offset:1147
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:1132
6.哨兵模式sentinel.conf
  • 1)当前主从模式
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.3.142,port=6379,state=online,offset=1720,lag=1
slave1:ip=172.16.3.140,port=6379,state=online,offset=1720,lag=1
  • 2)配置所有节点的sentinel
vi /usr/local/redis/etc/sentinel.conf
dir "/usr/local/redis/work"
logfile "/usr/local/redis/sentinel.log"
daemonize yes
protected-mode no
sentinel monitor mymaster 172.16.3.140 6379 2
# 上面的mymaster随意起,但是一定要放在下面这行引用的名字之前,不然会报名字找不到
sentinel auth-pass mymaster foobared
  • 3)启动sentinel监控redis-sentinel /usr/local/redis/etc/sentinel.conf
14332:X 29 Mar 14:02:37.607 # +monitor master mymaster 172.16.3.141 6379 quorum 2
14332:X 29 Mar 14:03:07.660 # +sdown master mymaster 172.16.3.141 6379  # ???
  • 4)关闭master 172.16.3.141
[root@haproxy02 init.d]# ps -ef|grep redis
root     12880     1  0 13:32 ?        00:00:04 /usr/local/redis/bin/redis-server *:6379
root     12919  5968  0 13:32 pts/2    00:00:00 redis-cli -h 172.16.3.141 -p 6379 -a foobared
root     14332 14166  0 14:02 pts/3    00:00:01 redis-sentinel *:26379 [sentinel]
root     14667 32366  0 14:09 pts/1    00:00:00 grep --color=auto redis
[root@haproxy02 init.d]# kill -9 12880
[root@haproxy02 init.d]# ps -ef|grep redis
root     12919  5968  0 13:32 pts/2    00:00:00 redis-cli -h 172.16.3.141 -p 6379 -a foobared
root     14332 14166  0 14:02 pts/3    00:00:01 redis-sentinel *:26379 [sentinel]
root     14676 32366  0 14:09 pts/1    00:00:00 grep --color=auto redis
  • 5)master已经飘到其中一个slave上了
172.16.3.141:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.3.142,port=6379,state=online,offset=6827,lag=1
  • 6)sentinel切换日志
23205:X 29 Mar 17:06:28.590 # +sdown master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.590 # +odown master mymaster 172.16.3.140 6379 #quorum 1/1
23205:X 29 Mar 17:06:28.590 # +new-epoch 98
23205:X 29 Mar 17:06:28.590 # +try-failover master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.592 # +vote-for-leader 863c1c8c627415dbc3004deb529d27df2299c2df 98
23205:X 29 Mar 17:06:28.597 # da7c4243e90de25a81ed87acd6a4e40a53c860e3 voted for 863c1c8c627415dbc3004deb529d27df2299c2df 98
23205:X 29 Mar 17:06:28.670 # +elected-leader master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.670 # +failover-state-select-slave master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.725 # +selected-slave slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.725 * +failover-state-send-slaveof-noone slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:28.792 * +failover-state-wait-promotion slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.795 # +promoted-slave slave 172.16.3.141:6379 172.16.3.141 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.795 # +failover-state-reconf-slaves master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:29.850 * +slave-reconf-sent slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.844 * +slave-reconf-inprog slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.844 * +slave-reconf-done slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.927 # +failover-end master mymaster 172.16.3.140 6379
23205:X 29 Mar 17:06:30.927 # +switch-master mymaster 172.16.3.140 6379 172.16.3.141 6379
23205:X 29 Mar 17:06:30.927 * +slave slave 172.16.3.142:6379 172.16.3.142 6379 @ mymaster 172.16.3.141 6379
23205:X 29 Mar 17:06:30.927 * +slave slave 172.16.3.140:6379 172.16.3.140 6379 @ mymaster 172.16.3.141 6379
23205:X 29 Mar 17:07:00.953 # +sdown slave 172.16.3.140:6379 172.16.3.140 6379 @ mymaster 172.16.3.141 6379
  • 7)注意
21239:X 29 Mar 16:43:12.722 # +try-failover master mymaster 172.16.3.140 6379
21239:X 29 Mar 16:43:12.724 # +vote-for-leader 863c1c8c627415dbc3004deb529d27df2299c2df 95
21239:X 29 Mar 16:43:23.438 # -failover-abort-not-elected master mymaster 172.16.3.140 6379
21239:X 29 Mar 16:43:23.497 # Next failover delay: I will not start a failover before Thu Mar 29 16:49:13 2018

当出现上面停掉master后,无法failover,我用的是第一种方法

1)如果redis实例没有配置
protected-mode yes
bind 192.168.98.136
#
则在sentinel 配置文件加上
protected-mode no 
#
即可
#
2)如果redis实例有配置
protected-mode yes
bind 192.168.98.136
#
则在sentinel 配置文件加上
protected-mode yes
bind 192.168.98.136
#
即可
redis常用命令
  • 1)指定参数运行/usr/local/redis-server /usr/local/redis/etc/redis.conf
  • 2)停止redis-server服务redis -cli shutdown
  • 3)远程关闭服务redis-cli -h xxx.xxx.xxx.xxx -p xxx -u xxx -a xxx shutdown
  • 4)命令行获取密码
172.16.3.141:6379> config get requirepass
1) "requirepass"
2) "foobared"
redis命令
  • redis-benchmark 压力测试工具
  • redis-check-aof 检查redis持久化命令文件的完整性
  • redis-check-dump 检查redis持久化数据文件的完整性
  • redis-cli 客户端命令
  • redis-sentinel sentinel集群管理工具,主要负责主从切换,ln到redis-server
  • redis-server 服务daemon启动程序

https://blog.csdn.net/zhu1289303556/article/details/50839112
https://blog.csdn.net/yingxiake/article/details/51669030
https://blog.csdn.net/xuxile/article/details/52213854
http://blog.51cto.com/ckl893/1769094
https://blog.csdn.net/yypzye/article/details/52281282
https://blog.csdn.net/huangjin0507/article/details/50737671
https://blog.csdn.net/vtopqx/article/details/49247285

posted @ 2018-03-30 14:07  Jenvid  阅读(271)  评论(0编辑  收藏  举报