Redis master-slave replication environment & Notes

The first chapter .Redis master-slave replication

1.1 Master-slave replication role

Mainly to solve the problem when the risk of data loss occurs single host, call the shots from the backup data to achieve, and MySQL master-slave replication as nature.

Master-slave synchronization Example 1.2

Host environment Roles
192.168.188.159-Redis01 Primary-side (primary library)
192.168.188.160-Redis02 Preparation of the end (from the library)

1.2.1 master node operation

1.打包备份目录的后上传到备份节点
[root@redis01 ~]# tar zcvf redis01.tar.gz  /opt/redis_cluster/
[root@redis01 ~]# scp  redis01.tar.gz redis02:/opt/

1.2.2 From node operation

1.解压缩,生成redids-cli命令
[root@redis02 opt]# tar xf redis01.tar.gz
[root@redis02 opt]# mv opt/* .
[root@redis02 opt]# rm -rf redis01.tar.gz opt/
[root@redis02 opt]# cd redis_cluster/redis/ ; make install

2.创建备份目录
[root@redis02 redis-3.2.9]# mkdir -p /data/redis_cluster/redis_6379/

3.修改配置文件
[root@redis02 redis]# vim /opt/redis_cluster/redis_6379/conf/redis_6379.conf 
## 绑定主机上的网卡IP地址,这里需要修改成你当前主机的内网IP地址,备的地址
bind 127.0.0.1 192.168.188.160
##同步主数据文件
SLAVEOF 192.168.188.159 6379
##验证主库认证密码
#masterauth 123456
[root@redis02 redis]# redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
[root@redis02 redis]# netstat -lntp
[root@redis02 redis]# redis-cli -h redis02
redis02:6379> 

1.2.3 build relations from a master copy

从库端操作
配置方法:
1.临时生效
[root@redis02 redis]# redis-cli -h redis02
redis02:6379> SLAVEOF 192.168.188.159 6379
OK

2.写入配置文件永久生效
SLAVEOF 192.168.188.159 6379

1.2.4 check the log, success

主端日志:
[root@redis01 ~]# tail -8  /opt/redis_cluster/redis_6379/logs/redis_6379.log 
1112:M 24 Mar 00:24:13.174 * Slave 192.168.188.160:6379 asks for synchronization
1112:M 24 Mar 00:24:13.174 * Full resync requested by slave 192.168.188.160:6379
1112:M 24 Mar 00:24:13.174 * Starting BGSAVE for SYNC with target: disk 
1112:M 24 Mar 00:24:13.358 * Background saving started by pid 1663
1663:C 24 Mar 00:24:13.432 * DB saved on disk
1663:C 24 Mar 00:24:13.433 * RDB: 6 MB of memory used by copy-on-write
1112:M 24 Mar 00:24:13.526 * Background saving terminated with success
1112:M 24 Mar 00:24:13.532 * Synchronization with slave 192.168.188.160:6379 succeeded
#出现连接的slave端信息后,日志说明已经配置成功

备端日志:
[root@redis02 redis]#  tail -8  /opt/redis_cluster/redis_6379/logs/redis_6379.log
2005:C 24 Mar 00:24:13.635 * Parent agreed to stop sending diffs. Finalizing AOF...
2005:C 24 Mar 00:24:13.636 * Concatenating 0.00 MB of AOF diff received from parent.
2005:C 24 Mar 00:24:13.637 * SYNC append only file rewrite performed
2005:C 24 Mar 00:24:13.638 * AOF rewrite: 6 MB of memory used by copy-on-write
1984:S 24 Mar 00:24:13.694 * Background AOF rewrite terminated with success
1984:S 24 Mar 00:24:13.694 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
1984:S 24 Mar 00:24:13.694 * Background AOF rewrite finished successfully
1984:S 24 Mar 00:25:51.481 * SLAVE OF would result into synchronization with the master we are already connected with. No operation performed.
#未检测到主端的操作记录,配置成功

1.2.5 master-slave replication processes

从日志中看出:
1.从节点发送同步请求到主节点上进行确立同步关系
2.主节点接收到从节点的请求之后,做的相关操作
- 确定主从关系
- 主节点立即执行bgsave将当前内存里的持久化数据,保存在硬盘上,用作同步使用.
- 持久化完成之后(DB saved on disk),将持久化数据发送给从节点
3.从节点接收到持久化数据文件之后,做的相关操作
- 清空从节点的自身内存中的数据
- 加载来自主节点的持久化数据到从节点的内存中
- 从库确定完成结果,发送给主库,主库确认完成
4.确定完成同步,往后的操作就是主写入从同步(只读,不可写)

1.2.6 summary of master-slave replication

关于同步:
1.配置主从同步的时候 ,需要优先备份主从库的持久化数据,避免出现问题数据丢失的情况.
2.开始同步 SLAVEOF 192.168.188.159 6379   	#从库指定主库的host和port
3.停止同步 SLAVEOF no one   				#可在从库配置文件或内存中操作
4.主从同步的时候, 主库负责写入,从库负责接收主库的信息,且从库不可写,只可以读.
	如从库插入数据会报错 (error) READONLY You can't write against a read only slave.
5.当主节点发生故障后,从节点依旧同步主节点,从节点不会因主挂掉就不同步.
6.当主发生故障后,从节点操作如下:
	1.修改配置文件的bind指向主节点主机信息
	2.从节点执行停止同步 SLAVEOF no one
7.建立主从同步后,从节点会清空现有数据后,在开始发起同步请求.如果同步错误,从库数据就不复存在.
8.主从同步配置完成后,当主库有认证登录的时候,从库需要配置认证信息,配置文件增加 masterauth 123456

Guess you like

Origin www.cnblogs.com/liych/p/12559117.html