docker pull mysql
參考: 鏡像地址html
docker run -t --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql
docker run -t --name mysql-master -p 3307:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql
docker run -t --name mysql-slave -p 3308:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql
經過binary logging來實現複製mysql
/etc/mysql/conf.d/
git
在/etc/mysql/conf.d/
目錄下添加*.cnf文件(名字隨便取)github
[mysqld] log-bin=mysql-bin server-id=1
[mysqld] #log-bin=mysql-bin #不必啓用 binary logging除非 從服務器 要做爲其餘服務器的 主服務器 server-id=2
mysql-master
及mysql-slave
直接將文件複製到容器的相應木目錄下,而後重啓容器sql
docker cp master.cnf mysql-master:/etc/mysql/my.cnf docker cp slave.cnf mysql-master:/etc/mysql/my.cnf
docker exec -it mysql-master mysql -uroot -p123456
CREATE USER 'repl'@'%' IDENTIFIED BY '123456'; -- '%'意味着全部的終端均可以用這個用戶登陸 GRANT SELECT,REPLICATION SLAVE ON *.* TO 'repl'@'%'; -- SELECT權限是爲了讓repl能夠讀取到數據,生產環境建議建立另外一個用戶
docker exec -it mysql-slave mysql -uroot -p123456
CHANGE MASTER TO MASTER_HOST='192.168.99.100',MASTER_PORT=3308, MASTER_USER='repl', MASTER_PASSWORD='123456'; START SLAVE; SHOW SLAVE STATUS\G -- \G用來代替";",能把查詢結果按鍵值的方式顯示
因爲剛開始沒有配置主服務器的端口致使以下錯誤運行結果docker
mysql> mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Connecting to master Master_Host: 192.168.99.100 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Read_Master_Log_Pos: 4 Relay_Log_File: d0f565d3c84f-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: Slave_IO_Running: Connecting 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: 0 Relay_Log_Space: 154 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: 2003 Last_IO_Error: error connecting to master 'repl@192.168.99.100:3306' - retry-time: 60 retries: 2 Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 Master_UUID: 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: 170622 08:50:54 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)
正確運行結果數據庫
mysql> change master to -> master_host='192.168.99.100' -> master_port=3308 -> master_user='repl'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_port=3308 master_user='repl'' at line 3 mysql> change master to master_host='192.168.99.100',master_port=3308,master_user='repl',master_passwrod='123456'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'master_passwrod='123456'' at line 1 mysql> change master to master_host='192.168.99.100',master_port=3308,master_user='repl',master_password='123456'; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> start slave -> ; Query OK, 0 rows affected (0.00 sec) mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.99.100 Master_User: repl Master_Port: 3308 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 313 Relay_Log_File: d0f565d3c84f-relay-bin.000002 Relay_Log_Pos: 526 Relay_Master_Log_File: mysql-bin.000001 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: 313 Relay_Log_Space: 740 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: 2b3c5dee-5722-11e7-8ddb-0242ac110003 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)
在mysql-master
執行以下操做服務器
CREATE DATABASE test;
查看mysql-slave
中也會建立相關的數據庫,至此,主從備份搞定。測試
參考官網命令行