The master copy from redis
A, redis master-slave synchronization
How it works:
1. Send SYNC command to the master server from the server.
2. SYNC command to the master server calls BGSAVE command to create a RDB file, and use all the write command buffer recording executed next.
3. When the primary server BGSAVE been executed command, it sends the file to the RDB server, the file will be received and loaded from the server.
4. All the write command stored in the buffer to the master server transmitted from the server to execute.
-------------
1, when opened from the master copy, using the RDB embodiment, the synchronization data from the master
2, then the synchronization start, propagate through the primary library commands, active the replication achieve
3,2.8 later realized PSYNC mechanism to achieve reconnection
1, environment preparation
Main Library: 6380 redis (master) from Library 1: redis (slave) 6381 of redis 6382's (Slave): from the library 2
Main library configuration redis.conf
port 6380 # port daemonize yes # running in the background pidfile /data/6380/redis.pid # process the above mentioned id the LogLevel Notice logfile "/data/6380/redis.log" # log file, you need to create their own dbfilename dump.rdb # persistent storage the dir / Data / 6380 protected-MODE NO
Configuration from the library 1redis.conf
6381 Port to daemonize Yes PidFile /data/6381/redis.pid LogLevel Notice logfile "/data/6381/redis.log" dbfilename dump.rdb the dir / Data / 6381 protected MODE NO- IP slaveof 192.168.61.129 main library designated # 6380 and port
Configuration from the library 2redis.conf
port 6382 daemonize yes pidfile /data/6382/redis.pid loglevel notice logfile "/data/6382/redis.log" dbfilename dump.rdb dir /data/6382 protected-mode no slaveof 192.168.61.129 6380
note:
If the primary database password is set, then we in the library configuration file you need to add a password main library
masterauth root # password specified main library
2, start the server redis
redis-server /data/6380/redis.conf redis-server /data/6381/redis.conf redis-server /data/6382/redis.conf
3, log in to view the master-slave relationship database
Main Library [6380]:
[root@szx /data/6380 21:46:33]#redis-cli -p 6380 -h 192.168.61.129 192.168.61.129:6380> info replication # Replication role:master connected_slaves:2 # 有两个从库连接 slave0:ip=192.168.61.129,port=6381,state=online,offset=2100,lag=0 slave1:ip=192.168.61.129,port=6382,state=online,offset=2100,lag=0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2100 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2100
[6381] 1 from the library:
[root@szx /data/6381 21:50:27]#redis-cli -p 6381 -h 192.168.61.129 192.168.61.129:6381> info replication # Replication role:slave # 从库 master_host:192.168.61.129 # 主库ip master_port:6380 # 主库端口 master_link_status:up # 连接主库正常 master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:2226 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2226 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2226
从库2【6382】:
[root@szx /data/6382 21:50:27]#redis-cli -p 6382 -h 192.168.61.129 127.0.0.1:6382> info replication # Replication role:slave master_host:192.168.61.129 master_port:6380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:2366 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2366 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:43 repl_backlog_histlen:2324
4、查看主从复制效果
主库写入数据
192.168.61.129:6380> SET name "alex" OK 192.168.61.129:6380> mset name "wusir" age 88 OK
从库1查看key
192.168.61.129:6381> get name "alex" 192.168.61.129:6381> mget name age 1) "wusir" 2) "88"
从库2查看key
127.0.0.1:6382> get name "alex" 127.0.0.1:6382> mget name age 1) "wusir" 2) "88"
二、主从复制故障恢复
从库挂了无所谓 主库挂了得立即恢复主库,或者将从库切换为主库,继续工作 -.实验步骤 1.分别启动 6380 6381 6382 三个数据库实例,建立好主从关系 2.杀死6380主库,此时6381 6382 群龙无首 3.选择让6381为新的主库,就要去除6381的从的身份 redis-cli -p 6381 slaveof no one #查看此时6381的身份 redis-cli -p 6381 info replication 4.此时将6382的主人改为6381 redis-cli -p 6382 slaveof 192.168.61.129 6381
一、redis主从同步
原理:
1. 从服务器向主服务器发送 SYNC 命令。
2. 接到 SYNC 命令的主服务器会调用BGSAVE 命令,创建一个 RDB 文件,并使用缓冲区记录接下来执行的所有写命令。
3. 当主服务器执行完 BGSAVE 命令时,它会向从服务器发送 RDB 文件,而从服务器则会接收并载入这个文件。
4. 主服务器将缓冲区储存的所有写命令发送给从服务器执行。
-------------
1、在开启主从复制的时候,使用的是RDB方式的,同步主从数据的
2、同步开始之后,通过主库命令传播的方式,主动的复制方式实现
3、2.8以后实现PSYNC的机制,实现断线重连
1、环境准备
主库:6380的redis(master) 从库1:6381的redis(slave) 从库2:6382的redis(slave)
主库redis.conf配置
port 6380 # 端口 daemonize yes # 在后台运行 pidfile /data/6380/redis.pid # 进程id loglevel notice logfile "/data/6380/redis.log" # 日志文件,需自己创建 dbfilename dump.rdb # 持久化存储 dir /data/6380 protected-mode no
从库1redis.conf配置
port 6381 daemonize yes pidfile /data/6381/redis.pid loglevel notice logfile "/data/6381/redis.log" dbfilename dump.rdb dir /data/6381 protected-mode no slaveof 192.168.61.129 6380 # 指定主库的ip和端口
从库2redis.conf配置
port 6382 daemonize yes pidfile /data/6382/redis.pid loglevel notice logfile "/data/6382/redis.log" dbfilename dump.rdb dir /data/6382 protected-mode no slaveof 192.168.61.129 6380
注意:
如果主库中设置了密码,那么我们在从库的配置文件中就需要添加主库的密码
masterauth root # 指定主库的密码
2、启动redis服务端
redis-server /data/6380/redis.conf redis-server /data/6381/redis.conf redis-server /data/6382/redis.conf
3、登录数据库查看主从关系
主库【6380】:
[root@szx /data/6380 21:46:33]#redis-cli -p 6380 -h 192.168.61.129 192.168.61.129:6380> info replication # Replication role:master connected_slaves:2 # 有两个从库连接 slave0:ip=192.168.61.129,port=6381,state=online,offset=2100,lag=0 slave1:ip=192.168.61.129,port=6382,state=online,offset=2100,lag=0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2100 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2100
从库1【6381】:
[root@szx /data/6381 21:50:27]#redis-cli -p 6381 -h 192.168.61.129 192.168.61.129:6381> info replication # Replication role:slave # 从库 master_host:192.168.61.129 # 主库ip master_port:6380 # 主库端口 master_link_status:up # 连接主库正常 master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:2226 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2226 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2226
从库2【6382】:
[root@szx /data/6382 21:50:27]#redis-cli -p 6382 -h 192.168.61.129 127.0.0.1:6382> info replication # Replication role:slave master_host:192.168.61.129 master_port:6380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:2366 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:a70b4cc777e7f27efef1d2463f4d78d556a0162b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2366 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:43 repl_backlog_histlen:2324
4、查看主从复制效果
主库写入数据
192.168.61.129:6380> SET name "alex" OK 192.168.61.129:6380> mset name "wusir" age 88 OK
从库1查看key
192.168.61.129:6381> get name "alex" 192.168.61.129:6381> mget name age 1) "wusir" 2) "88"
从库2查看key
127.0.0.1:6382> get name "alex" 127.0.0.1:6382> mget name age 1) "wusir" 2) "88"
二、主从复制故障恢复
From the library hung up the matter of the main library terrible hang immediate resumption of the main library, the library or switching from the main library, continue to work - Experimental Procedure 1. Launch 638,063,816,382 respectively three database instance, the establishment of a good master-slave relationship 2. Kill death 6380 main library, this time 63,816,382 rudderless 3. choose to have 6381 as the new main library, it is necessary to remove the identity from the 6381's redis-cli -p 6381 slaveof NO One # 6381 to view at this time the identity of redis-cli -p Replication info 6381 4. At this time, the master 6382 to 6381 Redis -p-CLI 6381 6382 slaveof 192.168.61.129