記錄一下本身配置調試的過程python
docker pull mysql=5.7
複製代碼
version: '3'
services:
db_m:
image: mysql:5.7
container_name: master
ports:
- "3310:3306"
environment:
MYSQL_ROOT_PASSWORD: future
MYSQL_DATABASE: my_test
LANG: C.UTF-8
volumes:
- ./data/master:/var/lib/mysql
- ./config/master:/etc/mysql/mysql.conf.d/
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
#restart:always
db_s:
image: mysql:5.7
container_name: slave
ports:
- "3311:3306"
environment:
MYSQL_ROOT_PASSWORD: future
MYSQL_DATABASE: my_test
LANG: C.UTF-8
volumes:
- ./data/slave:/var/lib/mysql
- ./config/slave:/etc/mysql/mysql.conf.d/
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']
#restart:always
複製代碼
目的是建立一主一從兩個mysql的容器,將mysql端口分別帶出來,設置好root用戶的密碼以及字符格式,最重要的是 將mysql的data數據映射出來,再映射一個配置文件的目錄mysql
docker-compose up -d
複製代碼
主:sql
[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
# my settings
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION, NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
server-id = 100
log-bin=/var/lib/mysql/mysql-bin.log
binlog-do-db=my_test
複製代碼
從:docker
[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
# my settings
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION, NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
server-id = 101
log-bin=/var/lib/mysql/mysql-bin_slave.log
replicate-do-db=my_test
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60
複製代碼
須要設置sql_mode,否則有些sql語句不能執行,設置2個mysql的server-id,設置成不一樣的就能夠了,再打開mysql的bin-log.ubuntu
grant replication client , replication slave on *.* to sla@'%' identified by 'sla_123';
flush privileges;
複製代碼
這裏我是在navcat裏執行的bash
cp ./data/master ./data/slave
複製代碼
這裏是將保證從庫跟主庫數據文件一致,再查看gtidsocket
show global variables like 'gtid_executed'
複製代碼
在主庫跟從庫中分別執行,檢查gtid是否一致。ide
top slave;
複製代碼
change master to master_host = '192.168.40.132',
master_port = 3310,master_user = 'sla',master_password='sla_123';
複製代碼
start slave;
複製代碼
這裏的master_host 是我虛擬機的ip,port是主庫帶出來的端口工具
在從庫中執行ui
show slave status
複製代碼
在主庫中新建一張表,並添加一些數據,打開從庫,可發現從庫也存在該表,數據也是一致的,此時,docker下的mysql主從複製功能就基本上實現了
另外喜歡封面圖片的朋友還能夠去這裏看看
此文章僅爲記錄所用 by chuandong