MySQL主從復(fù)制搭建流程分步實(shí)現(xiàn)
主從復(fù)制
主從也叫做(AB復(fù)制),允許一個(gè)服務(wù)器從一個(gè)服務(wù)器數(shù)據(jù)庫(kù)(主服務(wù)器)的數(shù)據(jù)復(fù)制到一個(gè)或者多個(gè)MySQL數(shù)據(jù)庫(kù)服務(wù)器。
主從復(fù)制的優(yōu)點(diǎn)有很多,包括橫向擴(kuò)展解決方案,數(shù)據(jù)安全性,數(shù)據(jù)分析,遠(yuǎn)程數(shù)據(jù)分發(fā)。
主從搭建
作為主服務(wù)器的服務(wù)器開啟二進(jìn)制日志(binlog);
下面我們來(lái)搭建主從復(fù)制的,采用3個(gè)節(jié)點(diǎn)來(lái)嘗試;
| 節(jié)點(diǎn)角色 | 主機(jī)名 | Ip地址 | server-id |
|---|---|---|---|
| master | master | 192.168.246.132 | 1 |
| slave | salve | 192.168.246.134 | 2 |
| slave2 | salve2 | 192.168.246.136 | 3 |
首先需要在etc/hosts中添加IP地址和主機(jī)名之間的映射關(guān)系,啟動(dòng)節(jié)點(diǎn)的數(shù)據(jù)庫(kù),然后可以查看他們的server-id
show variables like 'server_id'; # 創(chuàng)建主從復(fù)制的賬號(hào) create user 'rep1'@'192.168.246.%' identified by '123456'; grant replication slave on * .* to 'rep1'@'192.168.246.%'; flush privileges;
創(chuàng)建管理賬號(hào)
create user 'myadmin'@'192.168.246.%' identified by '123456'; grant all privileges on *.* to 'myadmin'@'192.168.246.%'; flush privileges;
在所有的節(jié)點(diǎn)上都啟用GTID
set @@GLOBAL.ENFORCE_GTID_CONSISTENCY = WARN; set global ENFORCE_GTID_CONSISTENCY = ON; set global GTID_MODE = OFF_PERMISSIVE; set global GTID_MODE = ON_PERMISSIVE; set global GTID_MODE = ON;
GTID全稱為Global Transaction Identifier, 即全局事務(wù)標(biāo)識(shí)符。用于在binlog中標(biāo)識(shí)唯一的事務(wù),當(dāng)MySQL Server在寫binlog的時(shí)候,會(huì)寫一個(gè)特殊的binlog Event,指定下一個(gè)事務(wù)的GTID。
配置主從復(fù)制的命令
注意,需要在所有的從節(jié)點(diǎn)都配置
change master to master_host='192.168.246.132',master_user='rep1',master_password='123456',master_auto_position=1;
在所有的從節(jié)點(diǎn)中開啟主從復(fù)制
start salve;
然后在從節(jié)點(diǎn)上查看主從復(fù)制的狀態(tài),出現(xiàn)下面的就算成功了。
show slave status \G;
show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.246.132
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000008
Read_Master_Log_Pos: 157
Relay_Log_File: salve-relay-bin.000002
Relay_Log_Pos: 379
Relay_Master_Log_File: mysql-binlog.000008
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: 157
Relay_Log_Space: 589
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: 71341b64-4450-11ed-90f4-000c29f2a7f4
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica 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: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)ERROR:
No query specified
測(cè)試
我們現(xiàn)在主節(jié)點(diǎn)上創(chuàng)建一張表并且插入一些數(shù)據(jù),來(lái)查看從節(jié)點(diǎn)是否有同步數(shù)據(jù);
create database demo2; use demo2; create table test1 (tid int); insert into test1 values(123);
這個(gè)時(shí)候我們?nèi)墓?jié)點(diǎn)上進(jìn)行查詢,這個(gè)時(shí)候我們可以看到數(shù)據(jù)已經(jīng)同步過(guò)來(lái)了。

我們還可以到我們?cè)O(shè)置的data目錄下面查看binlog,

再到數(shù)據(jù)庫(kù)里面查看,就可以查看具體的binlog信息了。
mysql> show binlog events in 'mysql-binlog.000008';
+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
| mysql-binlog.000008 | 4 | Format_desc | 2 | 126 | Server ver: 8.0.30, Binlog ver: 4 |
| mysql-binlog.000008 | 126 | Previous_gtids | 2 | 157 | |
| mysql-binlog.000008 | 157 | Gtid | 1 | 241 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:1' |
| mysql-binlog.000008 | 241 | Query | 1 | 352 | create database demo2 /* xid=51 */ |
| mysql-binlog.000008 | 352 | Gtid | 1 | 436 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:2' |
| mysql-binlog.000008 | 436 | Query | 1 | 554 | use `demo2`; create table test1 (tid int) /* xid=64 */ |
| mysql-binlog.000008 | 554 | Gtid | 1 | 640 | SET @@SESSION.GTID_NEXT= '71341b64-4450-11ed-90f4-000c29f2a7f4:3' |
| mysql-binlog.000008 | 640 | Query | 1 | 711 | BEGIN |
| mysql-binlog.000008 | 711 | Table_map | 1 | 763 | table_id: 94 (demo2.test1) |
| mysql-binlog.000008 | 763 | Write_rows | 1 | 803 | table_id: 94 flags: STMT_END_F |
| mysql-binlog.000008 | 803 | Xid | 1 | 834 | COMMIT /* xid=66 */ |
+---------------------+-----+----------------+-----------+-------------+-------------------------------------------------------------------+
11 rows in set (0.00 sec)
總結(jié)
上述就是搭建主從復(fù)制的搭建過(guò)程,相信大家跟著教程一步步搭建起來(lái)應(yīng)該也可以成功
到此這篇關(guān)于MySQL主從復(fù)制搭建流程分步實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL主從復(fù)制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過(guò)MySQL慢查詢優(yōu)化MySQL性能的方法講解
今天小編就為大家分享一篇關(guān)于通過(guò)MySQL慢查詢優(yōu)化MySQL性能的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
MySQL分表實(shí)現(xiàn)上百萬(wàn)上千萬(wàn)記錄分布存儲(chǔ)的批量查詢?cè)O(shè)計(jì)模式詳解
本篇文章是對(duì)使用MySQL分表實(shí)現(xiàn)上百萬(wàn)上千萬(wàn)記錄分布存儲(chǔ)的批量查詢?cè)O(shè)計(jì)模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
MySQL的查詢計(jì)劃中ken_len的值計(jì)算方法
本文首先介紹了MySQL的查詢計(jì)劃中ken_len的含義;然后介紹了key_len的計(jì)算方法;最后通過(guò)一個(gè)偽造的例子,來(lái)說(shuō)明如何通過(guò)key_len來(lái)查看聯(lián)合索引有多少列被使用2017-02-02
MySQL安全配置向?qū)ysql_secure_installation詳解
這篇文章主要介紹了MySQL安全配置向?qū)ysql_secure_installation各項(xiàng)配置的含義,并依據(jù)經(jīng)驗(yàn)給予一了一些建議,需要的朋友可以參考下2014-03-03
解決Windows安裝mysql時(shí)提示MSVCR120.DLL動(dòng)態(tài)庫(kù)缺失問(wèn)題
在Windows Server 2012系統(tǒng)上安裝MySQL 5.7時(shí)遇到“由于找不到MSVCR120.dll,無(wú)法繼續(xù)執(zhí)行代碼”的錯(cuò)誤,原因是系統(tǒng)缺少部分配置文件,解決方法是下載并安裝vcredist文件2025-02-02
MySQL數(shù)據(jù)庫(kù)中存儲(chǔ)圖片和讀取圖片的操作代碼
在MySQL數(shù)據(jù)庫(kù)中存儲(chǔ)圖片通常有兩種主要方式:將圖片以二進(jìn)制數(shù)據(jù)(BLOB 類型)直接存儲(chǔ)在數(shù)據(jù)庫(kù)中,或者將圖片文件存儲(chǔ)在服務(wù)器文件系統(tǒng)上,而在數(shù)據(jù)庫(kù)中存儲(chǔ)圖片的路徑或URL,以下是這兩種方法的詳細(xì)解釋,包括存儲(chǔ)和讀取操作,需要的朋友可以參考下2024-11-11
如何選擇合適的MySQL日期時(shí)間類型來(lái)存儲(chǔ)你的時(shí)間
這篇文章主要介紹了如何選擇合適的MySQL日期時(shí)間類型來(lái)存儲(chǔ)你的時(shí)間,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-08-08

