主頁 > 知識庫 > MySQL5.7.18主從復制搭建(一主一從)教程詳解

MySQL5.7.18主從復制搭建(一主一從)教程詳解

熱門標簽:百度競價排名 AI電銷 網(wǎng)站排名優(yōu)化 服務外包 Linux服務器 地方門戶網(wǎng)站 鐵路電話系統(tǒng) 呼叫中心市場需求

一、復制原理

主服務器將更新寫入二進制日志文件,并維護文件的一個索引以跟蹤日志循環(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.主服務器上為服務器設置一個連接賬戶并授予REPLICATION SLAVE權限。

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

這兩個值必須設置,設置好之后,重啟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 (唯一,不能與主庫一樣,一般設為服務器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   (只讀模式)

設置完之后,重啟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 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相關進程

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服務器上創(chuàng)建數(shù)據(jù)庫、表等操作,看slave數(shù)據(jù)庫是否同步!

總結

以上所述是小編給大家介紹的MySQL5.7.18主從復制搭建(一主一從)教程詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

您可能感興趣的文章:
  • MySQL主從數(shù)據(jù)庫搭建方法詳解
  • 使用Docker容器搭建MySql主從復制
  • mysql 5.7 docker 主從復制架構搭建教程
  • 詳解MySQL主從復制讀寫分離搭建
  • 基于Docker的MySQL主從復制環(huán)境搭建的實現(xiàn)步驟
  • MySQL主從搭建(多主一從)的實現(xiàn)思路與步驟

標簽:蘭州 黃山 仙桃 湖南 銅川 湘潭 衡水 崇左

巨人網(wǎng)絡通訊聲明:本文標題《MySQL5.7.18主從復制搭建(一主一從)教程詳解》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266