MySQL5.7.18主從復(fù)制搭建(一主一從)教程詳解
一、復(fù)制原理
主服務(wù)器將更新寫入二進(jìn)制日志文件,并維護文件的一個索引以跟蹤日志循環(huán)。這些日志可以記錄發(fā)送到從服務(wù)器的更新.當(dāng)一個從服務(wù)器連接主服務(wù)器時,它通知主服務(wù)器從服務(wù)器在日志中讀取的最后一次成功更新的位置。從服務(wù)器接收從那時起發(fā)生的任何更新,然后封鎖并等待主服務(wù)器通知新的更新。
MySQL使用3個線程來執(zhí)行復(fù)制功能(其中1個在主服務(wù)器上,另兩個在從服務(wù)器上。當(dāng)發(fā)出START SLAVE時,從服務(wù)器創(chuàng)建一個I/O線程,以連接主服務(wù)器并讓它發(fā)送記錄在其二進(jìn)制日志中的語句。主服務(wù)器創(chuàng)建一個線程將二進(jìn)制日志中的內(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ù)器準(zhǔn)備
操作系統(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
三、主從復(fù)制實施細(xì)節(jié)
1.主服務(wù)器上為服務(wù)器設(shè)置一個連接賬戶并授予REPLICATION SLAVE權(quán)限。
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl@20170509';
2.修改master配置文件my.cnf
server-id = 245 log_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è)為服務(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ù)器上恢復(fù)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; #啟動復(fù)制 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)進(jìn)程
master Binlog Dump線程:
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線程:
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主從復(fù)制搭建(一主一從)教程詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
MySQL示例講解數(shù)據(jù)庫約束以及表的設(shè)計
約束主要完成對數(shù)據(jù)的檢驗,保證數(shù)據(jù)庫數(shù)據(jù)的完整性;如果有相互依賴數(shù)據(jù),保證該數(shù)據(jù)不被刪除,本篇文章教你如何給表設(shè)置約束及設(shè)計2022-06-06RedHat6.5/CentOS6.5安裝Mysql5.7.20的教程詳解
這篇文章主要介紹了RedHat6.5/CentOS6.5安裝Mysql5.7.20的教程詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11mysql如何比對兩個數(shù)據(jù)庫表結(jié)構(gòu)的方法
這篇文章主要介紹了mysql如何比對兩個數(shù)據(jù)庫表結(jié)構(gòu)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Mysql如何在select查詢時追加(添加)一個字段并指定值
這篇文章主要介紹了Mysql如何在select查詢時追加(添加)一個字段并指定值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09MySQL將select結(jié)果執(zhí)行update的實例教程
這篇文章主要給大家介紹了關(guān)于MySQL將select結(jié)果執(zhí)行update的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Mysql聯(lián)合查詢UNION和UNION ALL的使用介紹
本文詳細(xì)介紹了Mysql的聯(lián)合查詢命令UNION和UNION ALL,總結(jié)了使用語法和注意事項,以及學(xué)習(xí)例子和項目例子,需要的朋友可以參考下2014-04-04