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 49 50 51 52 53 54
| ================== ↓↓↓↓↓↓ 配置主库 ↓↓↓↓↓↓ ================== # 进入主库 docker exec -it mysql_master /bin/bash # 登录mysql mysql -uroot -pabc123 # 创建用户slave,密码123456 CREATE USER 'slave'@'%' IDENTIFIED BY 'abc123'; # 授予slave用户 `REPLICATION SLAVE`权限和`REPLICATION CLIENT`权限,用于在`主` `从` 数据库之间同步数据 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; # 授予所有权限则执行命令: GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%'; # 使操作生效 FLUSH PRIVILEGES; # 查看状态 show master status; # 注:File和Position字段的值slave中将会用到,在slave操作完成之前不要操作master,否则将会引起状态变化,即File和Position字段的值变化 !!! # + # | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | # + # | mysql-bin.000003 | 769 | | | | # + # 1 row in set (0.00 sec)
# ================== ↓↓↓↓↓↓ 配置从库 ↓↓↓↓↓↓ ================== # 进入从库 docker exec -it mysql_slave /bin/bash # 登录mysql mysql -uroot -pabc123 CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_USER='slave',MASTER_PASSWORD='abc123', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=769;
# MASTER_LOG_FILE 和 MASTER_LOG_POS 和上面master的 File,Position 对应上
# 开启主从同步过程 【停止命令:stop slave;】 start slave; # 查看主从同步状态 show slave status \G # Slave_IO_Running 和 Slave_SQL_Running 都是Yes的话,就说明主从同步已经配置好了! # 如果Slave_IO_Running为Connecting,SlaveSQLRunning为Yes,则说明配置有问题,这时候就要检查配置中哪一步出现问题了哦,可根据Last_IO_Error字段信息排错或谷歌… # *************************** 1. row *************************** # Slave_IO_State: Waiting for master to send event # Master_Host: www.zhengqingya.com # Master_User: slave # Master_Port: 3306 # Connect_Retry: 30 # Master_Log_File: mysql-bin.000003 # Read_Master_Log_Pos: 769 # Relay_Log_File: c598d8402b43-relay-bin.000002 # Relay_Log_Pos: 320 # Relay_Master_Log_File: mysql-bin.000003 # Slave_IO_Running: Yes # Slave_SQL_Running: Yes # Replicate_Do_DB:
|