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

Mar 04, 2017 pm 02:51 PM

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


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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

How do containerization technologies (like Docker) affect the importance of Java's platform independence? How do containerization technologies (like Docker) affect the importance of Java's platform independence? Apr 22, 2025 pm 06:49 PM

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles