MySQL 數(shù)據(jù)庫兩臺(tái)主機(jī)同步實(shí)戰(zhàn)(linux)
當(dāng)一個(gè)從服務(wù)器連接到主服務(wù)器時(shí),它通知主服務(wù)器從服務(wù)器在日志中讀取的最后一次成功更新的位置。從服務(wù)器接收從那時(shí)起發(fā)生的任何更新,然后封鎖并等待主服務(wù)器通知下一次更新。
在實(shí)際項(xiàng)目中,兩臺(tái)分布于異地的主機(jī)上安裝有MySQL數(shù)據(jù)庫,兩臺(tái)服務(wù)器互為主備,客戶要求當(dāng)其中一臺(tái)機(jī)器出現(xiàn)故障時(shí),另外一臺(tái)能夠接管服務(wù)器上的應(yīng)用,這就需要兩臺(tái)數(shù)據(jù)庫的數(shù)據(jù)要實(shí)時(shí)保持一致,在這里使用MySQL的同步功能實(shí)現(xiàn)雙機(jī)的同步復(fù)制。
以下是操作實(shí)例:
1、數(shù)據(jù)庫同步設(shè)置
主機(jī)操作系統(tǒng):RedHat Enterprise Linux 5
數(shù)據(jù)庫版本:MySQL Ver 14.12 Distrib 5.0.22
前提:MySQL數(shù)據(jù)庫正常啟動(dòng)
假設(shè)兩臺(tái)主機(jī)地址分別為:
ServA:10.240.136.9
ServB:10.240.136.149
1.1 配置同步賬號(hào)
在ServA上增加一個(gè)ServB可以登錄的帳號(hào):
MySQL>GRANT all privileges ON *.* TO tongbu@'10.240.136.149' IDENTIFIED BY '123456'; |
在ServB上增加一個(gè)ServA可以登錄的帳號(hào):
MySQL>GRANT all privileges ON *.* TO tongbu@'10.240.136.9' IDENTIFIED BY '123456'; |
1.2 配置數(shù)據(jù)庫參數(shù)
1、以root用戶登錄ServA,修改ServA的my.cnf文件
vi /etc/my.cnf |
在[MySQLd]的配置項(xiàng)中增加如下配置:
1 default-character-set=utf8
2
3 log-bin=MySQL-bin
4
5 relay-log=relay-bin
6
7 relay-log-index=relay-bin-index
8
9 server-id=1
10
11 master-host=10.240.136.149
12
13 master-user=tongbu
14
15 master-password=123456
16
17 master-port=3306
18
19 master-connect-retry=30
20
21 binlog-do-db=umsdb
22
23 replicate-do-db=umsdb
24
25 replicate-ignore-table=umsdb.boco_tb_menu
26
27 replicate-ignore-table=umsdb.boco_tb_connect_log
28
29 replicate-ignore-table=umsdb.boco_tb_data_stat
30
31 replicate-ignore-table=umsdb.boco_tb_log_record
32
33 replicate-ignore-table=umsdb.boco_tb_workorder_record
2、以root用戶登錄ServB,修改ServB的my.cnf文件
vi /etc/my.cnf |
在[MySQLd]的配置項(xiàng)中增加如下配置:
1 default-character-set=utf8
2
3 log-bin=MySQL-bin
4
5 relay-log=relay-bin
6
7 relay-log-index=relay-bin-index
8
9 server-id=2
10
11 master-host=10.240.136.9
12
13 master-user=tongbu
14
15 master-password=123456
16
17 master-port=3306
18
19 master-connect-retry=30
20
21 binlog-do-db=umsdb
22
23 replicate-do-db=umsdb
24
25 replicate-ignore-table=umsdb.boco_tb_menu
26
27 replicate-ignore-table=umsdb.boco_tb_connect_log
28
29 replicate-ignore-table=umsdb.boco_tb_data_stat
30
31 replicate-ignore-table=umsdb.boco_tb_log_record
32
33 replicate-ignore-table=umsdb.boco_tb_workorder_record
1.3 手工執(zhí)行數(shù)據(jù)庫同步
假設(shè)以ServA為主服務(wù)器,在ServB上重啟MySQL:
service MySQLd restart |
在ServB上用root用戶登錄MySQL,執(zhí)行:
MySQL> stop slave; |
在ServA上重啟MySQL:
service MySQLd restart |
1.4 查看數(shù)據(jù)庫同步狀態(tài)
在MySQL命令提示符下執(zhí)行:
MySQL> show slave status\G |
將顯示同步進(jìn)程的狀態(tài),如下所示,兩行藍(lán)色字體為slave進(jìn)程狀態(tài),如果都為yes表示正常;紅色字體表示同步錯(cuò)誤指示,如果有問題會(huì)有錯(cuò)誤提示:
1 *************************** 1. row ***************************
2
3 Slave_IO_State: Waiting for master to send event
4
5 Master_Host: 10.21.2.90
6
7 Master_User: tongbu
8
9 Master_Port: 3306
10
11 Connect_Retry: 30
12
13 Master_Log_File: localhost-bin.000005
14
15 Read_Master_Log_Pos: 39753882
16
17 Relay_Log_File: localhost-relay-bin.000062
18
19 Relay_Log_Pos: 9826663
20
21 Relay_Master_Log_File: localhost-bin.000005
22
23 Slave_IO_Running: Yes
24
25 Slave_SQL_Running: Yes
26
27 Replicate_Do_DB: bak,umsdb
28
29 Replicate_Ignore_DB:
30
31 Replicate_Do_Table:
32
33 Replicate_Ignore_Table: umsdb.boco_tb_connect_log,umsdb.boco_tb_menu,umsdb.boco_tb_workorder_record,
umsdb.boco_tb_data_stat,umsdb.boco_tb_log_record
34
35 Replicate_Wild_Do_Table:
36
37 Replicate_Wild_Ignore_Table:
38
39 Last_Errno: 0
40
41 Last_Error:
42
43 Skip_Counter: 0
44
45 Exec_Master_Log_Pos: 39753882
46
47 Relay_Log_Space: 9826663
48
49 Until_Condition: None
50
51 Until_Log_File:
52
53 Until_Log_Pos: 0
54
55 Master_SSL_Allowed: No
56
57 Master_SSL_CA_File:
58
59 Master_SSL_CA_Path:
60
61 Master_SSL_Cert:
62
63 Master_SSL_Cipher:
64
65 Master_SSL_Key:
66
67 Seconds_Behind_Master:
3、數(shù)據(jù)庫同步測(cè)試
配置完數(shù)據(jù)庫后進(jìn)行測(cè)試,首先在網(wǎng)絡(luò)正常情況下測(cè)試,在ServA上進(jìn)行數(shù)據(jù)庫操作,和在ServB上進(jìn)行數(shù)據(jù)庫操作,數(shù)據(jù)都能夠同步過去。
拔掉ServB主機(jī)上的網(wǎng)線,然后在ServA上做一些數(shù)據(jù)庫操作,之后再恢復(fù)ServB的網(wǎng)絡(luò)環(huán)境,但是在ServB上卻看不到同步的數(shù)據(jù),通過命令show slave status\G查看發(fā)現(xiàn)Slave_IO_Running的狀態(tài)是No,這種狀態(tài)持續(xù)很長一段時(shí)間,數(shù)據(jù)才能同步到ServB上去。這是什么問題呢?同步延遲不會(huì)這么大吧。后來通過網(wǎng)上查找相關(guān)資料,找到一個(gè)同步延遲相關(guān)的參數(shù):
--slave-net-timeout=seconds |
參數(shù)含義:當(dāng)slave從主數(shù)據(jù)庫讀取log數(shù)據(jù)失敗后,等待多久重新建立連接并獲取數(shù)據(jù)。
于是在配置文件中增加該參數(shù),設(shè)置為60秒
slave-net-timeout=60 |
重啟MySQL數(shù)據(jù)庫后測(cè)試,該問題解決。
4、 數(shù)據(jù)庫同步失效的解決
當(dāng)數(shù)據(jù)同步進(jìn)程失效后,首先手工檢查slave主機(jī)當(dāng)前備份的數(shù)據(jù)庫日志文件在master主機(jī)上是否存在,在slave主機(jī)上運(yùn)行:
MySQL> show slave status\G |
一般獲得如下的信息:
1 *************************** 1. row ***************************
2
3 Slave_IO_State: Waiting for master to send event
4
5 Master_Host: 10.21.3.240
6
7 Master_User: tongbu
8
9 Master_Port: 3306
10
11 Connect_Retry: 30
12
13 Master_Log_File: MySQL-bin.000001
14
15 Read_Master_Log_Pos: 360
16
17 Relay_Log_File: localhost-relay-bin.000003
18
19 Relay_Log_Pos: 497
20
21 Relay_Master_Log_File: MySQL-bin.000001
22
23 Slave_IO_Running: Yes
24
25 Slave_SQL_Running: Yes
26
27 Replicate_Do_DB: bak
28
29 Replicate_Ignore_DB:
30
31 Replicate_Do_Table:
32
33 Replicate_Ignore_Table:
34
35 Replicate_Wild_Do_Table:
36
37 Replicate_Wild_Ignore_Table:
38
39 Last_Errno: 0
40
41 Last_Error:
42
43 Skip_Counter: 0
44
45 Exec_Master_Log_Pos: 360
46
47 Relay_Log_Space: 497
48
49 Until_Condition: None
50
51 Until_Log_File:
52
53 Until_Log_Pos: 0
54
55 Master_SSL_Allowed: No
56
57 Master_SSL_CA_File:
58
59 Master_SSL_CA_Path:
60
61 Master_SSL_Cert:
62
63 Master_SSL_Cipher:
64
65 Master_SSL_Key:
66
67 Seconds_Behind_Master: 0
其中Master_Log_File描述的是master主機(jī)上的日志文件。
在master上檢查當(dāng)前的數(shù)據(jù)庫列表:
MySQL> show master logs; |
得到的日志列表如下:
+----------------------+-----------+
| Log_name | File_size |
+----------------------+-----------+
| localhost-bin.000001 | 495 |
| localhost-bin.000002 | 3394 |
+----------------------+-----------+
如果slave主機(jī)上使用的的Master_Log_File對(duì)應(yīng)的文件在master的日志列表中存在,在slave主機(jī)上開啟從屬服務(wù)器線程后可以自動(dòng)同步:
MySQL> start slave; |
如果master主機(jī)上的日志文件已經(jīng)不存在,則需要首先從master主機(jī)上恢復(fù)全部數(shù)據(jù),再開啟同步機(jī)制。
在slave主機(jī)上運(yùn)行:
MySQL> stop slave; |
在master主機(jī)上運(yùn)行:
MySQL> stop slave; |
在slave主機(jī)上運(yùn)行:
MySQL> load data from master; |
在master主機(jī)上運(yùn)行:
MySQL> reset slave; |
注意:LOAD DATA FROM MASTER目前只在所有表使用MyISAM存儲(chǔ)引擎的數(shù)據(jù)庫上有效。
- Linux下MySQL數(shù)據(jù)庫的主從同步復(fù)制配置
- MYSQL5.6.33數(shù)據(jù)庫主從(Master/Slave)同步安裝與配置詳解(Master-Linux Slave-windows7)
- Linux下rsync遠(yuǎn)程數(shù)據(jù)同步命令的詳細(xì)介紹
- linux下實(shí)現(xiàn)web數(shù)據(jù)同步的四種方式(性能比較)
- linux下指定mysql數(shù)據(jù)庫服務(wù)器主從同步的配置實(shí)例
- Linux下指定mysql數(shù)據(jù)庫數(shù)據(jù)配置主主同步的實(shí)例
- linux下mysql數(shù)據(jù)庫單向同步配置方法分享
- cwrsync實(shí)現(xiàn)從linux到windows的數(shù)據(jù)同步備份
- Linux下sersync數(shù)據(jù)實(shí)時(shí)同步
相關(guān)文章
mysql無法啟動(dòng)服務(wù)及其他問題總結(jié)
MySQL無法啟動(dòng),可能有多種原因?qū)е?本文主要介紹了mysql無法啟動(dòng)服務(wù)及其他問題總結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01mysql根據(jù)拼音字母查詢(簡單易懂的字段拼音查詢)
MySQL在開發(fā)中,我們經(jīng)常需要根據(jù)字段拼音查詢數(shù)據(jù)庫中的數(shù)據(jù),它支持多種查詢方式,包括根據(jù)拼音字母查詢,使用 Collation 可以方便地進(jìn)行簡單的拼音查詢,而使用拼音索引可以大幅提高查詢性能,根據(jù)具體的需求和情況,我們可以選擇合適的方法來實(shí)現(xiàn)拼音查詢2023-10-10關(guān)于Mysql5.7及8.0版本索引失效情況匯總
這篇文章主要介紹了關(guān)于Mysql5.7及8.0版本索引失效情況匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08SQL Server 2005 安裝遇到的錯(cuò)誤提示和解決方法
在安裝SQL Server 2005時(shí)有時(shí)會(huì)出現(xiàn)意想不到的問題,如IIS,性能計(jì)數(shù)器,OWC11,無法配置外圍應(yīng)用的問題,下面筆者分享一下在安裝SQL Server 2005時(shí)常見問題解決方法2014-01-01mysql數(shù)據(jù)庫無法被其他ip訪問的解決方法
這篇文章主要給大家介紹了關(guān)于mysql數(shù)據(jù)庫無法被其他ip訪問的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09