一、復制原理
主服務器將更新寫入二進制日志文件,并維護文件的一個索引以跟蹤日志循環(huán)。這些日志可以記錄發(fā)送到從服務器的更新.當一個從服務器連接主服務器時,它通知主服務器從服務器在日志中讀取的最后一次成功更新的位置。從服務器接收從那時起發(fā)生的任何更新,然后封鎖并等待主服務器通知新的更新。
MySQL使用3個線程來執(zhí)行復制功能(其中1個在主服務器上,另兩個在從服務器上。當發(fā)出START SLAVE時,從服務器創(chuàng)建一個I/O線程,以連接主服務器并讓它發(fā)送記錄在其二進制日志中的語句。主服務器創(chuàng)建一個線程將二進制日志中的內(nèi)容發(fā)送到從服務器。
該線程為主服務器上的Binlog Dump線程。從服務器I/O線程讀取主服務器Binlog Dump線程發(fā)送的內(nèi)容并將該數(shù)據(jù)拷貝到從服務器數(shù)據(jù)目錄中的本地文件中,即中繼日志。第3個線程是SQL線程,是從服務器創(chuàng)建用于讀取中繼日志并執(zhí)行日志中包含的更新。
二、服務器準備
操作系統(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
主從服務器上都已安裝MySQL5.7.18
三、主從復制實施細節(jié)
1.主服務器上為服務器設(shè)置一個連接賬戶并授予REPLICATION SLAVE權(quán)限。
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl@20170509';
2.修改master配置文件my.cnf
server-id = 245log_bin = /data/mysqllog/3306/bin_log/binlog
這兩個值必須設(shè)置,設(shè)置好之后,重啟MySQL。
3.備份master上一份完整的數(shù)據(jù)
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名稱和位置
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
server-id = 247 (唯一,不能與主庫一樣,一般設(shè)為服務器IP后3位)log_bin = /data/mysql/logdir/3306/bin_log/binloginnodb_file_per_table = ONskip_name_resolve = ONrelay_log = /data/mysql/logdir/3306/relay_log/relay.logbinlog-format = rowlog-slave-updates = true
read_only=ON (只讀模式)
設(shè)置完之后,重啟MySQL。
6.在slave服務器上恢復master備份
mysql -u root -p'密碼' < all.sql
7.停止從庫,并配置主從參數(shù),打開從庫。
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 eventMaster_Host: 172.16.115.245Master_User: replMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000004Read_Master_Log_Pos: 104634190Relay_Log_File: relay.000003Relay_Log_Pos: 104632819Relay_Master_Log_File: binlog.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 104634190Relay_Log_Space: 104634713Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 245Master_UUID: 4f545573-3170-11e7-b903-000c29462d8cMaster_Info_File: /data/mysql/datadir/3306/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
8.查看master、slave相關(guān)進程
master Binlog Dump線程:
mysql> SHOW PROCESSLIST /G*************************** 1. row ***************************Id: 13User: replHost: 172.16.115.247:44602db: NULLCommand: Binlog DumpTime: 76514State: Master has sent all binlog to slave; waiting for more updatesInfo: NULL
slave IO/SQL線程:
mysql> SHOW PROCESSLIST /G*************************** 1. row ***************************Id: 10User: system userHost: db: NULLCommand: ConnectTime: 81148State: Waiting for master to send eventInfo: NULL*************************** 2. row ***************************Id: 12User: system userHost: db: NULLCommand: ConnectTime: 5State: Reading event from the relay logInfo: NULL
9.至此,主從配置已經(jīng)完成,可以到master服務器上創(chuàng)建數(shù)據(jù)庫、表等操作,看slave數(shù)據(jù)庫是否同步!
總結(jié)
以上所述是小編給大家介紹的MySQL5.7.18主從復制搭建(一主一從)教程詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答
圖片精選