一、復制原理
主服務(wù)器將更新寫入二進制日志文件,并維護文件的一個索引以跟蹤日志循環(huán)。這些日志可以記錄發(fā)送到從服務(wù)器的更新.當一個從服務(wù)器連接主服務(wù)器時,它通知主服務(wù)器從服務(wù)器在日志中讀取的最后一次成功更新的位置。從服務(wù)器接收從那時起發(fā)生的任何更新,然后封鎖并等待主服務(wù)器通知新的更新。
MySQL使用3個線程來執(zhí)行復制功能(其中1個在主服務(wù)器上,另兩個在從服務(wù)器上。當發(fā)出START SLAVE時,從服務(wù)器創(chuàng)建一個I/O線程,以連接主服務(wù)器并讓它發(fā)送記錄在其二進制日志中的語句。主服務(wù)器創(chuàng)建一個線程將二進制日志中的內(nèi)容發(fā)送到從服務(wù)器。
該線程為主服務(wù)器上的Binlog Dump線程。從服務(wù)器I/O線程讀取主服務(wù)器Binlog Dump線程發(fā)送的內(nèi)容并將該數(shù)據(jù)拷貝到從服務(wù)器數(shù)據(jù)目錄中的本地文件中,即中繼日志。第3個線程是SQL線程,是從服務(wù)器創(chuàng)建用于讀取中繼日志并執(zhí)行日志中包含的更新。
二、服務(wù)器準備
操作系統(tǒng)版本:Red Hat Enterprise Linux Server release 6.7 (Santiago)
Master(主) ip:172.16.115.245 主機名稱:mysql2 server_id:245
Slave(從) ip:172.16.115.247 主機名稱:mysql3 server_id:247
主從服務(wù)器上都已安裝MySQL5.7.18
三、主從復制實施細節(jié)
1.主服務(wù)器上為服務(wù)器設(shè)置一個連接賬戶并授予REPLICATION SLAVE權(quán)限。
1
|
GRANT REPLICATION SLAVE ON *.* TO 'repl' @ '%' IDENTIFIED BY 'repl@20170509' ; |
2.修改master配置文件my.cnf
1
2
|
server-id = 245 log_bin = /data/mysqllog/3306/bin_log/binlog |
這兩個值必須設(shè)置,設(shè)置好之后,重啟MySQL。
3.備份master上一份完整的數(shù)據(jù)
1
|
mysqldump -uroot -p '密碼' --master-data=2 --single-transaction -R --triggers -A > /backup/all.sql |
說明:
--master-data=2代表備份時刻記錄master的Binlog位置和Position
--single-transaction意思是獲取一致性快照
-R意思是備份存儲過程和函數(shù)
--triggres的意思是備份觸發(fā)器
-A代表備份所有的庫
4.查看主庫備份時的binlog名稱和位置
1
2
3
4
5
6
7
|
SHOW MASTER STATUS; mysql> SHOW MASTER STATUS; + ---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | + ---------------+----------+--------------+------------------+-------------------+ | binlog.000004 | 79394496 | | | | + ---------------+----------+--------------+------------------+-------------------+ |
或者到剛才備份的數(shù)據(jù)庫文件中看:vi all.sql
5.修改slave庫配置文件my.cnf
1
2
3
4
5
6
7
|
server-id = 247 (唯一,不能與主庫一樣,一般設(shè)為服務(wù)器IP后3位) log_bin = /data/mysql/logdir/3306/bin_log/binlog innodb_file_per_table = ON skip_name_resolve = ON relay_log = /data/mysql/logdir/3306/relay_log/relay.log binlog-format = row log-slave-updates = true |
read_only=ON (只讀模式)
設(shè)置完之后,重啟MySQL。
6.在slave服務(wù)器上恢復master備份
1
|
mysql -u root -p '密碼' < all .sql |
7.停止從庫,并配置主從參數(shù),打開從庫。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
mysql> stop slave; #暫停從庫 mysql>CHANGE MASTER TO MASTER_HOST= '172.16.115.245' ,MASTER_USER= 'repl' , MASTER_PASSWORD= 'repl@20170509' ,MASTER_LOG_FILE= 'binlog.000004' ,MASTER_LOG_POS=154; mysql> start slave; #啟動復制 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.115.245 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000004 Read_Master_Log_Pos: 104634190 Relay_Log_File: relay.000003 Relay_Log_Pos: 104632819 Relay_Master_Log_File: binlog.000004 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: 104634190 Relay_Log_Space: 104634713 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: 245 Master_UUID: 4f545573-3170-11e7-b903-000c29462d8c Master_Info_File: /data/mysql/datadir/3306/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: |
8.查看master、slave相關(guān)進程
master Binlog Dump線程:
1
2
3
4
5
6
7
8
9
10
|
mysql> SHOW PROCESSLIST \G *************************** 1. row *************************** Id: 13 User : repl Host: 172.16.115.247:44602 db: NULL Command: Binlog Dump Time : 76514 State: Master has sent all binlog to slave; waiting for more updates Info: NULL |
slave IO/SQL線程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
mysql> SHOW PROCESSLIST \G *************************** 1. row *************************** Id: 10 User : system user Host: db: NULL Command: Connect Time : 81148 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 12 User : system user Host: db: NULL Command: Connect Time : 5 State: Reading event from the relay log Info: NULL |
9.至此,主從配置已經(jīng)完成,可以到master服務(wù)器上創(chuàng)建數(shù)據(jù)庫、表等操作,看slave數(shù)據(jù)庫是否同步!
總結(jié)
以上所述是小編給大家介紹的MySQL5.7.18主從復制搭建(一主一從)教程詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:http://blog.csdn.net/hwh1231/article/details/71537611