当前位置:网站首页>Docker swarm cluster builds highly available MySQL active and standby
Docker swarm cluster builds highly available MySQL active and standby
2022-07-26 13:43:00 【CS beat you】
docker swarm Cluster construction is highly available mysql The main equipment
Premise : for fear of mysql A single point of failure , It is necessary to introduce mysql The main equipment . The main and standby are on different machines , And need to maintain data consistency
technology : Used here nginx+mysql+mysql Be a highly available active standby , utilize nginx Acting as a reverse agent , When one of them mysql When the server goes down , Automatically switch to another , Note that load balancing cannot be done here , Because there are problems with high concurrency .

One , start-up docker swarm colony
1,docker stack The configuration is as follows , If you can't build docker swarm Clustered , Reference article : build docker swarm Highly available clusters and common commands
#mysql1 service
mysql-1:
image: mysql:5.7
ports:
- 3310:3306
networks:
# Specify the network the container joins
- network
environment:
# It is best to use this to set the time zone , Other still images can also be used
- TZ=CST-8
# Appoint mysql Access code for
- MYSQL_ROOT_PASSWORD=123456
# You can add --default-time-zone='+8:00' Set time zone
volumes:
# Local file directory , Persistence
- /home/AuthenticationCenter/new_mysql/mone/data:/var/lib/mysql
# Specify on binlog Configuration file for
- /home/AuthenticationCenter/new_mysql/mone/conf/my.cnf:/etc/mysql/my.cnf
command: --character-set-server=utf8
--collation-server=utf8_general_ci
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
deploy:
placement:
constraints:
# The hostname Specify the host on which the container starts
- node.hostname == ecs2d8ed9c368b9
#mysql2 service
mysql-2:
image: mysql:5.7
ports:
- 3311:3306
networks:
# Specify the network the container joins
- network
environment:
# It is best to use this to set the time zone , Other still images can also be used
- TZ=CST-8
# Appoint mysql Access code for
- MYSQL_ROOT_PASSWORD=123456
# You can add --default-time-zone='+8:00' Set time zone
volumes:
# Local file directory mount , Persistence
- /home/AuthenticationCenter/new_mysql/mtwo/data:/var/lib/mysql
# Specify on binlog Configuration file for
- /home/AuthenticationCenter/new_mysql/mtwo/conf/my.cnf:/etc/mysql/my.cnf
command: --character-set-server=utf8
--collation-server=utf8_general_ci
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
deploy:
placement:
constraints:
# The hostname Specify the host on which the container starts
- node.hostname == ecseafe0d11214a
#mysql-nginx service
mysql_nginx:
image: nginx:1.19.2
ports:
- 33060:3306
networks:
- network
volumes:
# Local file directory
- /home/AuthenticationCenter/new_mysql/nginx/nginx.conf:/etc/nginx/nginx.conf
- /home/AuthenticationCenter/new_mysql/nginx/data:/var/www/html/upload
deploy:
mode: replicated
# Here open two copies , prevent nginx A single point of failure
replicas: 2
placement:
constraints: [node.role == manager]
depends_on:
- mysql-1lookup mysql-1 and mysql-2 Of hostname, Can pass manager The management node executes the following command to view , The results are as follows
command :docker nodel ls

mysql-1 Of my.cnf To configure :
[mysqld]
server_id=1
log-bin= mysql-bin
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
read-only=0
relay_log=mysql-relay-bin
log-slave-updates=on
auto-increment-offset=1
auto-increment-increment=2
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/mysql-2 Of my.cnf To configure :
[mysqld]
server_id=2
log-bin= mysql-bin
replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
read-only=0
relay_log=mysql-relay-bin
log-slave-updates=on
auto-increment-offset=2
auto-increment-increment=2
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/utilize nginx Do the reverse proxy of the fourth layer , You can also use Haproxy
nginx The configuration is as follows
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
stream {
upstream mysql {
#backup As standby mysql, When mysql-1 Automatic switching after failure mysql-2, Achieve the active and standby effect ,
server mysql-1:3306 max_fails=3 fail_timeout=30s;
server mysql-2:3306 backup;
}
server {
listen 3306;
proxy_connect_timeout 3000s;
proxy_timeout 6000s;
proxy_pass mysql;
}
}
Start by command docker swarm colony : docker stack deploy -c docker-stack-test.yml mysqltest
Two , Turn on mysql Master master synchronization ,
1, Execute the following command on the corresponding host , Get into mysql-1 and mysql-2 Containers
see docker The container of id: docker ps -f "name=mysql-1" perhaps docker ps -f "name=mysql-2"
Into the container :docker exec -it ( Containers ID) /bin/bash ( Containers ID Is the position of the red box in the figure below )

2, Respectively into the mysql-1 and mysql-2 After the container, execute the following command to enter mysql service ( password :123456)
Get into mysql service :mysql -u root -p
Input password :123456
3,mysql-1 and mysql-2 Execute the following commands respectively , Allow others slave Of io thread Monitor your own binlog file , The principle is as follows
GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456';

4, Execute the following command to record mysql-1 synchronous binlog File status
show master status;
Here's the picture : Record mysql-bin.000003 and 3825

5,mysql-2 Execute the following command , Turn on IO Thread listening mysql-1 Of binlog file , Yellow message by mysql-1 Recorded information
explain :master_host: Here you can specify the container name , Because in the same docker swarm colony ,
master_password: visit mysql-1 perhaps mysql-2 Password
command :change master to master_host='mysql-1',master_user='slave',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=3825,master_port=3306;
6,mysql-2 Start synchronization in
start slave ;
7, Record mysql-2 synchronous binlog File status
show master status;
Here's the picture : Record mysql-bin.000004 and 3161

8, stay mysql-1 Execute the following command in , Yellow message by mysql-2 Recorded information
change master to master_host='mysql-2',master_user='slave',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=3161,master_port=3306;
9,mysql-1 Turn on synchronization
start slave ;
10, Respectively in mysql-1 and mysql-2 Execute the following command in to check whether it is successfully opened .
show slave status\G;
Here's the picture : If both are yes , It means success .

doubt :
1, By entering the corresponding MySQL Container configuration MySQL Master slave configuration , Do you need to reconfigure after the next restart , answer : No need to reconfigure , Because the configuration file has been persisted to the host , Restart does not affect .
2, Join one MySQL It's down. , When this one MySQL Will the previously unsynchronized data be automatically synchronized after restart take : It will be , Because the corresponding master Of binlog Synchronized location ,positon The location of .
3、 ... and ,mysql Data recovery
If someone MySQL The server data is lost , Data recovery needs to be done again , And it needs to be reconfigured MySQL Lord , Let's talk about this in the next issue .......
边栏推荐
- Intercept the coordinate points (four point coordinates of the face frame) face image from the marked XML file and save it in the specified folder
- Seven steps to copywriting script ---- document team collaborative management
- B+ tree index uses (7) matching column prefix, matching value range (19)
- 天翼云Web应用防火墙(边缘云版)支持检测和拦截Apache Spark shell命令注入漏洞
- 421. Maximum XOR value of two numbers in the array
- Cavans realizes Static Rolling barrage
- Activity.onStop() 延迟10秒?精彩绝伦的排查历程
- 消息的订阅和发布
- 时间复杂度和空间复杂度
- Tianjin emergency response Bureau and central enterprises in Tianjin signed an agreement to deepen the construction of emergency linkage mechanism
猜你喜欢

解决远程主机无法连接mysql数据库的问题

flutter多渠道打包运行

See you tomorrow at the industrial session of cloud intelligence technology forum!

Unicode file parsing methods and existing problems

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

Basic sentence structure of English ----- origin
![[collection of topics that C language learners must know 1] consolidate the foundation and steadily improve](/img/95/bec94176cadfac112585df259156c9.png)
[collection of topics that C language learners must know 1] consolidate the foundation and steadily improve
![[oauth2] v. oauth2loginauthenticationfilter](/img/54/3c3f02511e30c301a5cea6f6ddd4c2.png)
[oauth2] v. oauth2loginauthenticationfilter

Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
随机推荐
Cavans realizes Static Rolling barrage
JSON data transfer parameters & date type parameter transfer
白帽子揭秘:互联网千亿黑产吓退马斯克
How to write the introduction of GIS method journals and papers?
The serialization class in unity is in JSON format
Canvas upload image Base64 with cropping function jcrop.js
[oauth2] v. oauth2loginauthenticationfilter
华为机考 ~ 偏移量实现字符串加密
MySQL data directory (1) -- database structure (24)
The last time I heard about eBay, or the last time
Huawei computer test ~ offset realizes string encryption
.net6与英雄联盟邂逅之——根据官方LCU API制作游戏助手
Click El dropdown item/@click.native
Seven steps to copywriting script ---- document team collaborative management
Parent class reference to child class (parent class reference points to child class object)
We were tossed all night by a Kong performance bug
SuperMap iclient for leaflet loads Gauss Kruger projection three-dimensional zonation CGCS2000 geodetic coordinate system WMTs service
Sword finger offer (x): rectangular coverage
Official announcement! Edweisen group and Baidu xirang reached a deep co creation cooperation
银行业客户体验管理现状与优化策略分析