search
HomeDatabaseMysql TutorialDetailed introduction on how to use docker to quickly build a MySQL master-slave replication environment

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:


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/

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


-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

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


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)!


Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)