Home > Database > Mysql Tutorial > Detailed introduction on how to use docker to quickly build a MySQL master-slave replication environment

Detailed introduction on how to use docker to quickly build a MySQL master-slave replication environment

黄舟
Release: 2017-03-04 14:51:58
Original
1225 people have browsed it

In the process of learning MySQL, the effects of various parameters are often tested. At this time, you need to quickly build a MySQL instance, even a master-slave instance.

Consider the following scenario:

For example, I want to test the impact of mysqldump on the myisam table when the --single-transaction parameter is specified.

Originally I wanted to do it in a ready-made test environment, but in the test environment, there is a large amount of data. Execute mysqldump for full backup. The generated SQL file is difficult to search based on the table.

At this time, I am particularly eager to have a clean set of examples for testing.


At this moment, it is particularly necessary to quickly build capabilities. Many children may ask, can't it be achieved through scripts? Why use docker?

Personal feeling: The script is too heavy and will involve a lot of extra work, such as creating users, a relatively long database initialization process, and the MySQL startup process. What I need is the ability to build quickly and destroy quickly. .

And this is Docker’s strength.

The following is the time it takes to start an instance using docker, which is less than 1 second. If you use a script to do it, it will never be so fast.


# time  docker run --name slave -v /etc/slave.cnf:/etc/mysql/my.cnf -v //lib/mysql/slave://lib/mysql -p3307:-e MYSQL_ROOT_PASSWORD= -d mysql:
Copy after login


So I wrote a script based on docker, which can create a new MySQL master-slave replication environment in about 30 seconds

#!/bin/=/var/lib/mysql/=/var/lib/mysql/ - - - ---name master -v /etc/master.cnf:/etc/mysql/my.cnf -v $MASTER_DIR:/var/lib/mysql  
--net=host -e MYSQL_ROOT_PASSWORD= -d mysql:.--name slave -v /etc/slave.cnf:/etc/mysql/my.cnf -v $SLAVE_DIR:/var/lib/mysql --net=host -e 
MYSQL_ROOT_PASSWORD= -. -it master mysql -S /var/lib/mysql/mysql.sock -e LAVE ON *.* TO @;=`docker exec -it master mysql 
-S /var/lib/mysql/mysql.sock -e =`  |   =`  |  =-it slave mysql -S /var/lib/mysql/mysql.sock -e eplrepldocker exec -it slave mysql 
-S /var/lib/mysql/mysql.sock -e   /etc/ [ $? -eq  ];
      >> /etc/  >> /etc/  >> /etc//etc/
Copy after login

The script itself does not have much to explain. After the master-slave container is started, it still follows the common master-slave replication establishment process.

Mainly talk about 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
Copy after login


-v /etc/master.cnf:/etc/mysql/my.cnf: Maps the local configuration file to the container configuration file, so that it can be Modify the local configuration file to achieve the effect of modifying the container configuration file.

-v $MASTER_DIR:/var/lib/mysql: Map the local directory to the container’s data directory. This makes it easy to view the contents of the data directory. Otherwise, it will be saved in /var/lib/docker by default. /volumes directory, it is really inconvenient to view.

--net=host: Sharing the host's network greatly reduces the complexity of communication between containers.

Note

When the script first starts, the previous container will be deleted. This involves a two-step operation

1. Delete the container through the docker command

2. Delete the data directory of the previous container through the operating system command.

If you do not delete it, when you create a container through the following command again, the previous data directory will not be cleared, but will be loaded directly, which is equivalent to starting a new instance before the mysqld process is started.


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
Copy after login

This also provides us with an idea. If you just want to test the effect of parameters and do not want to create a new instance, you only need to delete the container through the docker command and modify the configuration file. , just create the container through the above command.

After starting the instance, we performed an operation to restart the instance, because during the test, we found that if we perform operations such as docker exec -it master bash, the container will go down (the specific reason for down Not analyzed yet), but there will be no problem after restarting the instance.

docker stop master slave
docker start master slave

sleep 3
Copy after login


Set shortcut keys

mysql: mysql client, you can connect to other clients through this client MySQL server on the host machine.

master: Execute master to log in to the local master instance, eliminating the need to specify the host name and port.

salve: Execute slave to log in to the local slave instance.

The above is the detailed introduction on how to use docker to quickly build a MySQL master-slave replication environment. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template