In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to build a master-slave replication environment based on MySQL in Docker. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1. Preface
The previous program architecture might be in this form:
When the volume of the program expands, we may expand multiple background service instances, but there is still only one database, so the bottleneck of the system is still on the database, so the main task this time is to expand the database. The main form is to expand multiple database instances to achieve read-write separation, and assign some write tasks to the main database. Use a subdatabase to read the read task. So as to improve the performance of the system.
The modified schema is as follows:
two。 Environmental pre-construction
This time, docker is used to build this environment, using MySQL version 5.7.13.
Docker pull mysql:5.7.13
The overall structure is as follows:
1 master master node as the write node.
2 slave slave nodes as read nodes.
First, start these nodes and map them to different ports. Use the database connection tool to connect on this machine to test whether it starts normally and connects normally.
Docker run-p 3307 mysql:5.7.13docker run 3306-- name mysql-master-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7.13docker run-p 3308 mysql:5.7.13docker run 3306-- name mysql-slave1-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7.13docker run-p 3309 Vera 3306-- name mysql-slave2-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7.13
Here I map the master node (mysql-master) to port 3307 and the two slave nodes (mysql-slave1,2) to port 3308 and 3309 respectively. Then set the root password for MySQL to 123456.
You can then use tools such as navicat to connect and test the MySQL.
Enter each of these nodes and edit the configuration file.
Docker exec-it mysql-master / bin/bash
I use name to enter the container, or I can choose according to id, that is, docker exec-it corresponds to the id / bin/bash of the container.
Since the vi and vim programs are not pre-installed, and then you need to execute the apt update command to download it, it will be downloaded from a foreign source. Due to well-known reasons, the speed is very slow. I will change the download source to the domestic source.
Go to the / etc/apt folder and first back up the original files:
Mv sources.list sources.list.bak
Then use the following command to create a new file and enter the contents:
Echo deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse > sources.list
Then we perform operations such as apt update, and finally install vim.
3. Master-slave configuration
Master node (master) configuration
After entering the master node container, go to the / etc/mysql folder, there will be a my.cnf file, mainly to modify it.
Edit the file, find [mysqld], and add the following command below:
[mysqld]. # # unique number server-id=101## this is the key configuration item log-bin=mysql-bin
After the configuration is complete, you need to restart the MySQL service for the configuration to take effect. Use the service mysql restart command to restart. After the restart is completed, the MySQL container will be closed. We also need to restart the container docker restart mysql-master.
Slave node (slave) configuration
Like the master node, edit the / etc/mysql/my.cnf file
[mysqld]. # # unique numbered server-id=103## selection. If you need to use this node as the master node of another node, you need to add # log-bin=mysql-bin
Link master node and slave node
Primary node
Enter MySQLmysql-u root-p in the master node container, and the password is 123456 set when the container is started.
Execute show master status;: after entering MySQL
From here we get the values of File and Position, which I have here are mysql-bin.000001 and 154, respectively.
Slave node
Enter MySQL and execute the following command:
Change master to master_host='***', master_port=3306, master_user='root', master_password='123456', master_log_file='****', master_log_pos= * *
Explain what these parameters represent:
Master_host: the ip address of the master node. You can view the ip address of the container by using the following command on the local machine
Docker inspect-- format=' {{.NetworkSettings.IPAddress} 'Container name | Container id
The port number of master_port:mysql, which is not the port number of external mapping
For users in master_user:mysql, if you want to have permission, I directly use root, or you can create a new user to use
Master_password: mysql account password for synchronization
Master_log_file: the file used for synchronization, that is, the file queried from the master node. Here is mysql-bin.000001.
The location where the master_log_pos:binlog file starts to be synchronized is the location queried from the master node.
Execute show slave status\ G; on the MySQL terminal after executing the command just now to check the master-slave synchronization status.
We can check the configuration information from here, and then we can see that the two properties slave_io_running and slave_sql_running are both no, that is, the closed state.
We can execute start slave to turn on master-slave replication, and execute show slave status\ G again after execution; if the command can see that both attributes become yes, master-slave replication has been enabled.
If the startup is not successful, we can check whether the network is connected, whether the mysql password used for synchronization is correct, and whether the name and location of the synchronization file are correct!
test
We can create a new database in the master library, and if we see the existence of this library in the slave library, it means that the master-slave synchronization is complete.
4. Cascade configuration
I want to add another backup node, and this node is backed up from the slave1 node, that is, the slave1 node is the primary node of the backup node. This forms a cascading relationship of master- > slave- > backup.
I originally followed the steps above to add it to the my.cnf of slave.
Log-bin=mysql-slave-bin # in order to distinguish, I changed the file name
Then executed on the backup node
Change master to master_host='***', master_user='root', master_password='123456', master_port=3306, master_log_file='****', master_log_pos= * *
The command is replaced with attributes such as ip of the corresponding slave node. Turns out it didn't work. After the primary node has changed, the backup node has not changed!
So I started troubleshooting and found that the binlog file in the slave node has no record of changing information, while the backup node is listening for the file to change, and the file has not changed, so the backup node will not change. As an extension, mysql's binlog records all our changes, so in theory we can use binlog to recover the database content at any time.
So the problem becomes how to record the binlog log of the slave node after the master node changes.
We can add another line when editing the my.cnf file: log_slave_updates=1, so that slave can also write the binary log to its own binlog after receiving the master synchronization.
This can be done, after the master node changes, both the slave node and the backup node will change, and the data of the backup node will be backed up from the slave node.
This is the end of the article on "how to build a MySQL master-slave replication environment based on Docker". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.