redis主从复制
1.创建三个redis实例
在redis安装目录下,执行 ./install_server.sh 文件,修改对应的端口号进行分区。具体命令如下:
[root@hadoop1 redis]# cd /usr/local/redis/redis-5.0.10/utils
[root@hadoop1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] 6382
Please select the redis config file name [/etc/redis/6382.conf]
Selected default - /etc/redis/6382.conf
Please select the redis log file name [/var/log/redis_6382.log]
Selected default - /var/log/redis_6382.log
Please select the data directory for this instance [/var/lib/redis/6382]
Selected default - /var/lib/redis/6382
Please select the redis executable path [/usr/bin/redis-server]
Selected config:
Port : 6382
Config file : /etc/redis/6382.conf
Log file : /var/log/redis_6382.log
Data dir : /var/lib/redis/6382
Executable : /usr/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6382.conf => /etc/init.d/redis_6382
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
按照上面的操作进行两次,就得到需要的三个redis实例。
2.修改每个redis实例对应的配置文件
配置文件路径和修改的内容如下
--配置文件的路径
/etc/redis/6382.conf
vi 6382.conf
--具体内容为
--不已后台启动,日志打印在控制台
daemonize no
#logfile /var/log/redis_6380.log
将日志文件注释掉
3.分别启动三个redis实例的服务端和客户端
--正常启动服务端和客户端
/usr/local/redis/bin/redis-server /etc/redis/6382.conf
/usr/local/redis/bin/redis-cli -p 6382
--在客户端执行
help replicaof
得到:
REPLICAOF host port
summary: Make the server a replica of another instance, or promote it as master.
since: 5.0.0
group: server
--redis版本5的主从关联命令为
replicaof 127.0.0.1 6380
--执行该命令后两个redis实例端变化为:
主:
4173:M 09 Jan 2021 14:00:31.966 * Starting BGSAVE for SYNC with target: disk
4173:M 09 Jan 2021 14:00:31.968 * Background saving started by pid 4192
4192:C 09 Jan 2021 14:00:31.975 * DB saved on disk
4192:C 09 Jan 2021 14:00:31.976 * RDB: 6 MB of memory used by copy-on-write
4173:M 09 Jan 2021 14:00:31.986 * Background saving terminated with success
4173:M 09 Jan 2021 14:00:31.987 * Synchronization with replica 127.0.0.1:6381 succeeded
4173:M 09 Jan 2021 14:01:05.848 * Replica 127.0.0.1:6382 asks for synchronization
4173:M 09 Jan 2021 14:01:05.848 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'f2ae5f29461fec0dd5ace28a99a2dd87f25e08fb', my replication IDs are '3ee985fcfad67846a1b8ea4868fdffe9a1499bba' and '0000000000000000000000000000000000000000')
从:
4183:S 09 Jan 2021 14:00:31.520 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
4183:S 09 Jan 2021 14:00:31.520 * REPLICAOF 127.0.0.1:6380 enabled (user request from 'id=3 addr=127.0.0.1:48701 fd=7 name= age=5 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=44 qbuf-free=32724 obl=0 oll=0 omem=0 events=r cmd=replicaof')
4183:S 09 Jan 2021 14:00:31.964 * Connecting to MASTER 127.0.0.1:6380
4183:S 09 Jan 2021 14:00:31.965 * MASTER <-> REPLICA sync started
4183:S 09 Jan 2021 14:00:31.965 * Non blocking connect for SYNC fired the event.
4183:S 09 Jan 2021 14:00:31.965 * Master replied to PING, replication can continue...
4183:S 09 Jan 2021 14:00:31.966 * Trying a partial resynchronization (request 066c8d375423c56ec5437a5513595cbb206b1ce7:1).
4183:S 09 Jan 2021 14:00:31.970 * Full resync from master: 3ee985fcfad67846a1b8ea4868fdffe9a1499bba:0
4183:S 09 Jan 2021 14:00:31.970 * Discarding previously cached master state.
4183:S 09 Jan 2021 14:00:31.987 * MASTER <-> REPLICA sync: receiving 206 bytes from master
4183:S 09 Jan 2021 14:00:32.000 * MASTER <-> REPLICA sync: Flushing old data
4183:S 09 Jan 2021 14:00:32.000 * MASTER <-> REPLICA sync: Loading DB in memory
4.具体演示
--在主保存值
127.0.0.1:6380> set k 3
OK
127.0.0.1:6380> set kk 11
OK
--在从就会得到值
127.0.0.1:6381> get kk
"11"
--从机不能够设置值
127.0.0.1:6382> set k1 44
(error) READONLY You can't write against a read only replica.
5.故障演示
--从机挂了,启动命令为
[root@hadoop1 ~]# /usr/local/redis/bin/redis-server /etc/redis/6381.conf --replicaof 127.0.0.1 6380
--启动后,从机会自动同步主机所有数据,包括在从机挂掉后,主机新设置的值。当然前提是,这个从机以前就跟随这个主机
--主机挂了选择一个从机执行
replicaof no one
--然后在在另一个从机执行
replicaof 127.0.0.1 所要根据的主机端口号
6.补充
(1)在 主机是能够知道有那些从机连接。