Home> Database> Redis> body text

Redis master-slave replication step-by-step explanation and use

WBOY
Release: 2022-10-04 09:00:27
forward
2379 people have browsed it

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.

Redis master-slave replication step-by-step explanation and use

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;
Copy after login

Tips: If no operation occurs beyond the set time, it will be automatically unlocked.

mysql> show variables like '%timeout%';
Copy after login

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;
Copy after login

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
Copy after login

(6) Unlock

mysql> unlock tables;
Copy after login

(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
Copy after login

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
Copy after login

Check the restore:

mysql -uroot -p -e 'show databases;'
Copy after login

(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;
Copy after login

(4) Start the synchronization switch from the slave library

mysql> start slave;
Copy after login

Check the status:

mysql> show slave status\G
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!