基礎環境:html
主庫 | 從庫 | |
服務器IP地址 | 192.168.10.11 | 192.168.10.12 |
版本 | 5.7.24 | 5.7.24 |
已存在的數據庫 |
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | lijiamandb | | mysql | | performance_schema | | sys | | testdb | +--------------------+ |
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ |
(一)主數據庫複製相關參數配置(主)mysql
若是要使用複製功能,則必須開啓二進制日誌,若是要開啓二進制日誌,須要設置server-id參數,啓動二進制日誌須要重啓數據庫實例。最少的參數配置以下:sql
[mysqld] log-bin=mysql-bin server-id=1
但在實際使用中,還需配置其它參數,用於優化配置:數據庫
所以,個人my.cnf配置以下:服務器
[mysqld] # 複製相關參數配置 server_id = 1 binlog_format=ROW log_bin=/mysql/binlog/master-bin sync_binlog=1 expire_logs_days=1 max_binlog_size=1G sync_binlog=1 innodb_flush_log_at_trx_commit=1
(二)建立用於複製的用戶(主)網絡
每一個從服務器都須要使用MySQL的用戶和密碼鏈接到主服務器上,所以須要在主服務器上建立MySQL的用戶,用於進行復制操做。能夠爲每一個從庫單首創建一個帳號,也能夠使用同一個帳號。帳號需具備「replication slave」權限。less
mysql> grant replication slave on *.* to 'rep'@'%' identified by '123';
(三)初始化從庫數據(從)ide
初始化的方法有不少,能夠使用mysqldump,也能夠使用xtrabackup,其餘方法見https://dev.mysql.com/doc/refman/5.7/en/replication-snapshot-method.html。這裏以最經常使用的mysqldump進行操做。優化
(3.1)首先對主庫進行備份spa
[root@slavedb ~]# mysqldump -uroot -p123456 -h 192.168.10.11 --single-transaction --all-databases --master-data=2 > master.sql mysqldump: [Warning] Using a password on the command line interface can be insecure. [root@slavedb ~]# ls -l total 1058724 -rw-r--r-- 1 root root 1084126048 Feb 15 19:30 master.sql
注意:--master-data=2不可缺乏,該參數會將備份到的位置以註釋的形式保存在備份集文件中。
(3.2)在備庫上還原
[root@slavedb ~]# mysql -uroot -p123456 < master.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
(3.3)確認備庫還原到的位置
[root@slavedb ~]# cat master.sql |grep "CHANGE MASTER"|less -- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000021', MASTER_LOG_POS=610;
(四)從數據庫複製相關參數配置(從)
若是還沒有設置從服務器ID,或者當前值與您爲主服務器選擇的值衝突,請關閉從服務器並編輯[mysqld]配置文件的 部分以指定惟一的服務器ID。例如:
[mysqld] server_id = 2
注意:沒必要在從屬服務器上啓用二進制日誌記錄便可設置複製。可是,若是在從屬服務器上啓用二進制日誌記錄,則能夠使用從屬服務器的二進制日誌進行數據備份和崩潰恢復,也能夠將從屬服務器用做更復雜的複製拓撲的一部分(級聯)。
(五)將從數據庫添加到複製環境(從)
在從服務器設置主服務器配置信息:
mysql> change master to -> master_host='192.168.10.11', -> master_port=3306, -> master_user='rep', -> master_password='123', -> master_log_file='master-bin.000021', -> master_log_pos=610;
啓動從庫複製相關進程:
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
(六)確認複製狀態
在從庫執行「 show slave status 」,若是Slave_IO_Running和Slave_SQL_Running都是「YES」,說明覆制正常。
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.11 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000022 Read_Master_Log_Pos: 1192320 Relay_Log_File: slavedb-relay-bin.000005 Relay_Log_Pos: 1192535 Relay_Master_Log_File: master-bin.000022 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: 1192320 Relay_Log_Space: 1192911 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: caa64a22-481a-11ea-b0f1-000c29fb6200 Master_Info_File: /mysql/data/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複製(replication)文檔集合: 1.複製概述 2.基於二進制日誌文件位置(binlog)配置複製3.基於全局事物標識符(GTID)配置複製4.多源複製5.級聯複製6.半同步複製7.延遲複製8.複製過濾規則9.對複製進行故障排除10.故障切換11.複製管理 |