Docker MySQL 最简单的主从搭建

主:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: "3"
services:
mysql:
image: mysql:8.0.22
container_name: mysql
restart: always
security_opt:
- seccomp:unconfined
ports:
- 3306:3306
volumes:
- ./mysql-data:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
environment:
- MYSQL_ROOT_PASSWORD=Kaiyuan@2020
- TZ=Asia/Shanghai

my.cnf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

max_connections=1024

sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

server-id=1

最主要的配置: server-id=1

从:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: "3"
services:
mysql:
image: mysql:8.0.22
container_name: mysql-slave-1
restart: always
security_opt:
- seccomp:unconfined
ports:
- 3306:3306
volumes:
- ./mysql-data:/var/lib/mysql
- ./my.cnf:/etc/mysql/my.cnf
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- MYSQL_ROOT_PASSWORD=Kaiyuan@2020
- TZ=Asia/Shanghai

my.cnf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

max_connections=1024

sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

server-id=2
super-read-only

和主不同的是, server-id=2, super-read-only 开启只读模式

init.sql:

1
2
3
change master to master_host='10.55.3.122',master_port=3306,master_user='root',master_password='Kaiyuan@2020';
reset slave;
start slave;