Detailed explanation of building MySQL master-slave environment with Docker

Detailed explanation of building MySQL master-slave environment with Docker

Preface

This article records how I use docker-compose and dockerfile to build a MySQL master-slave environment based on binlog. If you strictly follow the steps in this article, I believe you can quickly build a basic MySQL master-slave environment.

introduce

MySQL master-slave synchronization is divided into 3 steps:

  • The master node writes data update records to the binary log.
  • The slave node starts the IO thread to connect to the master node and requests to obtain the log after the specified position of the specified binary log file.
  • The binary log dump thread of the master node pushes the specified binary log information to the slave node.
  • After receiving the message, the IO thread of the slave node writes the log content to the relay log file.
  • The SQL thread of the slave node detects that new content has been added to the relay log, immediately parses the relay log file to generate corresponding SQL statements, and replays these SQL statements to the database to ensure master-slave data consistency.

Configuration

Create the Directory Structure

First, let's get the directory structure. My directory structure is as follows. If you want to build the directory according to your own ideas, pay attention to modifying the file path in the docker-compose.yaml file and Dockerfile file below.

Configure the docker-compose template file

version: "3"
services:
 mysql-master:
 build:
  context: ./
  dockerfile:mysql/master/Dockerfile
 container_name: mysql-master
 volumes:
  - ./mysql/master/data:/var/lib/mysql
 restart: always
 ports:
  - 3305:3306
 links:
  -mysql-slave

 mysql-slave:
 build:
  context: ./
  dockerfile:mysql/slave/Dockerfile
 container_name: mysql-slave
 volumes:
  - ./mysql/slave/data:/var/lib/mysql
 restart: always
 ports:
  -3306:3306

Configure the cluster.cnf file and Dockerfile file of the master node

[mysqld]
server_id=100
binlog-ignore-db=mysql
log-bin=replicas-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062

# My MySQL is 8.x, so I need to configure default_authentication_plugin=mysql_native_password as follows
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
FROM mysql:latest
ADD ./mysql/master/cluster.cnf /etc/mysql/conf.d/cluster.cnf
ENV MYSQL_ROOT_PASSWORD=password

Configure the cluster.cnf file and Dockerfile file of the slave node

[mysqld]
server_id=101
binlog-ignore-db=mysql
binlog_cache_size=1M
binlog_format=mixed
slave_skip_errors=1062
relay_log=replicas-mysql-relay-bin
log_slave_updates=1
read_only=1

# My MySQL is 8.x, so I need to configure default_authentication_plugin=mysql_native_password as follows
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
FROM mysql:latest
ADD ./mysql/slave/cluster.cnf /etc/mysql/conf.d/cluster.cnf
ENV MYSQL_ROOT_PASSWORD=password

Create a container

docker-compose up -d mysql-master mysql-slave

Run the above command to create a container. If the build time is too long, you can consider changing the image source, such as the following domestic high-quality image sources:

NetEase: http://hub-mirror.c.163.com

Alibaba Cloud: http://&lt ;your ID>.mirror.aliyuncs.com

University of Science and Technology of China: http://docker.mirrors.ustc.ed...

After the build is complete, use the docker ps command to check whether the container is running normally. If the following situation occurs, it can be considered that the build has been successful.

Configuring slave nodes

First, use the docker command to enter the mysql-master container, then log in to mysql and enter the show master status command to obtain the status of the master database. Here we need to pay attention to two parameters, File and Position , which will be used to configure the slave database later.

Next, use the docker command to enter the mysql-slave container, then log in to mysql and enter the following statement to connect to mysql-master.

CHANGE MASTER TO
 MASTER_HOST='mysql-master',
 MASTER_USER='root',
 MASTER_PASSWORD=the password you set,
 MASTER_LOG_FILE=File parameter obtained in the previous step,
 MASTER_LOG_POS=Position parameter obtained in the previous step;

After entering the command, type the start slave command to start the slave service. After startup, enter show slave status \G command to view the slave node status. If the following situation occurs, the configuration is successful.

Test the master-slave node synchronization status

Log in to the mysql-master node and create a new database. After the database is created successfully, switch to the mysql-slave node and enter show databases; command to check whether the synchronization is successful. If the following situation occurs, the configuration is successful. You can try other operations yourself, and I will not demonstrate them here.

Summarize

These are the steps I recorded when I tried to build a MySQL master-slave architecture. This is the end of this article about using Docker to build a MySQL master-slave environment. For more information about using Docker to build a MySQL master-slave environment, 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
  • How to deploy MySQL master and slave in Docker
  • Sample code for implementing mysql master-slave replication in docker
  • Docker opens mysql binlog log to solve data volume problems

<<:  A brief discussion on the datetime format when exporting table data from MySQL to Excel

>>:  Detailed explanation of the differences and applications of {{}}, v-text and v-html in Vue

Recommend

3 simple ways to achieve carousel effects with JS

This article shares 3 methods to achieve the spec...

Web design must have purpose, ideas, thoughts and persistence

<br />Introduction: This idea came to me whe...

Detailed process of installing nginx1.9.1 on centos8

1.17.9 More delicious, really Nginx download addr...

Building .NET Core 2.0 + Nginx + Supervisor environment under Centos7 system

1. Introduction to Linux .NET Core Microsoft has ...

Example of using setInterval function in React

This article is based on the Windows 10 system en...

Encapsulate a simplest ErrorBoundary component to handle react exceptions

Preface Starting from React 16, the concept of Er...

js to achieve simple front-end paging effect

Some projects have relatively simple business, bu...

JavaScript to implement login slider verification

This article example shares the specific code of ...

Detailed examples of replace and replace into in MySQL into_Mysql

MySQL replace and replace into are both frequentl...

Analysis of the principle of Vue nextTick

Table of contents Event Loop miscroTask (microtas...

Analyzing the node event loop and message queue

Table of contents What is async? Why do we need a...

How to run the springboot project in docker

1. Click Terminal below in IDEA and enter mvn cle...

5 ways to achieve the diagonal header effect in the table

Everyone must be familiar with table. We often en...

mysql implements the operation of setting multiple primary keys

User table, ID number must be unique, mobile phon...