Server configuration: (centos7)
IP server | Installation Components | node | Remark |
---|---|---|---|
192.168.27.210 | repeat-5.0.5 | master | A two master-slave architecture |
192.168.26.112 | repeat-5.0.5 | slave | |
192.168.26.206 | repeat-5.0.5 | slave |
Download the latest version to each node: Wget http://download.redis.io/releases/redis-5.0.5.tar.gz
extracting archive and configuration:
switch user (user ROOT less production operation)
[Jerry Data the @Master] $ the sudo chown -R & lt jerry.root Redis-5.0.5
of Cd /data/redis-5.0.5/src
the make
192.168.27.210 profile: vim /data/redis-5.0.5/redis-m-7000 .conf
the bind 0.0.0.0
Port 7000
logfile "7000.log"
dbfilename "dump-7000.rdb"
daemonize yes
rdbcompression yes
192.168.26.112 configuration:
Cd /data/redis-5.0.5/src
the make
[jerry@BDDB redis-5.0.5]$ vim /data/redis-5.0.5/redis-s-7000.conf
bind 0.0.0.0
port 7000
logfile "7000.log"
dbfilename "dump-7000.rdb"
daemonize yes
rdbcompression yes
slaveof 192.168.27.210 7000
192.168.26.206配置:
Cd /data/redis-5.0.5/src
make
bind 0.0.0.0
port 7000
logfile "7000.log"
dbfilename "dump-7000.rdb"
daemonize yes
rdbcompression yes
slaveof 192.168.27.210 7000
分别启动各结点:
[jerry@master src]$ ./redis-server ../redis-m-7000.conf
[jerry@BDDB src]$ sudo ln -s /data/redis-5.0.5/src/redis-server /usr/bin/redis-server
[jerry@BDDB src]$ redis-server ../redis-s-7000.conf
[jerry@DGIDC src]$ sudo ln -s /data/redis-5.0.5/src/redis-server /usr/bin/redis-server
[jerry@DGIDC src]$ redis-server ../redis-s-7000.conf
[jerry@DGIDC src]$ ss -tnl
看下三台服务器主从状态信息:
接着我们创建键值,测试主从是否同步:
三个结点我们都创建一个链接,方便我们使用。
[jerry@master src]$ sudo ln -s /data/redis-5.0.5/src/redis-cli /usr/bin/redis-cli
[jerry@BDDB src]$ sudo ln -s /data/redis-5.0.5/src/redis-cli /usr/bin/redis-cli
[jerry@DGIDC src]$ sudo ln -s /data/redis-5.0.5/src/redis-cli /usr/bin/redis-cli
观察主从信息状态:
info
重启主后服务器观察发现未保存的数据将丢失:
结论:
使用主从模式时应注意master节点的持久化操作,matser节点在未使用持久化的情况详情下如果宕机,并自动重新拉起服务,从服务器会出现丢失数据的情况。数据丢失的原因:因为master服务挂了之后,重启服务后,slave节点会与master节点进行一次完整的重同步操作,所以由于master节点没有持久化,就导致slave节点上的数据也会丢失掉。所以在配置了Redis的主从模式的时候,应该打开主服务器的持久化功能。
测试使用哨兵模式,自动监视Master节点,当前挂掉后,自动将Slaver节点变为Master节点:
然后手动切换从(26.206):
127.0.0.1:7000> slaveof 192.168.26.112 7000
127.0.0.1:7000> set name tom
小结:
- Master可读可写,Slaver只能读,不能写
- Master可以对应多个Slaver,但是数量越多压力越大,延迟就可能越严重
- Master return immediately after writing, writes substantially simultaneously synchronized to each asynchronous Slaver, so basically negligible delay
- Slaver can be upgraded by slaveof no one command as Master (Master hang up when the manual will become a Slaver Master)
- Can be monitored by sentinel sentry mode Master, hang up automatically when the Master Slaver election becomes Master, Slaver other automatic reconnection new Master (automatic unsuccessful sometimes need to manually cut the main, temporarily not find the specific reasons)