How to quickly build MySQL master-slave replication environment with Docker

Source: Internet
Author: User
Tags mysql client docker run

In the process of learning MySQL, the role of various parameters is often tested. At this point, you need to quickly build MySQL instances, even master-slave.

Consider the following scenario:

For example, I want to test the effect of mysqldump on the MyISAM table in the case of specifying the--single-transaction parameter.

Originally wanted to do in the ready-made test environment, but in the test environment, there is a lot of data, the execution of mysqldump to fully prepared, the resulting SQL file, it is difficult to search based on the table.

At this time, it is particularly eager to have a clean set of examples to test.

At the moment, the ability to build quickly is particularly necessary, and many children's shoes may ask, can not be achieved through the script? Why use Docker?

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

And that's what Docker's strengths are.

Below, it is time to use Docker to start an instance, less than 1s, and 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:-E mysql_root_password=123456 -D MySQL:5.6. 34
6b7fe5da9e8c77529ee634e163add57db5cd15757e88261ce320a502ae01f853real 0m0.986suser 0m0.026ssys 0m0.018s

So, based on Docker, wrote a script to create a new MySQL master-slave replication environment around 30s

#!/bin/Bashmaster_dir=/var/lib/mysql/Masterslave_dir=/var/lib/mysql/slave## First we couldRMThe existed ContainerdockerRM-F MasterdockerRM-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. theDocker 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. the# # Creating a User forreplicationdocker 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 ($ 1)}'' Master_log_pos=`Echo "$master _status"|awk 'Nr==3{print $}'' 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[$?-eq1]; Then Echo 'alias mysql= "Docker exec-it master MySQL"'>>/etc/ ProfileEcho 'alias master= "Docker exec-it master mysql-h 127.0.0.1-p3306"'>>/etc/ ProfileEcho '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 explain, the master-slave container up, followed by the common master-slave replication process.

The main talk is about the options involved in creating the 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=-D MySQL:5.6.  the

-V/ETC/MASTER.CNF:/ETC/MYSQL/MY.CNF: Maps a local configuration file to a container's configuration file, so that you can modify the local configuration file to achieve the effect of modifying the container configuration file.

-V $MASTER _dir:/var/lib/mysql: Maps a local directory into a container's data directory, which makes it easy to view the contents of the data directory, otherwise, it is not easy to view by default in the/var/lib/docker/volumes directory.

--net=host: Share host network, greatly reduce the communication complexity between containers.

Attention

At the beginning of the script, the previous container is deleted, which contains a two-step operation

1. Removing a container from 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 the container again by using the following command, it does not empty the previous data directory, but instead loads it directly, equivalent to a new instance of the MYSQLD process before it 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.  the

This also gives us a way of thinking, if you just want to test the role of parameters, and do not want to create a new instance, simply remove the container through the Docker command, modify the configuration file, the above command to create a container.

After launching the instance, a reboot instance was performed, because during the test, it was found that performing operations such as Docker exec-it Master Bash would cause the container to be down (not yet analyzed for specific reasons), but it would be fine after restarting the instance.

3

Set shortcut keys

The Mysql:mysql client can connect to the MySQL server on other hosts through the client.

Master: Executes master to log on to the master instance of this computer, eliminating the operation of specifying the hostname and port.

Salve: Execute slave to log on to the native slave instance.

How to quickly build MySQL master-slave replication environment with Docker

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.