In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to build MySQL master-slave cluster in Docker? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. Environmental preparation
Before building a master-slave cluster, we need to install a docker server on this machine. For specific installation steps, please see the installation of https://www.docker.com/ download service on docker's official website. After the installation is complete, the following command can display normally, which means that the installation is successful:
$docker-- versionDocker version 18.09.0, build 4d60db42. Master-slave cluster building 2.1 master server preparation
After installing docker, we first run a master container by executing the following command:
Docker run-- name mysql-master-- privileged=true-v / home/mysql/master-data:/var/lib/mysql-p 3306 privileged=true 3306-e MYSQL_ROOT_PASSWORD=root-d xiaochunping/mysql-master
After executing the above command, docker first detects whether there is a target image locally, that is, xiaochunping/mysql-master, if not, it downloads the image, and then runs it according to the configured parameters. The meanings of the parameters in the above command are as follows:
-- name specifies that the name of the container after running is mysql-master
-- privileged specifies whether the current container really has root permission. The so-called root permission refers to the root permission of the host, not just the root permission inside the container.
-v specifies that the specified directory in the container is mounted to a directory on the host. The purpose of this is to prevent the data configured in the container from being lost, because the docker container will not retain the relevant data that was previously run inside it after restart.
-p means that a port on the host is mapped to a port in the docker container, that is, port 3306 of the host is mapped to port 3306 inside the container.
-e indicates the environment variable that specifies the current container to run, which is typically used in the configuration file of the container's internal program, while the external running container specifies this parameter. The MYSQL_ROOT_PASSWORD here represents the startup password of the MySQL inside the container
The-d parameter specifies that the current container is running in the background.
After the master container is started, we can see the running container through the docker ps command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfa4afaeb24c4 xiaochunping/mysql-master "docker-entrypoint.s..." 12 days ago Up About an hour 0.0.0.0 3306-> 3306/tcp mysql-master
As you can see, the master container has started normally, and then we need to go inside the container and configure the master node. You can enter the container with the following command:
# through the container id method, the id here is the iddocker exec-it fa4afaeb24c4 / bin/bash# shown in the above docker ps by container name, and the name here is the name specified when the container is created, that is, mysql-masterdocker exec-it mysql-master / bin/bash
After entering the container, we need to connect to its MySQL service:
# the password here is the password specified when the container was created, mysql-uroot-proot
Then we need to create an account for the slave server that can be used for the master server, that is, to create an account specifically used to copy binlog, and give the account replication permission, with the following command:
Grant replication slave on *. * to 'test'@'%' identified by' 123456
The grant replication slave here is a command format that gives subsequent accounts permission to copy so that the slave node can get updates to the data from the master node. The account created in the above command has a user name of test and a password of 123456. Next we need to look at the binlog status of the master node:
Mysql > show master status +-+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +- -+ | mysql-bin.000014 | 154 | +- -+ 1 row in set (0.00 sec)
You need to remember the values of the File and Position properties here, because you need to use them when configuring slave below.
2.2 slave server preparation
For the use of slave containers, the startup command is very similar to the master container, but with some small differences:
Docker run-- name mysql-slave-- privileged=true-v / home/mysql/slave-data:/var/lib/mysql-p 3307 privileged=true 3306-- link mysql-master:master-e MYSQL_ROOT_PASSWORD=root-d xiaochunping/mysql-slave
There are two main startup parameters of slave container and master container:
The port number of the mapped host cannot be the same as the master container, because it is already occupied by the master container
You must add the-- link parameter, and then specify the container to which the current container is connected. Mysql-master represents the name of the container to be connected, and master represents an alias for the container. Generally speaking, the slave container can access the master container through these two names. The reason for this is that if master and slave are not in the same docker network, the two containers are inaccessible to each other. It is very important to note that I have not been able to successfully build the master-slave server according to the way I built it on the Internet, mainly because they have not mentioned to set this parameter.
After starting the slave container, we still need to enter the slave container and connect to the MySQL service:
Docker exec-it mysql-slave / bin/bashmysql-uroot-proot
After connecting to the MySQL server, we need to switch the state of the current service so that it can connect to the master server and copy its data:
Change master to master_host='master', master_user='test', master_password='123456', master_port=3306, master_log_file='mysql-bin.000014', master_log_pos=154, master_connect_retry=30
In the above command, the meaning of each parameter is easy to understand, mainly to specify the domain name of the master service (that is, the alias we used in the previous link), the user name, password, port, as well as the binlog file of the master node and the position of the log. The binlog file here and the position of log are the results of our previous show master status; command, and readers need to be consistent with the actual results of this machine. Finally, we need to enable master-slave replication:
Start slave
After enabling it, we can check the connection status of the slave server with the following command:
Mysql > show slave status\ G * * 1. Row * Slave_IO_State: Waiting for master to send event Master_Host: master Master_User: test Master_Port: 3306 Connect_Retry: 30 Master_Log_File: mysql-bin.000014 Read_Master_Log_Pos: 154 Relay_Log_File: 6774ae81bc25-relay-bin.000034 Relay_Log_Pos: 367 Relay_Master_Log_File: mysql-bin.000014 Slave_IO_Running: Yes Slave_SQL_Running: Yes.... Master_Server_Id: 1 Master_UUID: 8976b929-bc8b-11e9-bab3-0242ac110005 Master_Info_File: / var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log Waiting for more updates Master_Retry_Count: 86400 Master_Bind:.... 1 row in set (0.00 sec)
As long as we see that the two parameters Slave_IO_Running and Slave_SQL_Running are both true, the replication is normal, and our master-slave structure is completed.
3. Test master-slave service
Testing the master-slave service is relatively simple, you only need to execute the relevant commands on the master server, and then check to see if there are corresponding updates on the slave server. We can create a database and table by executing the following command on the master node, and then insert a piece of data:
Mysql > create database test;Query OK, 1 row affected (0.01sec) mysql > use test;Database changedmysql > create table t_user (id bigint, name varchar); Query OK, 0 rows affected (0.02sec) mysql > insert into t_user (id, name) value (1, 'Mary'); Query OK, 1 row affected (0.01sec)
Then we connect to the slave database to see if there are any relevant updates:
Mysql > select * from test.t_user;+-+-+ | id | name | +-+-+ | 1 | Mary | +-+-+ 1 row in set (0.00 sec)
As you can see, the relevant data is indeed synchronized from the server, which further verifies that the master-slave server we built is OK.
This is the answer to the question about how to build a MySQL master-slave cluster in Docker. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.