MYSQL主從複製

爲什麼做主從複製?

主從複製可以使MySQL數據庫主服務器的主數據庫,複製到一個或多個MySQL從服務器從數據庫,能夠實現實時災備,用於故障災備。


複製過程

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重做中繼日誌中的事件,將改變反映它自己的數據。


0_1330439010P7lI.gif

主機環境

Master:192.168.174.132

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上都可以查看到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章