导读:一、redis主从复制.主从复制:是存储数据的服务结构.主服务器:接受客户端连接的服务器.从服务器:自动与主服务器保持数据一致的服务器.配置主从复制.1、环境准备.主服务器 主机名:master IP地址:192.168.11.101/24.从服务器 主机名:
主从复制:是存储数据的服务结构
主服务器:接受客户端连接的服务器
从服务器:自动与主服务器保持数据一致的服务器
主服务器 主机名:master IP地址:192.168.11.101/24
从服务器 主机名:node01 IP地址:192.168.11.102/24
客户端 主机名:node02 IP地址:192.168.11.103/24
主从服务器都要安装redis服务,分别修改redis监听的地址为192.168.11.101,192.168.11.102
root@Master:~# ss -lntup | grep 6379
tcp LISTEN 0 128 127.0.0.1:6379 0.0.0.0:* users:(("redis-server",pid=1147,fd=6))
root@Master:~# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
root@Master:~# ss -lntup | grep 6379
root@Master:~# vim /etc/redis/6379.conf
bind 192.168.11.101
root@Master:~# /etc/init.d/redis_6379 start
Starting Redis server...
root@Master:~# redis-cli -h 192.168.11.101 -p 6379
192.168.11.101:6379> ping
PONG
192.168.11.101:6379> INFO replication
# Replication
role:master # 主节点
connected_slaves:0
master_replid:88c2e017cb0ea8cbd01a4f3874b314f8de1775b2
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.11.101:6379>
修改监听的IP地址
# 先停服务器
root@node01:~# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
# 修改配置文件
root@node01:~# vim /etc/redis/6379.conf
# 修改客户端连接redis服务的IP地址
bind 192.168.11.102
root@node01:~# /etc/init.d/redis_6379 start
Starting Redis server...
root@node01:~# ss -lntup |grep 6379
tcp LISTEN 0 128 192.168.11.102:6379 0.0.0.0:* users:(("redis-server",pid=7978,fd=6))
PS:在这还没添加从服务器信息,所以看到是master是正常的!!!
root@node01:~# redis-cli -h 192.168.11.102 -p 6379
192.168.11.102:6379> ping
PONG
192.168.11.102:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_replid:f151ec960f14af322c6b07946a074a30a4c2c731
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.11.102:6379> SLAVEOF 192.168.11.101 6379
OK
192.168.11.102:6379> INFO replication
# Replication
role:slave
master_host:192.168.11.101
master_port:6379
master_link_status:down
root@Master:~# systemctl is-active firewalld.service
active
root@Master:~# systemctl stop firewalld.service
root@Master:~# systemctl disable firewalld.service
root@Master:~# setenforce 0
# 第一次做忘了这条关闭连接命令 苦
root@node01:~# redis-cli -h 192.168.11.102 -p 6379 shutdown
root@node01:~#
root@node01:~# /etc/init.d/redis_6379 restart
/var/run/redis_6379.pid does not exist, process is not running
Starting Redis server...
root@node01:~# redis-cli -h 192.168.11.102 -p 6379 shutdown
root@node01:~# vim /etc/redis/6379.conf
slaveof 192.168.11.101 6379 #修改,指定主服务器的IP地址和端口号
root@node01:~# /etc/init.d/redis_6379 start
root@node01:~# redis-cli -h 192.168.11.102 -p 6379
192.168.11.102:6379> info replication #查看slave服务器的主从信息
# Replication
role:slave #从服务器
master_host:192.168.11.101 #主服务器IP地址
master_port:6379 #主服务器端口号
master_link_status:up #和主服务器的连接状态:up
192.168.11.101:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.11.102,port=6379,state=online,offset=1330,lag=0
master_replid:ecc43c0169695c02204b624365e75859defc0aec
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1330
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1330
# 添加数据
192.168.11.101:6379> set school qinghua
OK
# 查看数据
192.168.11.101:6379> GET school
"qinghua"
192.168.11.102:6379> KEYS *
1) "school"
192.168.11.102:6379> GET school
"qinghua"
root@node01:~# redis-cli -h 192.168.11.101 -p 6379
192.168.11.101:6379> ping
PONG
192.168.11.101:6379> SLAVEOF no one
OK
192.168.11.101:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.11.102,port=6379,state=online,offset=1853,lag=0
192.168.11.101:6379> exit
主服务器 主机名:master IP地址:192.168.11.101/24
从服务器 主机名:node01 IP地址:192.168.11.102/24
从服务器 主机名:node02 IP地址:192.168.11.103/24
192.168.11.101 192.168.11.102 192.168.11.103
# 停止服务
root@node02:~# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
# 修改配置文件
root@node02:~# vim /etc/redis/6379.conf
bind 192.168.11.103
root@node02:~# /etc/init.d/redis_6379 start
Starting Redis server...
root@node02:~# ss -lntup |grep redis
tcp LISTEN 0 128 192.168.11.103:6379 0.0.0.0:* users:(("redis-server",pid=9860,fd=6))
root@Master:~# redis-cli -h 192.168.11.101 -p 6379 shutdown
501 requirepass plj123
root@Master:~# /etc/init.d/redis_6379 start
root@Master:~# redis-cli -h 192.168.11.101 -p 6379 -a plj123
192.168.4.51:6379> info replication
# Replication
role:master
192.168.11.101:6379> set a 1
192.168.11.101:6379> set b 1
192.168.11.101:6379> set c 1
192.168.11.103:6379>
192.168.11.103:6379> SLAVEOF 192.168.11.101 6379
root@node01:~# redis-cli -h 192.168.11.102 -p 6379 shutdown
root@node01:~# vim /etc/redis/6379.conf
289 masterauth plj123
root@node01:~# /etc/init.d/redis_6379 start
192.168.11.102:6379> info replication
# Replication
role:slave
master_host:192.168.11.101
master_port:6379
master_link_status:up
192.168.11.103:6379> INFO replication
# Replication
role:slave
master_host:192.168.11.101
master_port:6379
master_link_status:up
192.168.11.103:6379> KEYS *
1) "school" # 数据已经同步
root@node01:~# redis-cli -h 192.168.11.102 -p 6379 shutdow
192.168.11.101:6379> set AAA CCC
OK
192.168.11.101:6379> KEYS *
1) "school"
2) "AAA"
192.168.11.103:6379> KEYS *
1) "AAA"
2) "school"
root@node01:~# /etc/init.d/redis_6379 restart
root@node01:~# ss -lntup |grep redis
tcp LISTEN 0 128 192.168.11.102:6379 0.0.0.0:* users:(("redis-server",pid=15384,fd=6))
#宕机期间的数据自动同步
root@node01:~# redis-cli -h 192.168.11.102 -p 6379
192.168.11.102:6379> ping
PONG
192.168.11.102:6379> KEYS *
1) "AAA"
2) "school"
上一篇:Redis持久化
下一篇:软件开发项目文档系列之十二如何撰