How to Use docker to quickly build a MySQL master-slave replication environment, dockermysql
When learning MySQL, we often test the functions of various parameters. At this time, you need to quickly build a MySQL instance, or even Master/Slave.
Consider the following scenarios:
For example, I want to test the impact of mysqldump ON THE myisam table when the -- single-transaction parameter is specified.
I originally wanted to do this in a ready-made test environment. However, in the test environment, a large amount of data is stored, and mysqldump is executed for full backup. The generated SQL file is hard to be searched based on tables.
At this time, we are particularly eager to have a set of clean instances for testing.
At this moment, the ability to quickly build is particularly necessary. Many children's shoes may ask, can it be achieved through scripts? Why docker?
My personal feeling: the script is too heavy and involves a lot of extra work, such as creating a user, a relatively long database initialization process, and a MySQL startup process. What I need is a fast build, fast destruction capability.
This is exactly the strength of Docker.
The following is how long it takes to start an instance using docker, less than 1 s. If you use a script, it will never be so fast.
# time docker run --name slave -v /etc/slave.cnf:/etc/mysql/my.cnf -v /var/lib/mysql/slave:/var/lib/mysql -p3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6.34
6b7fe5da9e8c77529ee634e163add57db5cd15757e88261ce320a502ae01f853real 0m0.986suser 0m0.026ssys 0m0.018s
Therefore, a script is written based on docker. You can create a MySQL master-slave replication environment in about 30 s.
#!/bin/bashMASTER_DIR=/var/lib/mysql/masterSLAVE_DIR=/var/lib/mysql/slave## First we could rm the existed containerdocker rm -f masterdocker rm -f slave## Rm the existed directoryrm -rf $MASTER_DIRrm -rf $SLAVE_DIR## Start instancedocker run --name master -v /etc/master.cnf:/etc/mysql/my.cnf -v $MASTER_DIR:/var/lib/mysql --net=host -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6.34docker run --name slave -v /etc/slave.cnf:/etc/mysql/my.cnf -v $SLAVE_DIR:/var/lib/mysql --net=host -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6.34## Creating a User for Replicationdocker stop master slavedocker start master slavesleep 3docker exec -it master mysql -S /var/lib/mysql/mysql.sock -e "CREATE USER 'repl'@'127.0.0.1' IDENTIFIED BY 'repl';GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1';"
## Obtaining the Replication Master Binary Log Coordinatesmaster_status=`docker exec -it master mysql -S /var/lib/mysql/mysql.sock -e "show master status\G"`master_log_file=`echo "$master_status" | awk 'NR==2{print substr($2,1,length($2)-1)}'`master_log_pos=`echo "$master_status" | awk 'NR==3{print $2}'`master_log_file="'""$master_log_file""'"## Setting Up Replication Slaves docker exec -it slave mysql -S /var/lib/mysql/mysql.sock -e "CHANGE MASTER TO MASTER_HOST='127.0.0.1',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE=$master_log_file,MASTER_LOG_POS=$master_log_pos;"docker exec -it slave mysql -S /var/lib/mysql/mysql.sock -e "start slave;"docker exec -it slave mysql -S /var/lib/mysql/mysql.sock -e "show slave status\G"## Creates shortcutsgrep "alias master" /etc/profileif [ $? -eq 1 ];then echo 'alias mysql="docker exec -it master mysql"' >> /etc/profile echo 'alias master="docker exec -it master mysql -h 127.0.0.1 -P3306"' >> /etc/profile echo 'alias slave="docker exec -it master mysql -h 127.0.0.1 -P3307"' >> /etc/profile source /etc/profilefi
The script itself does not have much to note. After the master-slave container is started up, it follows the common master-slave replication creation process.
It mainly describes the options involved in creating a container.
docker run --name master -v /etc/master.cnf:/etc/mysql/my.cnf -v $MASTER_DIR:/var/lib/mysql --net=host -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6.34
-V/etc/master. cnf:/etc/mysql/my. cnf: maps the local configuration file to the container configuration file. In this way, you can modify the local configuration file to modify the container configuration file.
-V $ MASTER_DIR:/var/lib/mysql: map the local directory to the data directory of the container, so that you can conveniently view the content of the data directory. Otherwise, it is saved in the/var/lib/docker/volumes directory by default, which is inconvenient to view.
-- Net = host: The network of the shared host, greatly reducing the complexity of communication between containers.
Note:
At the beginning of the script, the previous container is deleted, which includes two steps.
1. Run the docker command to delete the container.
2. Run the operating system command to delete the data directory of the previous container.
If it is not deleted, the following command is used again to create a container, instead of clearing the previous data directory, but loading directly, which is equivalent to a new instance before the mysqld process starts.
docker run --name master -v /etc/master.cnf:/etc/mysql/my.cnf -v $MASTER_DIR:/var/lib/mysql --net=host -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6.34
This also provides us with an idea. If you only want to test the function of parameters and do not want to create an instance, you only need to run the docker command to delete the container and modify the configuration file, use the preceding command to create a container.
After the instance is started, a restart operation is executed, because during the test, if you execute operations such as docker exec-it master bash, this will cause the container to go down (the reason for the specific down is not analyzed yet), but it will be okay after the instance is restarted.
docker stop master slavedocker start master slavesleep 3
Set shortcuts
Mysql: mysql client, which can be used to connect to MySQL servers on other hosts.
Master: Execute the master command to log on to the master instance of the Local Machine, saving the operation of the specified host name and port.
Salve: Execute the slave command to log on to the local slave instance.