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

Let's talk about my understanding and application of React Context

Table of contents Preface First look at React Con...

Detailed steps for quick installation of openshift

The fastest way to experience the latest version ...

Linux tutorial on replacing strings using sed command

To replace a string, we need to use the following...

Example code for making the pre tag automatically wrap

The pre element defines preformatted text. Text en...

How to set npm to load packages from multiple package sources at the same time

Table of contents 1. Build local storage 2. Creat...

Various front-end printing methods of web: CSS controls web page printing style

CSS controls the printing style of web pages : Use...

What is BFC? How to clear floats using CSS pseudo elements

BFC Concept: The block formatting context is an i...

An exploration of the JS operator in problem

Here's the thing: Everyone knows about "...

How to make spaces have the same width in IE and FF?

body{font-size:12px; font-family:"宋体";}...

33 ice and snow fonts recommended for download (personal and commercial)

01 Winter Flakes (Individual only) 02 Snowtop Cap...

Vue project implements left swipe delete function (complete code)

Achieve results The code is as follows html <t...

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

mysql method to recursively search for all child nodes of a menu node

background There is a requirement in the project ...

Introduction to common commands and shortcut keys in Linux

Table of contents 1 System Introduction 2 System ...

Mysql Sql statement comments

You can add comments to MySQL SQL statements. Her...