Docker deployment MySQL8 cluster (one master and two slaves) implementation steps

Docker deployment MySQL8 cluster (one master and two slaves) implementation steps

1. Install Docker on CentOS 7.9 20

1. Install the yum-utils tool

yum install -y yum-utils

2. Set up docker dependency sources

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Note: The Docker version installed by CentOS directly using the yum command is 1.13.1, which is the last version of the old version of Docker, so you need to configure a repo to install the new version of Docker-CE (Community Edition). Docker-EE (Enterprise Edition) is charged and readers can understand it by themselves. Here we use the CE Community Edition

3. Install Docker

yum -y install docker-ce

4. Check the installed version

docker -v
docker version 

image.png

5. Check the version of the supporting settings

yum list installed | grep docker 

image.png

6. Pull the MySQL8 image

docker pull mysql:8

Note: mysql:5.7 means MySQL version is 5.7

View Docker images

docker images 

image.png

2. Deploy MySQL cluster (one master and two slaves)

1. Create the master-slave MySQL configuration and data file storage directory

# Create the configuration directory and data directory of the master service mkdir -p /usr/local/mysqlData/master/cnf
mkdir -p /usr/local/mysqlData/master/data

# Create the configuration directory and data directory of slave server 1 mkdir -p /usr/local/mysqlData/slave/cnf
mkdir -p /usr/local/mysqlData/slave/data

# Create the configuration directory and data directory of slave server 2 mkdir -p /usr/local/mysqlData/slave2/cnf
mkdir -p /usr/local/mysqlData/slave2/data

The reason for creating two slave server configurations is that the server-id configured in MySQL cannot be repeated.

image.png

2. Configure the configuration file of the main server

vim /usr/local/mysqlData/master/cnf/mysql.cnf

The configuration file is as follows

[mysqld]
## Set server_id, note that it must be unique server-id=1
## Enable binlog
log-bin=mysql-bin
## binlog cache binlog_cache_size=1M
## binlog format (mixed, statement, row, the default format is statement)
binlog_format=mixed
##Set character encoding to utf8mb4
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

3. Configure the configuration file from the server

# No. 1 slave server vim /usr/local/mysqlData/slave/cnf/mysql.cnf
# No. 2 slave server vim /usr/local/mysqlData/slave2/cnf/mysql.cnf

The configuration files are as follows (server-id of No. 1 is set to 2, server-id of No. 2 is set to 3, no duplication is required)

[mysqld]
## Set server_id, note that it must be unique server-id=2
## Enable binlog
log-bin=mysql-slave-bin
## relay_log configuration relay log relay_log=edu-mysql-relay-bin
## If you need to synchronize functions or stored procedures log_bin_trust_function_creators=true
## binlog cache binlog_cache_size=1M
## binlog format (mixed, statement, row, the default format is statement)
binlog_format=mixed
##Set character encoding to utf8mb4
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect = 'SET NAMES utf8mb4'
slave_skip_errors=1062
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

4. Create a master-slave MySQL mirror

# Master server instantiation docker run -itd -p 3307:3306 --name master -v /usr/local/mysqlData/master/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8 

# Instantiate slave server 1 docker run -itd -p 3308:3306 --name slaver -v /usr/local/mysqlData/slave/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8 

# Instantiate slave server 2 docker run -itd -p 3309:3306 --name slaver2 -v /usr/local/mysqlData/slave2/cnf:/etc/mysql/conf.d -v /usr/local/mysqlData/slave2/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8

Parameter Explanation

-p specifies the port exposed by the container, host (physical machine) port: docker instance port
-p 3307:3306 maps the physical machine's port 3307 to the instance's port 3306

-v mounts the storage volume to the container and mounts it to a directory in the container
-v /usr/local/mysqlData/master/cnf:/etc/mysql/conf.d maps the newly created configuration folder to the instance's /etc/mysql/conf.d
-v /usr/local/mysqlData/master/data:/var/lib/mysql data folder mapping

-e specifies environment variables that can be used in the container
-e MYSQL_ROOT_PASSWORD=123456 Set the MySQL root account password to 123456

5. View the created instance

docker ps -a 

image.png

6. Create a mysql connection user

# Create user reader and set password to reader
CREATE USER reader IDENTIFIED BY 'reader';
# Grant reader synchronization permissions GRANT REPLICATION SLAVE ON *.* to 'reader'@'%';
FLUSH PRIVILEGES;

Note: For other users, remote connection settings are self-configured

7. Get the connection information of the main server

#MySQL connection information SHOW MASTER STATUS;

#Open a new connection to get the address of the master instance in docker inspect --format='{{.NetworkSettings.IPAddress}}' master

image.png

The slave server connects to the master server (both slave servers perform the following operations)

# Configure the connection parameters change master to master_host='172.17.0.2',master_user='reader',master_password='reader',master_log_file='mysql-bin.000003',master_log_pos=2259;
# Start synchronization start slave;
# Check if it is successful show slave status\G

# If both are Yes, it means success.
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes

# If it fails, you need to stop the connection and check other account passwords, addresses, POS and other parameters # Stop the connection. If it succeeds once, you don't need to use the stop slave command;

image.png

3. Results

The main server executes the command

SHOW SLAVE HOSTS; 

image.png

The IDs and ports of the two slave servers can be queried from the master server. Complete MySQL deployment.

This concludes this article about the implementation steps of Docker deployment of MySQL8 cluster (one master and two slaves). For more relevant content about Docker deployment of MySQL8 cluster, 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:
  • Deploy .Net6 project to docker
  • Detailed steps for deploying Microsoft Sql Server with Docker
  • Detailed process of deploying MySQL with docker (common applications deployed with docker)
  • Process analysis of deploying ASP.NET Core applications on Linux system Docker
  • Example of deploying MySQL on Docker
  • Deploy Asp.Net Core (.Net6) in Docker under Linux CentOS
  • Complete steps for deploying Asp.net core applications with docker
  • Docker deploys Mysql, .Net6, Sqlserver and other containers

<<:  How to start a transaction in MySQL

>>:  Introduction to 10 online development tools for web design

Recommend

Some findings and thoughts about iframe

This story starts with an unexpected discovery tod...

Introduction to SSL certificate installation and deployment steps under Nginx

Table of contents Problem description: Installati...

MySQL query optimization: causes and solutions for slow queries

Friends who are doing development, especially tho...

Detailed steps for porting busybox to build a minimal root file system

Busybox: A Swiss Army knife filled with small com...

MySQL 5.5.27 installation graphic tutorial

1. Installation of MYSQL 1. Open the downloaded M...

Detailed installation and use of docker-compose

Docker Compose is a Docker tool for defining and ...

Detailed explanation of the new CSS display:box property

1. display:box; Setting this property on an eleme...

Deploy Varnish cache proxy server based on Centos7

1. Varnish Overview 1. Introduction to Varnish Va...

Implementing custom scroll bar with native js

This article example shares the specific code of ...

CSS polar coordinates example code

Preface The project has requirements for charts, ...

Detailed explanation of TypeScript 2.0 marked union types

Table of contents Constructing payment methods us...

Implementation of pushing Docker images to Docker Hub

After the image is built successfully, it can be ...

Things to note when migrating MySQL to 8.0 (summary)

Password Mode PDO::__construct(): The server requ...

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...