MySQL 數(shù)據(jù)庫雙向鏡像、循環(huán)鏡像(復(fù)制)
更新時間:2011年05月26日 01:14:10 作者:
在MySQL數(shù)據(jù)庫鏡像的貼子中,主數(shù)據(jù)庫A 的數(shù)據(jù)鏡像到從數(shù)據(jù)庫B,是單向的,Zen Cart網(wǎng)店的數(shù)據(jù)讀寫都必須在數(shù)據(jù)庫A進(jìn)行,結(jié)果會自動鏡像到數(shù)據(jù)庫B中。但是對數(shù)據(jù)庫B的直接操作,不會影響數(shù)據(jù)庫A。
對于雙向數(shù)據(jù)庫鏡像,就是數(shù)據(jù)庫A的數(shù)據(jù)變化要鏡像到數(shù)據(jù)庫B中,同時數(shù)據(jù)庫B里的修改也要同時復(fù)制到數(shù)據(jù)庫A里。
對于循環(huán)數(shù)據(jù)庫鏡像,就是多個數(shù)據(jù)庫A、B、C、D等,對其中任一個數(shù)據(jù)庫的修改,都要同時鏡像到其它的數(shù)據(jù)庫里。
應(yīng)用:同一個Zen Cart網(wǎng)店的數(shù)據(jù)庫和程序,可以放置在不同的主機(jī)上,在任一臺主機(jī)上新增的訂單、客戶資料,都會同時加入其它的主機(jī)數(shù)據(jù)庫里。
要實現(xiàn)雙向或循環(huán)數(shù)據(jù)庫鏡像,首先要解決的就是防止數(shù)據(jù)庫中自動遞增(AUTO_INCREMENT)字段的沖突,以免多數(shù)據(jù)庫各自生成一樣的增量值。
下面以三臺主機(jī)循環(huán)鏡像為例,A是B的主鏡像,B是C的主鏡像,C是A的主鏡像。三臺主機(jī)上MySQL設(shè)置文件 /etc /my.cnf 中分別加入下面的參數(shù):
# 主機(jī)一:美國主機(jī) A, IP: 100.101.102.201
[mysqld]
server-id = 10
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 1
master-host = 100.101.102.203
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.201
# 主機(jī)二:中國主機(jī) B, IP: 100.101.102.202
[mysqld]
server-id = 20
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 2
master-host = 100.101.102.201
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.202
# 主機(jī)三:本地主機(jī) C, IP: 100.101.102.203
[mysqld]
server-id = 30
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 3
master-host = 100.101.102.202
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.203
簡單說明:
server-id:數(shù)據(jù)庫標(biāo)識,每個數(shù)據(jù)庫標(biāo)識必須唯一;
replicate-same-server-id:設(shè)置為0,防止數(shù)據(jù)循環(huán)更新;
auto_increment_increment:這是循環(huán)鏡像里最重要的參數(shù)之一,表示自動增量為10,這將允許最多10臺數(shù)據(jù)庫加入這個循環(huán)鏡像的陣列,而自動遞增字段不會重復(fù)。
auto_increment_offset:這是循環(huán)鏡像里最重要的參數(shù)之一,表示偏移值,每個數(shù)據(jù)庫的偏移值必須唯一,且在1和auto_increment_increment之間。
master-host:主數(shù)據(jù)庫服務(wù)器的IP;
master-user:用于連接主數(shù)據(jù)庫的鏡像用戶名;
master-password:用于連接主數(shù)據(jù)庫的鏡像密碼;
report-host:提供給主數(shù)據(jù)庫用于反向連接的IP,因為主數(shù)據(jù)庫有時無法正確判斷從服務(wù)器的IP,所以這里最好填上從服務(wù)器自己的IP地址。
另外,有時只需要鏡像某些數(shù)據(jù)庫,可以在 my.cnf 中加入:
replicate-do-db = db_name1
replicate-do-db = db_name2
replicate-do-db = db_name3
這樣就僅僅鏡像db_name1/db_name2/db_name3
如果只是某些數(shù)據(jù)庫不要鏡像,可以在 my.cnf 中加入:
replicate-ignore-db=db_name1
replicate-ignore-db=db_name2
replicate-ignore-db=db_name3
這樣鏡像時就忽略 db_name1/db_name2/db_name3 這三個數(shù)據(jù)庫。
對于循環(huán)數(shù)據(jù)庫鏡像,就是多個數(shù)據(jù)庫A、B、C、D等,對其中任一個數(shù)據(jù)庫的修改,都要同時鏡像到其它的數(shù)據(jù)庫里。
應(yīng)用:同一個Zen Cart網(wǎng)店的數(shù)據(jù)庫和程序,可以放置在不同的主機(jī)上,在任一臺主機(jī)上新增的訂單、客戶資料,都會同時加入其它的主機(jī)數(shù)據(jù)庫里。
要實現(xiàn)雙向或循環(huán)數(shù)據(jù)庫鏡像,首先要解決的就是防止數(shù)據(jù)庫中自動遞增(AUTO_INCREMENT)字段的沖突,以免多數(shù)據(jù)庫各自生成一樣的增量值。
下面以三臺主機(jī)循環(huán)鏡像為例,A是B的主鏡像,B是C的主鏡像,C是A的主鏡像。三臺主機(jī)上MySQL設(shè)置文件 /etc /my.cnf 中分別加入下面的參數(shù):
# 主機(jī)一:美國主機(jī) A, IP: 100.101.102.201
[mysqld]
server-id = 10
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 1
master-host = 100.101.102.203
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.201
# 主機(jī)二:中國主機(jī) B, IP: 100.101.102.202
[mysqld]
server-id = 20
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 2
master-host = 100.101.102.201
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.202
# 主機(jī)三:本地主機(jī) C, IP: 100.101.102.203
[mysqld]
server-id = 30
log-bin = mysql-bin
log-slave-updates
replicate-same-server-id = 0
auto_increment_increment = 10
auto_increment_offset = 3
master-host = 100.101.102.202
master-user = repl_user
master-password = repl_password
report-host = 100.101.102.203
簡單說明:
server-id:數(shù)據(jù)庫標(biāo)識,每個數(shù)據(jù)庫標(biāo)識必須唯一;
replicate-same-server-id:設(shè)置為0,防止數(shù)據(jù)循環(huán)更新;
auto_increment_increment:這是循環(huán)鏡像里最重要的參數(shù)之一,表示自動增量為10,這將允許最多10臺數(shù)據(jù)庫加入這個循環(huán)鏡像的陣列,而自動遞增字段不會重復(fù)。
auto_increment_offset:這是循環(huán)鏡像里最重要的參數(shù)之一,表示偏移值,每個數(shù)據(jù)庫的偏移值必須唯一,且在1和auto_increment_increment之間。
master-host:主數(shù)據(jù)庫服務(wù)器的IP;
master-user:用于連接主數(shù)據(jù)庫的鏡像用戶名;
master-password:用于連接主數(shù)據(jù)庫的鏡像密碼;
report-host:提供給主數(shù)據(jù)庫用于反向連接的IP,因為主數(shù)據(jù)庫有時無法正確判斷從服務(wù)器的IP,所以這里最好填上從服務(wù)器自己的IP地址。
另外,有時只需要鏡像某些數(shù)據(jù)庫,可以在 my.cnf 中加入:
replicate-do-db = db_name1
replicate-do-db = db_name2
replicate-do-db = db_name3
這樣就僅僅鏡像db_name1/db_name2/db_name3
如果只是某些數(shù)據(jù)庫不要鏡像,可以在 my.cnf 中加入:
replicate-ignore-db=db_name1
replicate-ignore-db=db_name2
replicate-ignore-db=db_name3
這樣鏡像時就忽略 db_name1/db_name2/db_name3 這三個數(shù)據(jù)庫。
您可能感興趣的文章:
- MySQL主從復(fù)制的原理及配置方法(比較詳細(xì))
- mysql中復(fù)制表結(jié)構(gòu)的方法小結(jié)
- MySQL復(fù)制表結(jié)構(gòu)和內(nèi)容到另一張表中的SQL語句
- MySQL快速復(fù)制數(shù)據(jù)庫數(shù)據(jù)表的方法
- Mysql主從復(fù)制(master-slave)實際操作案例
- mysql主從同步復(fù)制錯誤解決一例
- mysql跨數(shù)據(jù)庫復(fù)制表(在同一IP地址中)示例
- mysql同步復(fù)制搭建方法指南詳細(xì)步驟
- 簡單講解MySQL的數(shù)據(jù)庫復(fù)制方法
- 詳解MySQL雙活同步復(fù)制四種解決方案
- mysql 復(fù)制原理與實踐應(yīng)用詳解
相關(guān)文章
Mysql數(shù)據(jù)庫的QPS和TPS的意義和計算方法
今天小編就為大家分享一篇關(guān)于Mysql數(shù)據(jù)庫的QPS和TPS的意義和計算方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03如何通過sql查找所有父節(jié)點和所有子節(jié)點(以mysql為例)
這篇文章主要給大家介紹了關(guān)于如何通過sql查找所有父節(jié)點和所有子節(jié)點,本文以mysql為例,項目中遇到一個需求,要求查處菜單節(jié)點的所有節(jié)點,這里給大家總結(jié)下,需要的朋友可以參考下2023-08-08Mysql時間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條
這篇文章主要介紹了Mysql時間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條 ,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07MySQL和MySQL驅(qū)動mysql-connector-java升級到8.0.X版本問題
這篇文章主要介紹了MySQL和MySQL驅(qū)動mysql-connector-java升級到8.0.X版本問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02如何恢復(fù)Mysql數(shù)據(jù)庫的詳細(xì)介紹
這里說的MySql恢復(fù)數(shù)據(jù)庫,是指沒有通過正常備份的情況下,通過Mysql保存的數(shù)據(jù)文件如何恢復(fù)數(shù)據(jù)庫2013-09-09