一主多从节点环境

节点

ip地址

master

192.168.44.144

slave1

192.168.44.145

slave2

192.168.4.146

主从复制的过程

从服务器首次做的是全量同步,且同步的数据会覆盖本机的数据
第1步:slave向master发送sync命令
第2步:master启动后台存盘进程,并收集所有修改数据命令
第3步:master完成后台存盘后,传送整个数据文件到slave
第4步:slave接收数据文件,加载到内存中完成首次完全同步,后续有新数据产生时,master继续收集数据修改命令依次传给slave,完成同步

配置一主多从

所有节点关闭防火墙,selinux

systemctl disable firewalld.service
systemctl stop firewalld.service
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

所有节点安装redis

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8/
make && make install
#初始化,初始化完成后redis自动启动
./utils/install_server.sh

查看个节点redis状态

master节点
[root@redis_m redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=4932,fd=6))
slave1节点
[root@redis_s1 redis-4.0.8]#  ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=4931,fd=6))
slave2节点
[root@redis_s2 redis-4.0.8]#  ss -utnlp | grep redis
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=4977,fd=6))

运行的redis服务 必须使用本机的 物理网卡口的地址 接收连接请求 (使用默认的lo 地址无法彼此之间建立连接)

配置redis使用本机物理网卡接收请求

master节点

配置
[root@redis_m redis-4.0.8]# vim /etc/redis/6379.conf
 70 bind 127.0.0.1 192.168.44.144
重启
[root@redis_m redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis_m redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
查看
[root@redis_m redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.144:6379                  *:*                   users:(("redis-server",pid=5012,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=5012,fd=6))

slave1节点

配置
[root@redis_s1 redis-4.0.8]# vim +70 /etc/redis/6379.conf
 70 bind 127.0.0.1 192.168.44.145
重启
[root@redis_s1 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis_s1 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
查看
[root@redis_s1 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.145:6379                  *:*                   users:(("redis-server",pid=5040,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=5040,fd=6))

slave2节点

配置
[root@redis_s2 redis-4.0.8]# vim +70 /etc/redis/6379.conf
70 bind 127.0.0.1 192.168.44.146
重启
[root@redis_s2 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis_s2 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
查看
[root@redis_s2 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.146:6379                  *:*                   users:(("redis-server",pid=5076,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=5076,fd=6))

配置一主多从

redis服务器默认角色为 主 master,从服务器为0

配置前查看redis服务器主从角色

master节点
[root@redis_m redis-4.0.8]# redis-cli -h 192.168.44.144 -p 6379
192.168.44.144:6379> ping
PONG
192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:2e85b0a65e078408f17bc2228fdb7261a7ddde5e
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
192.168.44.144:6379>
slave1节点
[root@redis_s1 redis-4.0.8]# redis-cli -h 192.168.44.145 -p 6379
192.168.44.145:6379> ping
PONG
192.168.44.145:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:9388ef04c0bf4762ee6a8a8569f31737d176a240
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
192.168.44.145:6379>
slave2节点
[root@redis_s2 redis-4.0.8]# redis-cli -h 192.168.44.146 -p 6379
192.168.44.146:6379> ping
PONG
192.168.44.146:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:320f40434d3fa73ad80dca3a46aa218b0b51225f
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
192.168.44.146:6379>

配置slave1为master的从服务器

192.168.44.145:6379> slaveof 192.168.44.144 6379
OK

配置slave2为master的从服务器

192.168.44.146:6379> slaveof 192.168.44.144 6379
OK

查看slave1角色状态

192.168.44.145:6379> info replication
# Replication
role:slave
master_host:192.168.44.144
master_port:6379
master_link_status:up	#状态up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:14
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e3c1b1eea9e124a0fd496c0c3126d1d5e1c7016
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

查看slave2角色状态

192.168.44.146:6379> info replication
# Replication
role:slave
master_host:192.168.44.144
master_port:6379
master_link_status:up	#状态up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:56
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7e3c1b1eea9e124a0fd496c0c3126d1d5e1c7016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:56
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:43
repl_backlog_histlen:14

查看master角色状态

192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:2	#两个从服务器
slave0:ip=192.168.44.145,port=6379,state=online,offset=70,lag=1
slave1:ip=192.168.44.146,port=6379,state=online,offset=70,lag=1
master_replid:7e3c1b1eea9e124a0fd496c0c3126d1d5e1c7016
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

redis内使用命令配置主从当前立即生效,永久生效需把配置信息写入配置文件
永久生效的两种方法
1,redis内用命令把配置写入配置文件
2,直接修改配置文件,增加相关配置信息

配置永久生效的一主多从

redis内用命令把配置写入配置文件

slave1
192.168.44.145:6379> config rewrite
OK

命令的结果是在配置文件追加

slaveof 192.168.44.144 6379
slave2
192.168.44.146:6379> config rewrite
OK

命令的结果是在配置文件追加

slaveof 192.168.44.144 6379

直接修改配置文件,增加相关配置信息

修改配置文件的效果同上,手动的在配置文件追加

slaveof 192.168.44.144 6379

直接修改配置文件需要重启后生效

验证一主多从

master存储数据

192.168.44.144:6379> mset a 1 b 2 c 3 d 4
OK
192.168.44.144:6379> keys *
1) "c"
2) "d"
3) "b"
4) "a"
192.168.44.144:6379> mget a b c d
1) "1"
2) "2"
3) "3"
4) "4"

slave1读取存储的数据

[root@redis_s1 redis-4.0.8]# redis-cli -h 192.168.44.145 -p 6379
192.168.44.145:6379> keys *
1) "c"
2) "a"
3) "d"
4) "b"
192.168.44.145:6379> mget a b c d
1) "1"
2) "2"
3) "3"
4) "4"

slave2读取储存的数据

[root@redis_s2 redis-4.0.8]# redis-cli -h 192.168.44.146 -p 6379
192.168.44.146:6379> keys *
1) "d"
2) "a"
3) "c"
4) "b"
192.168.44.146:6379> mget a b c d
1) "1"
2) "2"
3) "3"
4) "4"

验证一主多从已永久生效

关闭slave1

[root@redis_s1 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis_s1 redis-4.0.8]# ss -utnlp | grep redis
[root@redis_s1 redis-4.0.8]#

关闭salve2

[root@redis_s2 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@redis_s2 redis-4.0.8]# ss -utnlp | grep redis
[root@redis_s2 redis-4.0.8]#

查看master状态

此时master的从服务器为0

192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:7e3c1b1eea9e124a0fd496c0c3126d1d5e1c7016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2529
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2529

开启slave1

[root@redis_s1 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis_s1 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.145:6379                  *:*                   users:(("redis-server",pid=15061,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=15061,fd=6))
[root@redis_s1 redis-4.0.8]#

开启slave2

[root@redis_s2 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis_s2 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.146:6379                  *:*                   users:(("redis-server",pid=15007,fd=7))
tcp    LISTEN     0      128    127.0.0.1:6379                  *:*                   users:(("redis-server",pid=15007,fd=6))

再次查看master状态

master的从服务器恢复为2

192.168.44.144:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.44.145,port=6379,state=online,offset=2599,lag=1
slave1:ip=192.168.44.146,port=6379,state=online,offset=2599,lag=1
master_replid:7e3c1b1eea9e124a0fd496c0c3126d1d5e1c7016
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2599
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2599