Home > Database > Mysql Tutorial > body text

How to use master-slave replication in MySQL to achieve data backup and recovery?

王林
Release: 2023-07-30 10:49:48
Original
1317 people have browsed it

How to use master-slave replication in MySQL to achieve data backup and recovery?

Data backup and recovery is a very important part of database management. MySQL provides the Master-Slave Replication function, which can realize automatic backup and recovery of data. This article will introduce in detail how to configure and use the master-slave replication function in MySQL.

1. Configure the master server (Master)

  1. In the my.cnf configuration file, add the following configuration:
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-do-db = your_database_name
Copy after login

Among them, server-id is the server ID, which can be set to any positive integer; log-bin is the name prefix of the binary log file; binlog-do-db specifies the name of the database that needs to be synchronized.

  1. Restart the MySQL service.
sudo service mysql restart
Copy after login
Copy after login
  1. Create an account for master-slave replication and grant replication permissions.
CREATE USER 'replication_user'@'%' IDENTIFIED BY 'your_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;
Copy after login
  1. View the main server status.
SHOW MASTER STATUS;
Copy after login

Record the values ​​of File and Position for later use.

2. Configure the slave server (Slave)

  1. In the my.cnf configuration file, add the following configuration:
[mysqld]
server-id = 2
Copy after login

Among them, server-id is the server ID and can be set to any positive integer.

  1. Restart the MySQL service.
sudo service mysql restart
Copy after login
Copy after login
  1. Connect to the slave server and execute the following command:
CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='replication_user', MASTER_PASSWORD='your_password', MASTER_LOG_FILE='master_log_file', MASTER_LOG_POS=master_log_pos;
Copy after login

Replace master_ip with the IP address of the master server and replication_user with the replication account of the master server. Replace your_password with the password of the replication account, master_log_file with the File value of the master server, and master_log_pos with the Position value of the master server.

  1. Start replication from the server.
START SLAVE;
Copy after login
  1. View slave server status.
SHOW SLAVE STATUSG
Copy after login

If the values ​​of Slave_IO_Running and Slave_SQL_Running are both "Yes", it means that the master-slave replication configuration is successful.

3. Data backup and recovery

  1. Data backup

When the data on the main server changes, MySQL will record these changes to the binary log In the file, the slave server will synchronize data by reading the binary log file of the master server.

  1. Data Recovery

If the master server fails, it needs to be switched to the slave server to provide services. At this point, you only need to upgrade the slave server to the master server.

STOP SLAVE;
RESET SLAVE;  -- 清除从服务器的主从配置
RESET MASTER; -- 清除主服务器的主从配置
Copy after login

Then modify the configuration of the slave server, set its server-id to 1, and restart the MySQL service.

In this way, the slave server is upgraded to the new master server. After the original master server is repaired, it can be configured as a slave server again.

So far, we have learned how to use master-slave replication in MySQL to implement data backup and recovery. By properly configuring the master-slave server, you can ensure data security and availability, reduce the risk of data loss, and improve system reliability and efficiency.

The above is the detailed content of How to use master-slave replication in MySQL to achieve data backup and recovery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template