This article brings you relevant knowledge about redis, which mainly introduces the relevant content about master-slave replication. Redis plays a huge role in our back-end services because of its high performance and ease of use. And the implementation of many important functions will rely on redis. Let’s take a look at it. I hope it will be helpful to everyone.
Recommended learning: Redis video tutorial
The master server (master) enables binary logs and selects a unique server-id to create a file with Users with replication permissions
enable relay logs from the slave server (slave), select a unique server-id to connect to the master server, and start replication
Main library ip: 192.168.235.130 port: 3306 Slave library ip: 192.168.235.139 Port: 3306
Main library configuration
(1) Set the server-id value and enable binlog parameters
[mysqld]
log_bin = mysql-bin
server_id = 130
Restart the database
(2) Create a synchronization account
create user 'rep1 '@'192.168.10.139' identified with mysql_native_password by 'Test@1234'#Set account password
grant replication slave on *.* to 'rep1'@'192.168.235.139';
grant replication slave on *. * to 'rep1'@'192.168.235.139';
show grants for 'rep1'@'192.168.235.139';
(3) Lock table setting read-only
Prepare for subsequent backups, please note that the production environment must apply for downtime in advance;
mysql> flush tables with read lock;
Tips: If no operation occurs beyond the set time, it will be automatically unlocked.
mysql> show variables like '%timeout%';
Test whether the database can be created after locking the table
4) Check the main library status Check the main library status, that is, the current log file name and binary log offset
mysql> show master status;
Remember file and position to facilitate subsequent slave connections.
(5) Back up database data
mysqldump -uroot -p -A -B |gzip > mysql_bak.$(date +%F).sql.gz
(6) Unlock
mysql> unlock tables;
(7) Upload the backup data of the main database to the slave database
scp /server/backup/mysql_bak.2022-09-22.sql.gz 192.168.235.139:/root/hh
Set up
(1) Set the server-id value and turn off the binlog parameter
#log_bin = /data/mysql/data/mysql-bin
server_id = 139
Restart the database
(2)Restore the backup data from the main database
cd /server/backup/ gzip -d mysql_bak.2022-09-22.sql.gz mysql -uroot -p < mysql_bak.2022-09-22.sql
Check the restore:
mysql -uroot -p -e 'show databases;'
(3) Set the synchronization from the main library
mysql> change master to -> master_host='192.168.235.130', -> master_port=3306, -> master_user='rep1', -> master_password='Test@1234', -> master_log_file='mysql-bin.000006', -> master_log_pos=157;
(4) Start the synchronization switch from the slave library
mysql> start slave;
Check the status:
mysql> show slave status\G
The primary and secondary replication functions are achieved.
Test below:
Execute on 192.168.235.130 (main):
create databses data;
New database
The data file is also built on the slave virtual machine, and the master-slave replication of Mysql is realized.
Recommended learning: Redis video tutorial
The above is the detailed content of Redis master-slave replication step-by-step explanation and use. For more information, please follow other related articles on the PHP Chinese website!