爲何作主從複製?java
主從複製可使MySQL數據庫主服務器的主數據庫,複製到一個或多個MySQL從服務器從數據庫,可以實現實時災備,用於故障災備。mysql
複製過程sql
master將改變記錄到二進制日誌(binary log)中(這些記錄叫作二進制日誌事件,binary log events)數據庫
slave將master的binary log events拷貝到它的中繼日誌(relay log) -- (首先,slave開始一個工做線程——I/O線程。I/O線程在master上打開一個普通的鏈接,而後開始binlog dump process,Binlog dump process從master的二進制日誌中讀取事件,若是已經跟上master,它會睡眠並等待master產生新的事件。I/O線程將這些事件寫入中繼日誌)服務器
slave重作中繼日誌中的事件,將改變反映它本身的數據。
app
主機環境ide
Master:192.168.174.132spa
Slave:192.168.174.133線程
所有關閉防火牆日誌
Master建立複製帳號
grant replication slave on *.* to backup@'%' identified by 'Jy@123456';
拷貝數據
假如兩臺·MYSQL爲新安裝則不須要,不一樣步時則關停Master服務器,將Master中的數據拷貝到Slave服務器中,使得Master和Slave中的數據同步,而且確保在所有設置操做結束前,禁止在Master和slave服務器中進行寫操做,使得兩數據庫中的數據必定要相同!
配置Master
server-id=1 指定惟一server-id log-bin=mysql-bin 打開二進制日誌
配置Slave
server_id = 2 log-bin=mysql-bin log_slave_updates=1 將複製時間寫進本身的二進制日誌 relay_log=mysql-relay-bin read_only=1
Master狀態檢查
show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000002 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.04 sec)
Slave鏈接Master
change master to master_host='192.168.174.132', master_user='backup', master_password='Jy@123456', master_log_file='mysql-bin.000002', master_log_pos=154;
啓動從服務器複製線程
start slave;
查看狀態
show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.174.132 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-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: 154 Relay_Log_Space: 527 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: 8682459f-6fe1-11e6-861a-000c29a9d958 Master_Info_File: /usr/local/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) ERROR: No query specified
Slave_IO_Running: Yes Slave_SQL_Running: Yes
全爲Yes即爲成功
這時在Master上寫入任何數據,在slave上均可以查看到。