CentOS-7 Redis主从复制实践笔记记录
2018-01-31 本文已影响18人
小钟钟同学
环境准备
基于:https://www.jianshu.com/p/c450612c6295安装完成的虚拟机上克隆出新的一个虚拟机做相关测试。
主从服务都安装上了redis环境
主服务器:192.169.74.128
从服务器:182.168.74.129
image.png
1.主服务器redis.conf配置
因为启动的redis的时候,我们的启动方式使用指定的默认配置文件来进行启动的!
[root@localhost ~]# redis-server /usr/local/redis/redis.conf
(1)所以我们需要修改一下相关启动文件的配置
bind 192.168.4.1
requirepass 123456
image.png
(2)启动主服务器redis进行测试
[root@localhost ~]# redis-cli -h 192.168.74.128 -p 6379 -a 123456
192.168.74.128:6379> ping
PONG #表示成功
192.168.74.128:6379>
2.从服务器 增加下面两个信息,修改相关的redis.conf配置
(1)配置主服务器的相关信息
slaveof 192.168.74.128 6379
masterauth 123456
image.png
(2)启动从服务器redis进行测试
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> ping
PONG # 表示成功
127.0.0.1:6379>
3.测试主从同步
(1)主服务器写入数据:
[root@localhost ~]# redis-cli -h 192.168.74.128 -p 6379 -a 123456
192.168.74.128:6379> ping
PONG
192.168.74.128:6379> set xiaozhong 'xiaozhong'
OK
192.168.74.128:6379> keys *
1) "xiaozhong"
192.168.74.128:6379>
(2)从服务器查看主服务器的写入的数据:
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "xiaozhong"
127.0.0.1:6379> get xiaozhong
"xiaozhong"
127.0.0.1:6379>
============================================
4.其他情况的测试
第1种:主从服务器都还在存活的情况下,在才服务器上进行写入
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "xiaozhong"
127.0.0.1:6379> get xiaozhong
"xiaozhong"
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379>
出现:(error) READONLY You can't write against a read only slave.错误提示!
尝试解决方案:
将从redis设成主redis
redis-cli -h 127.0.0.1 -p 6379 -a 123456 slaveof NO ONE
redis-cli -h 127.0.0.1 -p 6379 -a 123456 set slave-read-only no
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "xiaozhong"
127.0.0.1:6379> keys *
1) "xiaozhong"
127.0.0.1:6379> get xiaozhong
"xiaozhong"
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379> exit
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 slaveof NO ONE
OK
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 set slave-read-only no
OK
[root@localhost ~]#
重新再进行写入测试:
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 slaveof NO ONE
OK
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 set slave-read-only no
OK
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
OK
127.0.0.1:6379>
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 slaveof NO ONE
OK
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456 set slave-read-only no
OK
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
OK
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong"
127.0.0.1:6379>
查看主服务器的情况:
从数据库写入的数据在主服务器查看是没有同步过来!--(可能的原因因为我们配置的是主从模式)
[root@localhost ~]# redis-cli -h 192.168.74.128 -p 6379 -a 123456
192.168.74.128:6379> ping
PONG
192.168.74.128:6379> set xiaozhong 'xiaozhong'
OK
192.168.74.128:6379> keys *
1) "xiaozhong"
192.168.74.128:6379> keys *
1) "xiaozhong"
192.168.74.128:6379>
第2种:主服务器挂了的情况下,从服务器(升级为主节点后,继续向从服务器(slave)写入)
(1)停止主服务器
redis-cli -h 192.168.74.128 -p 6379 -a 123456 shutdown
或
[root@localhost ~]# ps -ef|grep redis
root 2918 1 0 03:29 ? 00:00:06 redis-server 192.168.74.128:6379
root 3097 2354 0 04:56 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# kill - 9 2918
192.168.74.128:6379> exit
[root@localhost ~]# ps -ef|grep redis
root 2918 1 0 03:29 ? 00:00:06 redis-server 192.168.74.128:6379
root 3097 2354 0 04:56 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# kill - 9 2918
-bash: kill: : invalid signal specification
[root@localhost ~]# kill -9 2918
[root@localhost ~]#
(2)从服务器在继续写入数据
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
OK
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong"
127.0.0.1:6379> set xiaozhong_congfuwuqi2 'congceshi2'
OK
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong_congfuwuqi2"
4) "xiaozhong"
127.0.0.1:6379>
(3)主服务器恢复并写入数据(查看数据)
[root@localhost ~]# redis-server /usr/local/redis/redis.conf
[root@localhost ~]# redis-cli -h 192.168.74.128 -p 6379 -a 123456
192.168.74.128:6379> set xiaozhong_zhufuwuqi 'ceshi'
OK
192.168.74.128:6379> keys *
1) "xiaozhong"
2) "xiaozhong_zhufuwuqi"
192.168.74.128:6379>
(4)再次查看从服务器数据
问题:发现主服务器新写入的数据,从服务器没有写入?
具体是什么问题呐?
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> set xiaozhong_congfuwuqi 'congceshi'
OK
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong"
127.0.0.1:6379> set xiaozhong_congfuwuqi2 'congceshi2'
OK
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong_congfuwuqi2"
4) "xiaozhong"
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong_congfuwuqi2"
4) "xiaozhong"
127.0.0.1:6379>
(5)退出从服务器重新进入,也还是没有主服务器最新的数据
127.0.0.1:6379> exit
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> keys *
1) "xiaozhong_congfuwuqi"
2) "slave-read-only"
3) "xiaozhong_congfuwuqi2"
4) "xiaozhong"
127.0.0.1:6379>
(6)重新启动从服务器
发现以前写入的数据,丢失了!主服务器的新写入的数据也写入了!
[root@localhost ~]# ps -ef|grep redis
root 2799 1 0 03:26 ? 00:00:08 redis-server 0.0.0.0:6379
root 3041 2311 0 05:06 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# kill -9 2799
[root@localhost ~]# ps -ef|grep redis
root 3043 2311 0 05:06 pts/0 00:00:00 grep --color=auto redis
[root@localhost ~]# redis-server /usr/local/redis/redis.conf
[root@localhost ~]# redis-cli -h 127.0.0.1 -p 6379 -a 123456
127.0.0.1:6379> keys *
1) "xiaozhong_zhufuwuqi"
2) "xiaozhong"
127.0.0.1:6379>