基于 Docker 搭建 MySQL 主从复制 —— 可扩展为一主多从
首先,从仓库拉取 MySQL 镜像
docker pull mysql:5.7
然后,建立如下目录文件结构
主库配置文件 —— master.cnf
[mysqld]
# 主从同步设置 - master
server-id = 1
log-bin=mysql-bin
binlog-ignore-db=mysql #忽略的数据库
binlog-ignore-db=information-schema
# 系统数据库编码设置,排序规则
character_set_server = utf8mb4
collation_server = utf8mb4_bin
back_log = 1024
explicit_defaults_for_timestamp = ON
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
lower_case_table_names=0
# 默认使用InnoDB存储引擎
default_storage_engine = InnoDB
innodb_buffer_pool_size = 64M
innodb_purge_threads = 1
innodb_log_buffer_size = 2M
innodb_log_file_size = 128M
innodb_lock_wait_timeout = 120
# 时区设置
default-time_zone = '+8:00'
从库1配置文件 —— slave1.cnf
[mysqld]
# 主从同步设置 - slave1
server-id = 2
log-bin=mysql-bin
replicate_ignore_db=mysql #被忽略的数据库
replicate-ignore-db=information-schema
slave-skip-errors=all #跳过所有错误
#系统数据库编码设置,排序规则
character_set_server = utf8mb4
collation_server = utf8mb4_bin
back_log = 1024
explicit_defaults_for_timestamp = ON
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
lower_case_table_names=0
#默认使用InnoDB存储引擎
default_storage_engine = InnoDB
innodb_buffer_pool_size = 64M
innodb_purge_threads = 1
innodb_log_buffer_size = 2M
innodb_log_file_size = 128M
innodb_lock_wait_timeout = 120
# 时区设置
default-time_zone = '+8:00'
创建容器
注意映射到宿主机的端口不能重复!!!
主库容器
docker run -d --privileged=true
--restart=always
--name=mysql5.7-master
-p 3306:3306
-v /home/usr/mysql5.7/master/data/:/var/lib/mysql
-v /home/usr/mysql5.7/master/conf/master.cnf:/etc/mysql/mysql.cnf
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
从库1容器
docker run -d --privileged=true
--restart=always
--name=mysql5.7-slave1
-p 3307:3306
-v /home/usr/mysql5.7/slave1/data/:/var/lib/mysql
-v /home/usr/mysql5.7/slave1/conf/slave1.cnf:/etc/mysql/mysql.cnf
-e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
接下来,就是配置主从复制了
查看主库状态
show master status;
在 从库1 上操作
stop slave;
change master to master_host='172.17.0.4', # 主库 ip 可以在 docker 上查到
master_user='root', # 注意用户名
master_password='123456', # 注意密码
master_log_file='mysql-bin.000003', # 与主库状态保持一致
master_log_pos=120;
start slave;
查看从库状态
show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.4
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: b8c4b95d9264-relay-bin.000007
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information-schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 534
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 33b4e34d-eb05-11e9-bd1e-0242ac110004
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
通过主库查看从库状态
show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 2 | | 3306 | 1 | 39dcf2c4-eb05-11e9-b0cd-0242ac110005 |
+-----------+------+------+-----------+--------------------------------------+