docker installs mysql to simulate a master-slave database

Posted by jkurrle on Thu, 27 Jan 2022 09:56:53 +0100

Pull mysql image

docker pull mysql:5.7.36

Create container

Create the following folders
master
slave1
slave2

Create master database

docker run \
--name mysql-master \
-p 3307:3306 \
-v  /usr/local/docker/mysql/data/master:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.36

Create slave database 1

docker run \
--name mysql-slave1 \
-p 3308:3306 \
-v  /usr/local/docker/mysql/data/slave1:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.36

Create slave database 2

docker run \
--name mysql-slave2 \
-p 3309:3306 \
-v  /usr/local/docker/mysql/data/slave2:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7.36

tip: parameter description

– name MySQL master: set the container name.
-p 3306:3306: map the port of the host to the port of the container.
-v /usr/local/docker/mysql/data/slave1:/var/lib/mysql: mount the directory of the host to the directory of the container to realize data persistence.
-e MYSQL_ROOT_PASSWORD=root: set the environment variable. Here is the password for setting the root account.
-d mysql:5.7.36: specify to create an image of the container and use the daemon to run it. The image can be specified by name or ID. when using the name, you can set a label. When the corresponding image cannot be found locally, it will be pulled from the online public warehouse.

Configure master-slave

Configure master library

View the LAN ip of the master database

docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql-master


Enter the main database

docker exec -it mysql-master /bin/bash

Install vim

apt-get update
apt-get install -y vim

Problem: an error occurs when executing apt get update

root@3c04cb20100f:/# apt-get update
Err:1 http://deb.debian.org/debian buster InRelease                                                 
  Temporary failure resolving 'deb.debian.org'
Err:2 http://security.debian.org/debian-security buster/updates InRelease                           
  Temporary failure resolving 'security.debian.org'
Err:3 http://repo.mysql.com/apt/debian buster InRelease              
  Temporary failure resolving 'repo.mysql.com'
Err:4 http://deb.debian.org/debian buster-updates InRelease     
  Temporary failure resolving 'deb.debian.org'
Reading package lists... Done    
W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease  Temporary failure resolving 'security.debian.org'
W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://repo.mysql.com/apt/debian/dists/buster/InRelease  Temporary failure resolving 'repo.mysql.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
root@3c04cb20100f:/# cat /etc/apt/sources.list
# deb http://snapshot.debian.org/archive/debian/20211220T000000Z buster main
deb http://deb.debian.org/debian buster main
# deb http://snapshot.debian.org/archive/debian-security/20211220T000000Z buster/updates main
deb http://security.debian.org/debian-security buster/updates main
# deb http://snapshot.debian.org/archive/debian/20211220T000000Z buster-updates main
deb http://deb.debian.org/debian buster-updates main

solve
Exit the database and restart docker

root@3c04cb20100f:/# exit
exit
systemctl restart docker

Modify master database configuration

vim /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
pid-file         = /var/run/mysqld/mysqld.pid
socket           = /var/run/mysqld/mysqld.sock
datadir          = /var/lib/mysql
#log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links   = 0
##newly added
server_id        = 1
log_bin          = mysql-bin
binlog_format    = statement
expire_logs_days = 30
binlog_ignore_db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys

tip: parameter description

datadir: data file storage directory, which should be consistent with the directory mounted when creating the container.
Bind address: allow remote access after commenting out.
server_id: service ID, which must be unique in the cluster.
log_bin: enable the binary log function and set the log file name. As the main database, it must be enabled.
expire_logs_days: binary log expiration days.
binlog_ignore_db: Specifies the database that does not log, or binlog can be used_ do_ DB specifies the database to log.

Restart service

docker restart mysql-master

mysql -u root -p

#Check whether the binary log function is enabled successfully:
show variables like 'log_bin'; // on

#Create users for synchronization hwy_slave, set the password to hwy123456, assign synchronization permission and check whether the assignment is successful:
CREATE USER 'hwy_slave'@'%' IDENTIFIED BY 'hwy123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'hwy_slave'@'%';
show GRANTS for 'hwy_slave'@'%';
#View the status of the primary server and record the values of File and Position
show master status;
  	File: mysql-bin.000001
  	Position: 625

Configure slave Library

Install vim, the same as the main database.

Enter from database

docker exec -it mysql-slave1 /bin/bash

Modify configuration

vim /etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]
pid-file            = /var/run/mysqld/mysqld.pid
socket              = /var/run/mysqld/mysqld.sock
datadir             = /var/lib/mysql
#log-error          = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address       = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links      = 0

server_id           = 11
relay_log           = mysql-slave-relay
replicate_ignore_db = mysql
replicate_ignore_db = information_schema
replicate_ignore_db = performation_schema
replicate_ignore_db = sys
slave-skip-errors = all

tip: parameter description

server_id: service ID, which must be unique in the cluster.
relay_log: relay log. After it is enabled, the binary log of the master database is stored in the local relay log file of the slave server, and then the relay log is read and applied to the slave database.
replicate_ignore_db: specify the database that does not need to be synchronized, or use replicate do DB to specify the database that needs to be synchronized.

Restart database

docker restart mysql-slave1

Re enter the slave database and set the master-slave

mysql -uroot -proot
CHANGE MASTER TO MASTER_HOST='172.17.0.2',MASTER_PORT=3306,MASTER_USER='hwy_slave',MASTER_PASSWORD='hwy123456',MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=625;   

tip: parameter description

MASTER_HOST: the host IP of the main database service. Here is the LAN IP of the MySQL master container recorded above.
MASTER_PORT: the port bound by the primary database service.
MASTER_USER: the user account with synchronization permission created by the master database service.
MASTER_PASSWORD: the password corresponding to the authorized account.
MASTER_LOG_FILE: the name of the binary log file of the primary database to be read.
MASTER_LOG_POS: read from the location specified by the binary log of the main database.
Start master-slave

mysql> start slave;

View slave library status

mysql> show slave status \G;

test

Topics: Database MySQL Docker server Container