Home > Article > Operation and Maintenance > How to configure database master-slave replication on Linux
How to configure database master-slave replication on Linux
In Linux systems, configuring database master-slave replication is a common task. Master-slave replication can provide data redundancy backup, load balancing and high availability. This article will introduce the steps of configuring database master-slave replication on Linux and provide corresponding code examples.
Step 1: Install database software
First, we need to install the corresponding database software on the master server and slave server. This article takes the MySQL database as an example. The installation process is as follows:
Execute the following commands on the master server and slave server to install the MySQL database:
sudo apt update sudo apt install mysql-server
Step 2: Configure the main server
Next, we need to perform a series of configurations on the main server:
Edit the MySQL configuration file/etc/mysql/mysql. conf.d/mysqld.cnf
, uncomment the following lines (delete the # before the line):
#bind-address = 127.0.0.1 #log_bin = /var/log/mysql/mysql-bin.log #binlog_do_db = mydatabase
Restart the MySQL server:
sudo service mysql restart
Log in to the MySQL server and execute the following SQL command:
mysql -u root -p
Execute in the MySQL interactive interface:
CREATE USER 'replication'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%'; FLUSH PRIVILEGES; FLUSH TABLES WITH READ LOCK; SHOW MASTER STATUS;
Note down the File
and The value of the Position
field will be used on the slave server.
Unlock form:
UNLOCK TABLES;
Step 3: Configure the slave server
Next, perform a series of configurations on the slave server:
Edit the MySQL configuration file/etc/mysql/mysql.conf.d/mysqld.cnf
and uncomment the following lines:
#bind-address = 127.0.0.1
Restart the MySQL server:
sudo service mysql restart
Log in to the MySQL server and execute the following SQL command: (Replace 412d557bec4e5def6d6435dfb165ebbe
with that of the master server IP address, 6c758c1d3077d1131076a81a435b0724
and e9089245baf80435fcf1948d83058143
are replaced with the File
and Position## recorded in the previous step. #The value of the field.)
mysql -u root -pExecute in the MySQL interactive interface:
CHANGE MASTER TO MASTER_HOST='<master-ip>', MASTER_USER='replication', MASTER_PASSWORD='password', MASTER_LOG_FILE='<master-file>', MASTER_LOG_POS=<master-position>; START SLAVE;
SHOW SLAVE STATUSGEnsure
The values of the Slave_IO_Running and
Slave_SQL_Running fields are both
Yes, indicating that master-slave replication has been successfully configured.
This article introduces the steps to configure database master-slave replication under Linux system, and provides corresponding code examples. Through master-slave replication, we can obtain the benefits of data redundancy backup, load balancing and high availability. When configuring master-slave replication, you need to pay attention to the software installation and configuration of the master server and slave servers, and correctly set the connections and permissions of the master and slave servers. I hope this article will help you configure database master-slave replication on your Linux system.
The above is the detailed content of How to configure database master-slave replication on Linux. For more information, please follow other related articles on the PHP Chinese website!