Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to get MySQL master-slave copy!

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Docker to get MySQL master-slave copy! I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

However, many friends report that it is difficult to install MySQL in Linux, and it is difficult to start from scratch, so today Songge will share with you how to quickly build MySQL master-slave replication through Docker.

About Docker

About Docker, Song GE will not say any more here. Song GE has had a Docker tutorial before. If you reply to Docker on the official account, you can get the download address of the tutorial.

Master-slave planning

First, plan two MySQL instances:

192.168.66.131VR 33061 / mainframe 192.168.66.131VR 33062 / Slave

Of course, you can prepare multiple slaves, and the configuration steps of the slaves are the same.

The command to create two MySQL instances in Docker is as follows:

Docker run-- name mysql1-p 33061 MYSQL_ROOT_PASSWORD=123 3306-e-d mysql:5.7-- character-set-server=utf8mb4-- collation-server=utf8mb4_unicode_ci

Docker run-- name mysql2-p 33062 MYSQL_ROOT_PASSWORD=123-d mysql:5.7-- character-set-server=utf8mb4-- collation-server=utf8mb4_unicode_ci

After the creation is completed, you can view the operation of the MySQL instance through the docker ps command:

This means that the MySQL instance is already running. The most convenient way to configure MySQL with Docker is that you can easily start from scratch with the wrong configuration.

Next, we will start the master-slave configuration.

Configure the host

The configuration on the host is mainly in two places:

The first is to configure a slave login user and the second configuration to enable binlog.

For the MySQL instance created in Docker, there is only one user by default, that is, root. Here we need to go to the MySQL command line and assign it another user. In the host, use the following command to connect to the host:

Mysql-u root-h 192.168.66.131-P 33061-p

After entering the password, go to the command line of the host. Then assign users to the slave machine (since MySQL is also installed on my host machine, you can directly execute the mysql command. If the host machine does not have MySQL installed, it is recommended to enter the MySQL container through docker exec, and then execute the following command):

GRANT REPLICATION SLAVE ON *. * to 'rep1'@'%' identified by' 123'

This means that the slave will use rep1/123 to log in to the host,% means that the account can be logged in from any address, or it can be given a fixed IP, indicating that the account can only log in from a certain IP.

Next, open binlog.

To enable binlog, you need to modify the configuration of MySQL, so we need to go inside the container to execute it.

First enter the inside of the container:

Docker exec-it mysql1 / bin/bash

Then locate the location of the MySQL configuration file:

/ etc/mysql/mysql.conf.d/mysqld.cnf

This is the configuration file for MySQL. We are going to modify it here. Because there is no VI editor by default in the MySQL container, it is difficult to install, so we can write the configuration file in the host, and then copy it to the MySQL container to overwrite the original configuration. We mainly add the following to the configuration file:

Log-bin=/var/lib/mysql/binlog

Server-id=1

Binlog-do-db = cmdb

The first line indicates the location where the binlog is configured. Theoretically, the binlog can be placed anywhere, but the MySQL must have permission to operate at that location. Server-id represents the unique identifier of each instance in the cluster. Bindlog-do-db indicates which databases to synchronize. When the slave is connected to the host, not every library in the host needs to be synchronized, which indicates which libraries need to be synchronized.

After the configuration is complete, save and exit.

Next, execute the command to copy the mysqld.cnf from the host to the container:

Docker cp. / mysqld.cnf mysql1:/etc/mysql/mysql.conf.d/

After the copy is complete, restart the container.

Docker restart mysql1

After the container is restarted, go to the command line of the host to check whether the configuration is successful:

File and Position need to remember that these two mark the starting point of the binary log, and these two parameters will be used in the slave configuration.

At this point, the configuration of the host is complete.

Configure Slave

The configuration of the slave machine is relatively simple. You don't need to open binlog or configure the library to be synchronized. You just need to add a server-id to the configuration file.

This is the mysqld.cnf configuration of the slave:

After the configuration is completed, copy the same to the container. Copy in the same way as the host:

Docker cp. / mysqld.cnf mysql2:/etc/mysql/mysql.conf.d/

After the configuration is complete, restart the slave container:

Docker restart mysql2

After the restart is completed, go to the command line of mysql2 and execute the following command to enable data synchronization:

Change master to master_host='192.168.66.131',master_port=33061,master_user='rep1',master_password='123',master_log_file='binlog.000001',master_log_pos=154

After the configuration is complete, start the slave process. Execute the following command from the command line:

Start slave

Next, execute show slave status\ G; check the slave status:

Here we focus on Slave_IO_Running and Slave_SQL_Running, both of which must have a value of Yes. If the value of one is not Yes, the configuration fails. In general, if the configuration fails, there will be a failure prompt below.

At this point, our MySQL master and slave is configured successfully.

Inspection

After the configuration is successful, we can connect our two MySQL instances through tools such as Navicat or SQLyog, and then create a library called db1 in the host, and you will find that the library will be automatically synchronized from the slave.

After reading the above, you know how to use Docker to get MySQL master-slave copy! Is there a way to do it? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report