This building environment

Docker 18.06, 1 – ce
Centos7
1. Pull the mysql5.7 image
Docker pull mysql: 5.7Copy the code
2. Bin-log is required to enable primary and secondary mysql, which is not enabled by default. Create the mysql configuration file my.conf and mount it to the container to enable this function
The contents of my.conf are as follows. You only need to change the server-ID to configure the secondary node
Log-bin = /var/ log/mysql.mysql/mysql.bin. log # bin-log = /var/ log/mysql.log-bin = /var/ log/mysql.bin. log # bin-log = /var/ log/mysql.bin.log binlog.index #[Err]1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains #nonaggregated column 'information_schema.profiling.SEQ' which is not functionally #dependent on columns in GROUP BY clause; This is incompatible with # SQL_mode =only_full_group_by # The preceding error occurs and sql_mode needs to be modified sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESCopy the code
3. Run the active node
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -v /home/mysql/mastar-data:/var/lib/mysql -v / home/mysql/master - the conf/my CNF: / etc/mysql/my. CNF -- -- the name mysql - master} {mysql mirror idCopy the code

-d Background running

-p Port mapping Host port: container port

-e Sets environment variables, myQL5.7 Login password must be specified during initialization

-v Host mount path: container path,mysql5.7 Default storage location /var/lib/mysql

–name Specifies the container name

4. Access the master container and log in to mysql in the container
Docker exec it {container ID} /bin/bash mysql -u root -pCopy the code
5. View the information about the primary node
show master status;
Copy the code
The following information is displayed:

Records the value of the File field, which is specified later when the secondary node connects to the primary node
6. Run the secondary node
docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -v /home/mysql/slave-data:/var/lib/mysql -v /home/mysql/slave-conf/my.cnf:/etc/mysql/my.cnf --name mysql-slave 2c9028880e58
Copy the code
Similar to mater, go inside the slave container and log in to mysql
Run the following command in the container to connect the master and enable the slave mode
CHANGE MASTER TO MASTER_HOST = '127.0.0.1, MASTER_USER =' root ', MASTER_PASSWORD = 'root', MASTER_LOG_FILE = '000001' mysql - bin., MASTER_LOG_POS = 0; start slave;Copy the code

MASTER_HOST IP address of the primary node

MASTER_USER User name of the primary node

MASTER_PASSWORD Password of the primary node

MASTER_LOG_FILE Specifies the bin-log file name of the primary node

MASTER_LOG_POS starts the cursor,mysql will automatically match

7. Check the slave status in the container
 show slave status\G;
Copy the code
The following figure shows that the association is created successfully