How to use Redis master-slave replication


This article mainly introduces how to use Redis master-slave replication. In daily operations, I believe many people have doubts about how to use Redis master-slave replication. The editor consulted various materials and sorted out simple and easy-to-use operations method, I hope it will be helpful for everyone to answer the doubts about how to use Redis master-slave replication! Next, please follow the editor to learn together!

The master server (master) enables binary logs and selects a unique server-id to create a user with replication permissions

Enables relay logs from the server (slave), selects a unique server -id Connect to the main server and start copying

Main library ip: Port: 3306 Slave library ip: Port: 3306

Main library configuration

(1) Set the server-id value and enable the binlog parameter

log_bin = mysql-bin
server_id = 130

< /blockquote>

Restart the database

(2) Create a synchronization account

create user 'rep1'@'' identified with mysql_native_password by 'Test@1234 '#Set account password
grant replication slave on *.* to 'rep1'@'';
grant replication slave on *.* to 'rep1'@'';< br/>show grants for 'rep1'@'';

(3) Lock table setting read-only

Prepare for future backup, pay attention to the production environment To apply for downtime in advance;

mysql> flush tables with read lock;

Reminder: Do not operate if the set time is exceeded will be unlocked automatically.

mysql> show variables like '%timeout%';

Test whether the database can be created after locking the table

4) View the status of the main library View the main libraryStatus, that is, the current log file name and binary log offset

mysql> show master status;

Remember the file and position to facilitate the subsequent connection of the slave.

(5) Backup database data

mysqldump -uroot -p -A -B |gzip > mysql_bak.$(date +%F).sql. gz


mysql> unlock tables;

(7) Upload the backup data of the master database to the slave database

scp /server/backup/mysql_bak.2022-09-22.sql.gz /hh

Set from the library

(1) Set the server-id value and close the binlog parameter

#log_bin = /data/mysql/data/mysql-bin

server_id = 139

Restart the database

(2) Restore the backup data from the main library

cd /server/backup/ gzip -d mysql_bak.2022-09-22.sql.gz mysql -uroot -p < mysql_bak.2022-09-22.sql

Check and restore:

mysql -uroot - p -e 'show databases;'

(3) Set synchronization from the main database

mysql> change master to -> master_host='', -> master_port=3306, -> master_user='rep1', -> master_password='Test@1234', -> master_log_file='mysql-bin.000006', -> master_log_pos=157;

(4) Start slave library synchronization switch

mysql> start slave;


Check status:

mysql> show slave status\G

The master-backup replication function has been achieved.

Test the following:

Execute on (main):

create databses data; New database


The data file has also been created from the virtual machine, realizing the master-slave replication of Mysql.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us

Recommended reading

high perspicacity