How to deploy MySQL master-slave replication with Docker?
To deploy MySQL master-slave replication in Docker, the following steps need to be carried out:
- Set up a network (optional):
- Create a network called mysql-net using Docker.
- Start the MySQL master node container.
- Start a Docker container named mysql-master on the mysql-net network with port 3306 mapped to the host, setting the MYSQL_ROOT_PASSWORD environment variable to “password”, using the mysql image.
- Start the MySQL standby node container.
- Run a mysql container named mysql-slave, in detached mode, on the mysql-net network with port 3307 mapped to port 3306, setting the MySQL root password to “password”.
- Enable binary logging and master-slave replication in the master node container.
- Run the “docker exec -it mysql-master bash” command to access the MySQL shell, then log in as the root user. grant replication privileges to a user ‘slave_user’ identified by ‘password’, flush privileges, lock tables for reading, and display the master status.
- Note down the values of File and Position.
- Configuring master-slave replication from the node container.
- Log into the MySQL slave container using Docker, then enter the MySQL server using the root user. Update the master server information with the hostname, username, password, log file, and log position, then start the replication process.
- Replace [File] and [Position] with the corresponding values of the master node.
You have successfully deployed MySQL master-slave replication. You can access the database by connecting to port 3306 on the master node container or port 3307 on the slave node container.