MySQL master-slave replication is a commonly used database replication technology that can synchronize data between the master server and the slave server. The master server is responsible for handling write operations and updating data, while the slave server is used for reading query operations and backing up data. This article will introduce how to configure the MySQL master-slave server and provide corresponding source code examples.
-
Environment Preparation
Before you begin configuration, ensure that the following requirements are met:- Install the MySQL database software and ensure that both the master and slave servers are accessible.
- Make sure that the master and slave servers are of the same version and support master-slave replication.
-
Configuring the Master Server
Perform the following configuration steps on the master server:- Open the main server's configuration file (my.cnf or my.ini).
- Add the following configuration in the [mysqld] section:
server-id = 1 log_bin = /var/log/mysql/mysql-bin.log
server-id
Set the unique identifier of the primary server, which can be any positive integer.log_bin
Specifies the location of the binary log file that records all writes and updates to the master server. - Save the configuration file and restart the master server.
-
Configuring the slave server
Perform the following configuration steps on the slave server:- Open the configuration file of the slave server.
- Add the following configuration in the [mysqld] section:
server-id = 2 relay_log = /var/log/mysql/mysql-relay-bin.log
server-id
Set the unique identifier of the slave server, which must be different from the master server.relay_log
Specifies the location of the relay log file where logs replicated from the master server are stored. - Save configuration file