Why build based Docker?
- Limited resources
- Virtual machine set up on the machine configuration is required, and a complicated step to install mysql
- A plurality of containers can be run on a single machine Docker
- Docker containers are independent, separate IP, do not conflict
- Docker use simple steps to start the second level in the container
Structures from the master server using Docker
First pull docker mirror, we here use the 5.7 version of mysql:
docker pull mysql:5.7
This image is then used to start the container, where the need to start from two separate containers master
Master (main):
docker run -p 3339:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
Slave (from):
docker run -p 3340:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
Foreign Port Master map is 3339, the external port Slave mapping is 3340. Because docker containers are independent of each other, each container has its own independent IP, so a different container using the same port and will not conflict. Here we should try to use mysql default port 3306, or may not appear in the issue by ip connection docker container mysql.
Use docker ps
the command to view the containers are running:
At this time, other tools can be used to test the connection mysql Navicat
Configuration Master (Main)
By docker exec -it 627a2368c865 /bin/bash
command Master into the interior of the container, you can also docker exec -it mysql-master /bin/bash
command to enter. 627a2368c865 is the id of the container, and mysql-master is the name of the vessel.
cd /etc/mysql
Switch to the under / etc / mysql directory, and then vi my.cnf
to edit my.cnf. At this point will be reported out bash: vi: command not found
, we need to install vim themselves inside the docker container. Use apt-get install vim
command to install vim
It will appear the following questions:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
Execution apt-get update
, and then executed again apt-get install vim
to successfully install vim. Then we can use vim to edit my.cnf, add the following configuration in the my.cnf:
[mysqld]
## 同一局域网内注意要唯一
server-id=100
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
After configuration is complete, need to restart the mysql service configuration to take effect. Use service mysql restart
complete restart. Will make the docker container to stop the restart mysql service, we also need to docker start mysql-master
start the container.
The next step is created in the Master database data synchronization user, the user grants permission slave REPLICATION SLAVE and REPLICATION CLIENT privilege for the primary synchronization between the data from the database.
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Configuring the Slave (from)
And configuring Master (primary) as added to the configuration file follows the my.cnf Slave:
[mysqld]
## 设置server_id,注意要唯一
server-id=101
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
After the configuration and the need to restart the mysql service operation and configuration of the container docker Master (primary) consistent.
Link Master (primary) and Slave (from)
In the Master into the mysql, executeshow master status;
Behind the value of the File and Position fields will be used, before the latter operation is completed, the need to ensure that Master library can not do anything, otherwise it will cause a state change, changes in the value of File and Position fields.
In Slave enter mysql, executechange 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= 2830, master_connect_retry=30;
Command Description:
MASTER_HOST : Master address, referring to the independent ip container, by docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器id
ip query container
MASTER_PORT : Master port number, port number refers to a container
MASTER_USER : user data for synchronization
master_password : used to synchronize the user's password
MASTER_LOG_FILE : Specifies Slave start copying data from which log file, i.e., File field value in the above-mentioned
master_log_pos value from which to start reading Position, Position i.e. the above-mentioned fields:
master_connect_retry : If the connection fails, retry interval, in seconds, default is 60 seconds
Mysql Slave is performed in the terminal show slave status \G;
for viewing master-slave synchronization state.
Under normal circumstances, SlaveIORunning and SlaveSQLRunning are No, because we have not turned on the master-slave replication process. Use start slave
open master-slave replication process, and then query the master-slave synchronization status again show slave status \G;
.
SlaveIORunning and SlaveSQLRunning are Yes, instructions from the master copy has been opened. At this point you can test data synchronization is successful.
Master-slave replication troubleshooting:
Use start slave
open master-slave replication process, if SlaveIORunning been the Connecting, then the master-slave replication has been connected, this situation is generally caused by several reasons below, we can be excluded in accordance with Last_IO_Error tips.
-
network issue
Check ip, port
-
Wrong password
Check if created for synchronizing users and user password is correct
-
not pos
Check the Master of Position
Test master-slave replication
Test master-slave replication will be very much, the simplest is to create a database Master, Slave and then check whether this database.
Master:
Slave:
Why build based Docker?
- Limited resources
- Virtual machine set up on the machine configuration is required, and a complicated step to install mysql
- A plurality of containers can be run on a single machine Docker
- Docker containers are independent, separate IP, do not conflict
- Docker use simple steps to start the second level in the container
Structures from the master server using Docker
First pull docker mirror, we here use the 5.7 version of mysql:
docker pull mysql:5.7
This image is then used to start the container, where the need to start from two separate containers master
Master (main):
docker run -p 3339:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
Slave (from):
docker run -p 3340:3306 --name mymysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
Foreign Port Master map is 3339, the external port Slave mapping is 3340. Because docker containers are independent of each other, each container has its own independent IP, so a different container using the same port and will not conflict. Here we should try to use mysql default port 3306, or may not appear in the issue by ip connection docker container mysql.
Use docker ps
the command to view the containers are running:
At this time, other tools can be used to test the connection mysql Navicat
Configuration Master (Main)
By docker exec -it 627a2368c865 /bin/bash
command Master into the interior of the container, you can also docker exec -it mysql-master /bin/bash
command to enter. 627a2368c865 is the id of the container, and mysql-master is the name of the vessel.
cd /etc/mysql
Switch to the under / etc / mysql directory, and then vi my.cnf
to edit my.cnf. At this point will be reported out bash: vi: command not found
, we need to install vim themselves inside the docker container. Use apt-get install vim
command to install vim
It will appear the following questions:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package vim
Execution apt-get update
, and then executed again apt-get install vim
to successfully install vim. Then we can use vim to edit my.cnf, add the following configuration in the my.cnf:
[mysqld]
## 同一局域网内注意要唯一
server-id=100
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
After configuration is complete, need to restart the mysql service configuration to take effect. Use service mysql restart
complete restart. Will make the docker container to stop the restart mysql service, we also need to docker start mysql-master
start the container.
The next step is created in the Master database data synchronization user, the user grants permission slave REPLICATION SLAVE and REPLICATION CLIENT privilege for the primary synchronization between the data from the database.
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Configuring the Slave (from)
And configuring Master (primary) as added to the configuration file follows the my.cnf Slave:
[mysqld]
## 设置server_id,注意要唯一
server-id=101
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
After the configuration and the need to restart the mysql service operation and configuration of the container docker Master (primary) consistent.
Link Master (primary) and Slave (from)
In the Master into the mysql, executeshow master status;
Behind the value of the File and Position fields will be used, before the latter operation is completed, the need to ensure that Master library can not do anything, otherwise it will cause a state change, changes in the value of File and Position fields.
In Slave enter mysql, executechange 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= 2830, master_connect_retry=30;
Command Description:
MASTER_HOST : Master address, referring to the independent ip container, by docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名称|容器id
ip query container
MASTER_PORT : Master port number, port number refers to a container
MASTER_USER : user data for synchronization
master_password : used to synchronize the user's password
MASTER_LOG_FILE : Specifies Slave start copying data from which log file, i.e., File field value in the above-mentioned
master_log_pos value from which to start reading Position, Position i.e. the above-mentioned fields:
master_connect_retry : If the connection fails, retry interval, in seconds, default is 60 seconds
Mysql Slave is performed in the terminal show slave status \G;
for viewing master-slave synchronization state.
Under normal circumstances, SlaveIORunning and SlaveSQLRunning are No, because we have not turned on the master-slave replication process. Use start slave
open master-slave replication process, and then query the master-slave synchronization status again show slave status \G;
.
SlaveIORunning and SlaveSQLRunning are Yes, instructions from the master copy has been opened. At this point you can test data synchronization is successful.
Master-slave replication troubleshooting:
Use start slave
open master-slave replication process, if SlaveIORunning been the Connecting, then the master-slave replication has been connected, this situation is generally caused by several reasons below, we can be excluded in accordance with Last_IO_Error tips.
-
network issue
Check ip, port
-
Wrong password
Check if created for synchronizing users and user password is correct
-
not pos
Check the Master of Position
Test master-slave replication
Test master-slave replication will be very much, the simplest is to create a database Master, Slave and then check whether this database.
Master:
Slave: