惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

Spaceack's Blog

使用简单算法两小时实现猎杀乌姆帕斯(Hunt the Wumpus)Python小游戏 使用Mysql实现消息队列 搭建etcd分布式kv数据库集群并使用python操作 天池大数据竞赛 Spaceack带你利用Pandas,趋势图与桑基图分析美国选民候选人喜好度 Python利用matplotlib万花尺画月饼 情人节遥寄 靠近爱人 牵着你的手 七夕收到的礼物
使用docker模拟双机环境测试mysql双机互为主备
Spaceack · 2021-02-23 · via Spaceack's Blog

MariaDB双主模式测试

1. 环境准备

创建目录 mkdir conf conf2 data data2 log log2 创建 MariaDB 配置文件 touch conf/my.cnf conf2/my.cnf conf/my.cnf文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format = mixed
binlog-do-db=event_analysis 
binlog-ignore-db =mysql
binlog-ignore-db =information_schema
binlog-ignore-db =performance_schema

replicate-do-db=event_analysis
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
read-only=0

relay_log=mysqld-relay-bin
log-slave-updates=ON
auto-increment-increment = 2
auto-increment-offset = 1

conf/my.cnf文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
[mysqld]
server-id=2
log-bin=mysql-bin
binlog_format = mixed
binlog-do-db=event_analysis 
binlog-ignore-db =mysql
binlog-ignore-db =information_schema
binlog-ignore-db =performance_schema

replicate-do-db=event_analysis
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
read-only=0

relay_log=mysqld-relay-bin
log-slave-updates=ON
auto-increment-increment = 2
auto-increment-offset = 1

使用 docker 启动两个 MariaDB 容器 bak1bak2 。使用 docker-compose up 启动容器。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
version: '2'
services:
  "mariadb":
    image: mariadb:10.5.5
    container_name: "bak1"
    restart: always
    environment:
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "123456"
      MYSQL_ROOT_PASSWORD: "123456"
      TZ: "Asia/Shanghai"
    ports:
      - "3307:3306"
    volumes:
      - ./data:/var/lib/mysql
      - ./log:/var/log/mysql
      - ./conf/my.cnf:/etc/mysql/my.cnf
    networks:
      mysql_bak_net:
  "mariadb2":
    image: mariadb:10.5.5
    container_name: "bak2"
    restart: always
    environment:
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "123456"
      MYSQL_ROOT_PASSWORD: "123456"
      TZ: "Asia/Shanghai"
    ports:
      - "3308:3306"
    volumes:
      - ./data2:/var/lib/mysql
      - ./log2:/var/log/mysql
      - ./conf2/my.cnf:/etc/mysql/my.cnf
    networks:
      mysql_bak_net:
networks:
  mysql_bak_net:

2. 操作步骤

容器名 bak1 bak2
ip 172.24.0.3 172.24.0.2
user root root
password 123456 123456
server-id 1 2
进入容器 docker exec -it bak1 bash docker exec -it bak2 bash
进入sql mysql -u root -p mysql -u root -p
查看master状态 show master status\G;
File: mysql-bin.000003
Position: 328
Binlog_Do_DB: event_analysis
Binlog_Ignore_DB: mysql,information_schema,performance_schema
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step1-1.png
show master status\G;
File: mysql-bin.000003
Position: 328
Binlog_Do_DB: event_analysis
Binlog_Ignore_DB: mysql,information_schema,performance_schema
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step1-2.png
主从复制 CHANGE MASTER TO
MASTER_HOST=‘172.24.0.2’,
MASTER_USER=‘root’,
MASTER_PASSWORD=‘123456’,
MASTER_LOG_FILE=‘mysql-bin.000003’,
MASTER_LOG_POS=328;/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step2-1.png
CHANGE MASTER TO
MASTER_HOST=‘172.24.0.3’,
MASTER_USER=‘root’,
MASTER_PASSWORD=‘123456’,
MASTER_LOG_FILE=‘mysql-bin.000003’,
MASTER_LOG_POS=328;
查看slave状态 show slave status\G;
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step3-1.png
show slave status\G;
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step3-2.png
重启MariaDB
查看slave状态 show slave status\G;
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step4-1.png
show slave status\G;
/20210222-%E4%BD%BF%E7%94%A8docker%E6%A8%A1%E6%8B%9F%E5%8F%8C%E6%9C%BA%E7%8E%AF%E5%A2%83%E6%B5%8B%E8%AF%95mysql%E5%8F%8C%E6%9C%BA%E4%BA%92%E4%B8%BA%E4%B8%BB%E5%A4%87/step4-2.png
验证 在bak1中创建表,更新数据,bak2也会更新。 在bak2中创建表,更新数据,bak1也会更新。