搭建主從複製須要兩個以上的MySQL, 使用 Docker 很是方便。若是之前沒用過,找個簡單的文檔看看,熟悉一下命令。mysql
docker pull mysql:5.7.21
docker run --name mysql_master -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21 //經過鏡像 mysql:5.7.21 啓動一個名爲 mysql_master 的 MySQL 服務器,端口號是3306,映射的宿主機端口號是3310,root 帳號密碼是123456
docker run --name mysql_slave -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.21
經過 docker container ls -a 能夠看到建立成功sql
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2298154e6d4 mysql:5.7.21 "docker-entrypoint.s…" 3 hours ago Up 2 hours 0.0.0.0:3311->3306/tcp mysql_slave b8d7e74b2d31 mysql:5.7.21 "docker-entrypoint.s…" 3 hours ago Up About an hour 0.0.0.0:3310->3306/tcp mysql_master
docker inspect --format='{{.NetworkSettings.IPAddress}}' b8d //172.17.0.2 docker inspect --format='{{.NetworkSettings.IPAddress}}' d22 //172.17.0.3
master IP : 172.17.0.2docker
slave IP : 172.17.0.3 bash
//進入Master服務器 docker exec -it b8d /bin/bash //進的入配置 cd /etc/mysql //修改配置文件--my.cnf增長下面兩行 server_id=100 log-bin=mysql-bin //配置完成後重啓 service mysql restart //若是容器中止了,要重新啓動進入,docker container start b8d //再次進入容器 mysql -uroot -p //執行下面命令進行復制受權 CREATE USER 'slave'@'%' IDENTIFIED BY '12345678'; GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%'; //檢查命令 show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 370 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) //File Position 兩個數據後面配置 Slave 會用到。
//進入Slave 容器 docker exec -it d22 /bin/bash //配置mysql,增長下面 server_id=101 log-bin=mysql-bin //重啓進入執行 change master to master_host='172.17.0.2', master_user='slave', master_password='12345678', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos=370, master_connect_retry=30; //啓動 start slave; //查看主從同步狀態 show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.17.0.2 Master_User: slave Master_Port: 3306 Connect_Retry: 30 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 370 Relay_Log_File: d2298154e6d4-relay-bin.000002 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: 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: 370 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: 100 Master_UUID: 2cf67843-8583-11e8-bfa5-0242ac110002 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)
Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes 代表配置成功。 配置 master_log_file='mysql-bin.000002', master_log_pos=370, 這兩個參數要注意 和 show服務器
master status 輸出的值保持一致,否則會出現同步錯誤。 tcp