How to deploy MySQL master and slave in Docker

How to deploy MySQL master and slave in Docker

Download image

Selecting a MySQL Image

docker search mysql 

insert image description here

Download MySQL 5.7 image

docker pull mysql:5.7

View mysql image

docker images 

insert image description here

Build MySQL master-slave

master

docker run --name mysql-master -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

slave

docker run --name mysql-slave -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

The port number mapped to the outside by the Master is 3307, and the port number mapped to the outside by the Slave is 3308

View Container

docker ps 

insert image description here

Connection Test

insert image description here
insert image description here

Configuring the Master

Enter the container in two ways:

docker exec -it 1b166e12ad6b /bin/bash #1b166e12ad6b is the container id
docker exec -it mysql-master /bin/bash #mysql-master is the container name

Modify the /etc/mysql/my.cnf configuration file

vim /etc/mysql/my.cnf 

insert image description here

The vim command was not found. Install the vim tool inside docker. Enter the following two commands to install vim

apt-get update
apt-get install vim

After the installation is complete, modify the my.cnf configuration file

vim /etc/mysql/my.cnf

[mysqld]
## Note that the server-id=100 must be unique within the same LAN  
## Enable binary log function, you can take any log (key)
log-bin=master-bin
binlog-format=ROW // Binary log format, there are three types: row, statement, mixed

After configuration, restart MySQL

service mysql restart

Restarting will cause the Docker container to stop. Use the following command to restart the container

docker ps -a 

insert image description here

docker start mysql-master

Create a database synchronization account

Enter the mysql-master container

docker exec -it 1b166e12ad6b /bin/bash

Log in to MySQL and authorize the synchronization account for the slave host

mysql -uroot -p123456

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; 

insert image description here

At this point the master configuration is complete

Slave

Use the command similar to the master to enter the container

docker exec -it mysql-slave /bin/bash

Modify the my.cnf configuration file and remember to install the vim command

vim /etc/mysql/my.cnf

[mysqld]
## Set server_id, note that it must be unique server-id=101  
## Enable binary logging in case the slave is used as the master of other slaves log-bin=mysql-slave-bin   
## relay_log configuration relay log relay_log=mysql-relay-bin  
read_only=1 ## Set to read-only. If this item is not set, it means that the slave can read and write

Restart MySQL

service mysql restart

Start the container

docker start mysql-slave

Enable Master-Slave replication

First, open two terminals and enter the master and slave containers and enter MySQL

mysql-master operations

mysql -uroot -p123456
show master status; 

insert image description here

mysql-slave operation

Note: Remember to change master_log_file='', master_log_pos= to the results you viewed on the master machine

mysql -uroot -p123456

change master to master_host='10.0.3.2', master_user='slave', master_password='123456', master_port=3307, master_log_file='master-bin.000001', master_log_pos=617;

start slave;

show slave status \G; 

insert image description here

Test success

mysql-master operations

create database dockertest; 

insert image description here

mysql-slave operation

insert image description here

The database created on the master appears on the slave, proving success.

Article reference link

For detailed information, please refer to

This is the end of this article about how to deploy MySQL with Docker as a master and a slave. For more information about deploying MySQL with Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to run MySQL in Docker environment and enable Binlog to configure master-slave synchronization
  • Realize MySQL real-time incremental data transmission based on Docker and Canal
  • Sample code for implementing mysql master-slave replication in docker
  • Detailed explanation of building MySQL master-slave environment with Docker
  • Docker opens mysql binlog log to solve data volume problems

<<:  Use and optimization of MySQL COUNT function

>>:  Web design experience: Make the navigation system thin

Recommend

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

A brief analysis of the basic concepts of HTML web pages

What is a web page? The page displayed after the ...

Differentiate between null value and empty character ('') in MySQL

In daily development, database addition, deletion...

Detailed installation and configuration tutorial of PostgreSQL 11 under CentOS7

1. Official website address The official website ...

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

CSS screen size adaptive implementation example

To achieve CSS screen size adaptation, we must fi...

uni-app implements NFC reading function

This article shares the specific code of uni-app ...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Detailed explanation of JavaScript prototype chain

Table of contents 1. Constructors and instances 2...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...