MySQL replication

Topics Covered

How To Set Up Replication In MySQL?

MySQL replication is a process of copying data from one MySQL database server (the source) to one or more other MySQL database servers (the destination). Replication can be used for a variety of purposes such as improving database performance, creating backups, or distributing data among multiple servers.

Here are the general steps to set up replication in MySQL:

  • Prepare the source server: Make sure that the source server is configured to enable replication. This involves setting the server-id, creating a replication user, and setting the binary logging format.
  • Prepare the destination server(s): Ensure that the destination server(s) are configured to connect to the master server and that the necessary privileges are granted to the replication user.
  • Start the replication process: On the source server, start the binary logging process, and then create a snapshot of the database. On the destination server, set up the replication process by issuing a command that specifies the source server and other details.
  • Monitor and maintain replication: Check the replication status periodically to ensure that it is running correctly. Monitor the error logs for any issues and take corrective action if necessary.

Prerequisites

Before setting up replication in MySQL, there are several prerequisites that you should meet:

  • MySQL version
  • Server Hardware
  • Network connectivity
  • Server configuration
  • User permissions

Understanding Replication in MySQL

Step 1 — Adjusting Your Source Server’s Firewall

If you are setting up MySQL replication between two servers on different networks, you may need to adjust your firewall settings to allow the servers to communicate with each other.

Here are the general steps to adjust the firewall on the source server:

  • Determine the source server’s IP address: Use the ifconfig or ip addr command to determine the IP address of the source server.
  • Determine the port number used by MySQL: By default, MySQL uses port 3306 for client connections. Check your MySQL configuration file to confirm the port number.
  • Open the firewall for incoming connections: Use your firewall software to open port 3306 on the source server for incoming connections. The exact steps to do this will depend on your firewall software.
  • Save your firewall settings: Save your firewall settings to ensure that they persist across reboots. The exact steps to do this will depend on your firewall software.

By adjusting your firewall settings in this way, you can allow incoming connections to your source server on the port used by MySQL, which is necessary for MySQL replication to work correctly.

Step 2 — Configuring the Source Database

Before you can set up MySQL replication, you need to configure the source database on the source server. Here are the general steps to configure the source database:

  1. Enable binary logging: Binary logging allows MySQL to record all changes to the source database in a log file, which can be used for replication. You can enable binary logging by adding the following line to your MySQL configuration file (/etc/my.cnf or /etc/mysql/my.cnf):

This line specifies the name of the binary log file that MySQL will use.

  1. Set the server ID: The server ID is a unique identifier that MySQL uses to distinguish between servers in a replication environment. You can set the server ID by adding the following line to your MySQL configuration file:

Replace the number 1 with a unique identifier for the source server.

  1. Restart MySQL: After making changes to the MySQL configuration file, you need to restart MySQL to apply the changes. You can do this using the following command:
  1. Create a replication user: You need to create a user on the source server that the replication process can use to connect to the source database. You can create this user using the following SQL command:

Replace replication_user and password with the desired username and password for the replication user.

  1. Flush the binary log: To ensure that the binary log contains only the changes that occur after the replication process starts, you need to flush the binary log using the following command:

By completing these steps, you have configured the source database on the source server to allow MySQL replication. The next step is to configure the destination server which is the replication user.

Step 3 — Creating a Replication User

In addition to creating a replication user on the source server, you also need to create a replication user on the destination server. This user is used by the destination server to connect to the source server and retrieve the binary log.

Here are the general steps to create a replication user on the destination server:

  1. Log in to MySQL on the destination server: Use the MySQL client to log in to MySQL on the destination server as a user with administrative privileges.
  1. Create a replication user: Use the following SQL command to create a new user called replication_user with a password of password:

Replace replication_user and password with the desired username and password for the replication user.

  1. Grant privileges to the replication user: Use the following SQL command to grant the REPLICATION SLAVE privilege to the replication_user user:
  1. Flush privileges: Use the following SQL command to flush the privileges and apply the changes:

By completing these steps, you have created a replication user on the destination server that can be used to connect to the source server and retrieve the binary log.

Step 4 — Retrieving Binary Log Coordinates from the Source

If Your Source Doesn’t Have Any Existing Data to Migrate If your source server doesn't have any existing data to migrate, you can skip the process of retrieving binary log coordinates from the source server. Instead, you can start replication from the beginning of the binary log.

Here are the steps to configure replication from the beginning of the binary log:

  1. Log in to MySQL on the destination server: Use the MySQL client to log in to MySQL on the destination server as a user with administrative privileges.
  1. Configure replication: Use the following SQL command to configure replication on the destination server:

Replace source_server_ip, replication_user, and password with the IP address of the source server, the username, and password of the replication user, respectively. The MASTER_LOG_FILE and MASTER_LOG_POS values indicate the binary log file and position from which replication should start. Setting the MASTER_LOG_POS value to 0 indicates that replication should start from the beginning of the binary log.

  1. Start replication: Use the following SQL command to start replication on the destination server:

By completing these steps, you have configured replication on the destination server to start from the beginning of the binary log.

If Your Source Has Existing Data to Migrate

If your source server has existing data that you want to replicate to the destination server, you need to retrieve the binary log coordinates from the source server. These coordinates indicate the position in the binary log from which the destination server should start replicating data.

Here are the steps to retrieve the binary log coordinates from the source server:

  1. Log in to MySQL on the source server: Use the MySQL client to log in to MySQL on the source server as a user with administrative privileges.
  1. Show the master status: Use the following SQL command to show the current binary log file and position:

This command will output a table that shows the binary log file name and the position within the file. Make a note of these values, as you will need them when configuring the destination server. 3. Export the data: Use a tool such as mysqldump to export the data from the source server. Be sure to include the --master-data option to include the binary log coordinates in the dump file.

This command will create a dump file called dump.sql that includes the binary log coordinates. 4. Transfer the dump file: Transfer the dump file to the destination server using a secure method such as SCP. 5. Import the data: Use the following command to import the data from the dump file:

This command will import the data into the destination server. 5. Log in to MySQL on the destination server: Use the MySQL client to log in to MySQL on the destination server as a user with administrative privileges.

  1. Configure replication: Use the following SQL command to configure replication on the destination server:

Replace source_server_ip, replication_user, and password with the IP address of the source server, the username, and password of the replication user, respectively. Replace binary_log_file_name and binary_log_position with the values obtained in step 2.

  1. Start replication: Use the following SQL command to start replication on the destination server:

By completing these steps, you have retrieved the binary log coordinates from the source server and imported the data into the destination server. The destination server is now configured to replicate data from the source server.

Step 5 — Configuring the Replica Database

Now you need to configure the replica database to ensure that the data is consistent with the source database.

Here are the steps to configure the replica database:

  1. Stop replication: Use the following SQL command to stop replication on the destination server:
  1. Reset the replica database: Use the following SQL command to reset the replica database:

This command removes all binary log files on the destination server and resets the binary log position to 0.

  1. Import the data: Use the same method used in Step 4 to import the data from the source database into the replica database.
  2. Configure replication filters: If you want to replicate only a subset of the data from the source database, you can use replication filters to specify the tables or databases to be replicated. Use the following SQL command to configure replication filters:

Replace database_name with the name of the database to be replicated. Repeat this command for each database that you want to replicate. Alternatively, you can use the REPLICATE_IGNORE_DB filter to exclude certain databases from replication.

  1. Start replication: Use the following SQL command to start replication on the destination server:

By completing these steps, you have configured the replica database and started the replication process. The replica database will now be updated with new data as it is inserted, updated, or deleted from the source database.

Step 6 — Starting and Testing Replication

After configuring the source and destination servers, creating a replication user, and setting up replication, you can start and test the replication process.

Here are the steps to start and test replication:

  1. Check the replication status: Use the following SQL command to check the status of replication:

This command shows the current status of replication, including the replication mode, the replication delay, and any error messages.

  1. Verify that replication is running: Check the output of the SHOW SLAVE STATUS command to verify that replication is running. Look for the Slave_IO_Running and Slave_SQL_Running fields in the output. If both fields show Yes, replication is running.
  2. Test replication: Make changes to the source database, such as inserting, updating, or deleting data, and verify that the changes are replicated to the destination server. You can use the following SQL command to check the contents of a table on the source server and the destination server:

Compare the results of this command on the source and destination servers to verify that the data is being replicated correctly.

  1. Monitor replication: Monitor the replication process to ensure that it is running smoothly. You can use tools such as mysqlbinlog to view the binary log files and check for errors.

By completing these steps, you have started and tested replication between the source and destination servers. You can now use replication to keep the data on the destination server up-to-date with the data on the source server.

Conclusion

To summarize, here are the main points to set up replication in MySQL:

  • Adjust the firewall on the source server to allow incoming traffic on the MySQL port.
  • Configure the source database to enable binary logging.
  • Create a replication user on the source server with the necessary permissions to replicate data.
  • Retrieve the binary log coordinates from the source server.
  • Configure the replica database on the destination server to ensure data consistency with the source server.
  • Start and test replication by checking the status of replication, verifying that replication is running, testing replication, and monitoring replication for errors.