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 build mysql Master-Slave replication in docker

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

Share

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

Today, I will talk to you about how to build mysql master-slave replication in docker. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Install mysql through docker, and I choose version 5.6 of yes here.

1) search for mysql image from docker

Docker pull mysql:5.6

2) use this image to start two mysql containers (master and slave containers)

Master (Master)

Docker run-p 3316 MYSQL_ROOT_PASSWORD=123456 3306-- name master-mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.6

Slave (from)

Docker run-p 3317 MYSQL_ROOT_PASSWORD=123456 3306-- name slave-mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.6

Parameter resolution:

-p maps port 3306 of the container to port 3306 of the host.

-- name uses docker to image mysql:5.6 and name the container mastermysql.

-e-e MYSQL_ROOT_PASSWORD=123456 initialization password.

-d runs the container in the background and returns the container ID.

Check whether the container is working properly: docker ps

2. Configure mysql master server (Master)

1) enter inside the mysql master server

Docker exec-it e8355163bbbc / bin/bash

2) Edit my.cnf

Cd / etc/mysql

Edit my.cnf (vim my.cnf)

Note: you may be prompted that vim is not installed, you can run apt-get install vim installation, and then execute vim my.cnf

Add at the end

[mysqld]

Server-id=100

Log-bin=mysql-bin

You need to restart mysql after configuration, and the docker image will stop during the restart process, so you also need to restart the image.

Service mysql restart

Docker start mysql-master

3) the next step is to create a data synchronization user in the Master database and grant the user slave REPLICATION SLAVE permission and REPLICATION CLIENT permission to synchronize data between master and slave libraries.

CREATE USER 'slave'@'%' IDENTIFIED BY' 123456'

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *. * TO 'slave'@'%'

Configure mysql slave server (Slave)

1) enter mysql from inside the server

Docker exec-it slave server id / bin/bash

2) Edit my.cnf

Cd / etc/mysql

Edit my.cnf (vim my.cnf)

Note: you may be prompted that vim is not installed, you can run apt-get install vim installation, and then execute vim my.cnf

Add at the end

[mysqld]

Server-id=101

Log-bin=mysql-slave-bin

Relay_log=edu-mysql-relay-bin

You need to restart mysql after configuration, and the docker image will stop during the restart process, so you also need to restart the image.

Service mysql restart

Docker start mysql-slave

IV. Other settings

1) Link Master (master) and Slave (slave)

Enter mysql at Master and execute show master status

The values of the File and Position fields will be used later. Before the later operation is completed, you need to ensure that the Master library cannot do anything, otherwise it will cause a state change, a change in the values of the File and Position fields.

2) enter mysql in Slave and execute

Change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 433, master_connect_retry=30

Command description:

Master_host: the address of Master, which refers to the independent ip of the container. You can query the container's ip via docker inspect-- format=' {{.NetworkSettings.IPAddress}} 'container name | Container id

The port number of master_port:Master, which refers to the port number of the container

Master_user: user for data synchronization

Master_password: password of the user used for synchronization

Master_log_file: specifies the log file from which Slave starts copying data, that is, the value of the File field mentioned above

Master_log_pos: which Position to read from, that is, the value of the Position field mentioned above

Master_connect_retry: if the connection fails, the interval between retries (in seconds). Default is 60 seconds.

3) execute show slave status\ G in the mysql terminal in Slave; used to view the master-slave synchronization status.

Normally, both SlaveIORunning and SlaveSQLRunning are No because we haven't started the master-slave replication process yet. Use start slave; to start the master-slave replication process, and then query the master-slave synchronization status show slave status\ G; again.

After reading the above, do you have any further understanding of how to build mysql master-slave replication in docker? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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