轉發自:https://blog.csdn.net/money9sun/article/details/85099134html
第一步:安裝docker https://www.cnblogs.com/cyq632694540/p/10596276.htmlmysql
第二步:檢查mysql的鏡像,並下載sql
# docker search mysql:5.7docker
# docker pull mysql:5.7vim
# docker imagesbash
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql 5.7 ae6b78bedf88 4 weeks ago 372 MB服務器
第三步:開啓兩個mysql的容器網絡
創建自定義的網絡,用於兩個容易互相通訊app
# docker network create --subnet=172.18.0.0/16 mynetworktcp
開啓master01
# docker run -p 3308:3306 -p 22002:22 --name master01 -e MYSQL_ROOT_PASSWORD=123456 --net mynetwork --ip 172.18.0.2 -d mysql:5.7
開啓master02
# docker run -p 3309:3306 -p 22003:22 --name master02 -e MYSQL_ROOT_PASSWORD=123456 --net mynetwork --ip 172.18.0.3 -d mysql:5.7
第三步:安裝軟件及修改配置
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2e76f0faae1 mysql:5.7 "docker-entrypoint..." 26 hours ago Up 25 hours 33060/tcp, 0.0.0.0:22004->22/tcp, 0.0.0.0:3309->3306/tcp master02
1b6773fc8c3d mysql:5.7 "docker-entrypoint..." 26 hours ago Up 25 hours 33060/tcp, 0.0.0.0:22003->22/tcp, 0.0.0.0:3308->3306/tcp master01
進入master01容器
# docker exec -it 1b6773fc8c3d /bin/bash
root@1b6773fc8c3d:/# apt-get update
root@1b6773fc8c3d:/# apt-get install vim
root@1b6773fc8c3d:/# cd /etc/mysql/mysql.conf.d
root@1b6773fc8c3d:/# ls
mysqld.cnf
修改配置文件
root@1b6773fc8c3d:/# vi mysqld.cnf
添加的內容以下:
server-id=31
auto-increment-increment=2
auto-increment-offset=1
log-bin=/var/log/mysql/mysql-bin.log
binlog_format=mixed
relay-log=relay-bin
log-slave-updates
保存;
service mysql restart 這時候容器會退出,再啓動便可,命令 docker restart 容器id便可
一樣的方法,進入master02容器,配置文件修改成
server-id=32
auto-increment-increment=2
auto-increment-offset=2 ### 這裏要注意 offset是偏移量,保證不一樣的mysql主機生成的主鍵是不一樣的,避免主鍵衝突
log-bin=/var/log/mysql/mysql-bin.log
binlog_format=mixed
relay-log=relay-bin
log-slave-updates
保存並重啓mysql服務。
第四步:建立同步帳戶,開啓slave
一、進入master01容器
# mysql -u root -p
二、建立同步帳戶
mysql>grant replication slave,replication client on *.* to 'slave'@'%' identified by '123456';
mysql> flush privileges;
mysql> flush tables with read lock;
三、查看binlog的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 433 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
四、進入master02容器
# mysql -u root -p
五、建立同步帳戶
mysql>grant replication slave,replication client on *.* to 'slave'@'%' identified by '123456';
mysql> flush privileges;
mysql> CHANGE MASTER TO MASTER_HOST='172.18.0.2',MASTER_PORT=3308,MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=433;
// PS:這邊的用戶信息,pos,和主服務器IP要填寫正確;鏈接錯誤的話,請查看 show slave status \G 裏面的 【 Last_IO_Error 】和 【Last_SQL_Error】 錯誤信息,進行錯誤排查
//相關操做
stop slave;
start slave;
show slave status \G;
show master status;
mysql> start slave;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.18.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 3184
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 1346
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
看到 Slave_IO_Running: Yes Slave_SQL_Running: Yes 表示成功
七、查看master02 binlog的狀態
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 521 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
八、回到master01 並進入mysql控制檯,建立同步帳戶
mysql> CHANGE MASTER TO MASTER_HOST='172.18.0.3',MASTER_PORT=3309,MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=521;
mysql> start slave;
mysql> show slave status \G
2019-05-30 更新------>出現主主不一樣步的狀況時:
進入mysql
> show slave status \G; //查看錯誤日誌
而後會有個提示 讓你看下某個表下面的log
select * from performance_schema.replication_applier_status_by_worker \G ;
而後看下什麼緣由致使的錯誤;解決問題後。
設置mysql的my.cnf 配置
添加此段信息 slave-skip-errors=all 跳過錯誤的同步,重啓mysql 搞定