Docker set up mysql master/slave replication
- Docker pull mysql:5.7
1. Start the master and slave
- docker run -it -p 3306:3306 –name master –privileged=true -v /f/DockerServer/mysql3306/conf/conf.d:/etc/mysql/conf.d – v/f/DockerServer/mysql3306 / data: / var/lib/mysql – e MYSQL_ROOT_PASSWORD = 123456 – d mysql: 5.7
- docker run -it -p 3307:3306 –name slave –privileged=true -v /f/DockerServer/mysql3306/conf/conf.d:/etc/mysql/conf.d -v / f/DockerServer/mysql3307 / data: / var/lib/mysql – e MYSQL_ROOT_PASSWORD = 123456 – d mysql: 5.7
2. Go to the master and modify the configuration
- docker exec -it 627a2368c865 bash
- cd /etc/mysql
- vim my.cnf
- Bash: vi: command not found
- apt-get install vim
- apt-get update
- apt-get install vim
- Add the following configuration to my.cnf
[mysqld] ## select * from mysqld where server id=100 ## select * from mysqld where server id=100 ## select * from mysqld where log-bin=mysql-binCopy the code
- After the configuration is complete, restart the mysql service for the configuration to take effect. Run the service mysql restart command to complete the restart. Docker start master: Docker start master: Docker start master: Docker start master
- Master Creates a data synchronization user for the Master database and grants the rights of slave REPLICATION slave and REPLICATION CLIENT to synchronize data between the Master and slave databases.
- Docker exec-it 627a2368c865 bash enters the master container
- mysql -u root -p
- Enter the password
- CREATE USER ‘slave’@’%’ IDENTIFIED BY ‘123456’;
- GRANT REPLICATION SLAVE, REPLICATION CLIENT ON . TO ‘slave’@’%’;
3. Access slave to modify configurations
- docker exec -it 123a2368c123 bash
- cd /etc/mysql
- vim my.cnf
- apt-get install vim
- Add the following configuration to my.cnf
[mysqld] ## mysqld ## mysqld ## mysqld ## mysqld ## mysqld When a Slave is the Master of another Slave, run log-bin=mysql-slave-bin ## relay_log to configure the relay log. Relay_log =edu-mysql-relay-binCopy the code
- After the configuration is complete, restart the mysql service for the configuration to take effect. Run the service mysql restart command to complete the restart. Docker start slave docker start slave docker start slave
4. Enter the master container
- Mysql -u root -p
- show master status;
- Mysql-bin.000001), Position (617)
5. Exit the Docker container.
- Query the IP addresses of master and slave:
- Docker inspect – format = ‘{{. NetworkSettings. IPAddress}}’ master (127.0.0.2)
- Docker inspect – format = ‘{{. NetworkSettings. IPAddress}}’ slave (127.0.0.3)
6. Enter the slave container
- Mysql -u root -p
- Change master to master_host=’172.17.0.2′, master_user=’slave’, master_password=’123456′, master_port=3306, master_log_file=’mysql-bin.000001′, master_log_pos= 617, master_connect_retry=30;
- Fields that
Master_port: indicates the port number of the Master. The port number of the container is master_user: indicates the user used for data synchronization. Master_password: indicates the password of the user used for data synchronization. Specifies the log File from which Slave will start replication, i.e., the value of File field master_LOG_POS: specifies the Position from which Slave will start replication, i.e., the value of Position field master_CONNECt_retry: If the connection fails, retry interval, in seconds. The default value is 60 secondsCopy the code
7. Slave Enables the master/slave replication
- Run show Slave status \G; This command is used to view the primary/secondary synchronization status
- Both SlaveIORunning and SlaveSQLRunning are No
- Start slave Enables the master/slave replication
- Show slave status \G;
8. Master/slave replication test
- Create a test database in master and generate a test database in Slave.