mysql 主从配置

使用docker-compose搭建,直接上配置文件

docker-compose.yml

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
version: '2'

services:
mysqlmaster:
image: docker.io/mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- "/data/master//mysql:/var/lib/mysql"
- "/data/master/config/my.cnf:/etc/mysql/my.cnf"
networks:
- backend

mysqlslave:
image: docker.io/mysql
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- "/data/slave/mysql:/var/lib/mysql"
- "/data/slave/config/my.cnf:/etc/mysql/my.cnf"
networks:
- backend
networks:
backend:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
  • 进入容器 docker exec -it work_mysqlmaster_1 bash
  • mysql -u root -p
  • Master: show master status; 得到类似 binlog.000003
  • 查看Master的IP docker inspect work_mysqlmaster_1
  • Slave: CHANGE MASTER TO MASTER_HOST='172.18.0.2',MASTER_PORT=3306,MASTER_USER='root',MASTER_PASSWORD='root',MASTER_LOG_FILE='binlog.000006',MASTER_LOG_POS=155,get_master_public_key=1;
  • Slave: show slave status\G
  • Slave: start slave; stop slave;
  • Slave: set global server_id=2; 如果提示ids错误,用这个分别对两个mysql设置serverid

my.cnf

1
2
3
4
5
6
7
8
9
10
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
symbolic-links=0
server-id=1

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