The process of redis master-slave replication synchronization implementation
1. The slave service sends a sync synchronization command to the master service to request full synchronization (Recommended learning: Redis Video Tutorial)
2. When the main service receives the sync synchronization command from the slave service, it will fork a child process to execute the bgsave command in the background (non-blocking) to save the snapshot. Generate an RDB file and send the
RDB file to the slave service
3. The slave service then loads the received RDB file into its own redis memory
4. Wait After the slave service completes loading the RDB, the master service sends all the write commands in the buffer to the slave service
5. The slave service loads all the write commands of the master service into the memory to achieve complete data synchronization
6. The next time the slave service needs to synchronize data, it only needs to send its own offset position (equivalent to the position of mysql binlog). Only the newly added data will be synchronized, and full synchronization is no longer required.
Master-slave synchronization through configuration files
1. Just configure the slave service configuration file
[root@localhost ~]# vim /app/redis/etc/redis.conf ….. slaveof 192.168.1.9 6379 #添加属于某台主机的从 服务 ….. masterauth 123456 #从服务连接主服的密码 …… slave-read-only yes #从服务只读,不可在命令行写入数据 ……
2. Restart the slave The service is to realize the master-slave connection
[root@localhost ~]# redis-server /app/redis/etc/redis.conf [root@localhost ~]# redis-cli 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> info replication #Replication role:slave master_host:192.168.1.9 master_port:6379 master_link_status:up …… ……
The above is the detailed content of How to perform master-slave replication in redis. For more information, please follow other related articles on the PHP Chinese website!