MySQL 數(shù)據(jù)庫雙向鏡像、循環(huán)鏡像(復制)
更新時間: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進行,結(jié)果會自動鏡像到數(shù)據(jù)庫B中。但是對數(shù)據(jù)庫B的直接操作,不會影響數(shù)據(jù)庫A。
對于雙向數(shù)據(jù)庫鏡像,就是數(shù)據(jù)庫A的數(shù)據(jù)變化要鏡像到數(shù)據(jù)庫B中,同時數(shù)據(jù)庫B里的修改也要同時復制到數(shù)據(jù)庫A里。
對于循環(huán)數(shù)據(jù)庫鏡像,就是多個數(shù)據(jù)庫A、B、C、D等,對其中任一個數(shù)據(jù)庫的修改,都要同時鏡像到其它的數(shù)據(jù)庫里。
應用:同一個Zen Cart網(wǎng)店的數(shù)據(jù)庫和程序,可以放置在不同的主機上,在任一臺主機上新增的訂單、客戶資料,都會同時加入其它的主機數(shù)據(jù)庫里。
要實現(xiàn)雙向或循環(huán)數(shù)據(jù)庫鏡像,首先要解決的就是防止數(shù)據(jù)庫中自動遞增(AUTO_INCREMENT)字段的沖突,以免多數(shù)據(jù)庫各自生成一樣的增量值。
下面以三臺主機循環(huán)鏡像為例,A是B的主鏡像,B是C的主鏡像,C是A的主鏡像。三臺主機上MySQL設置文件 /etc /my.cnf 中分別加入下面的參數(shù):
# 主機一:美國主機 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
# 主機二:中國主機 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
# 主機三:本地主機 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ù)庫標識,每個數(shù)據(jù)庫標識必須唯一;
replicate-same-server-id:設置為0,防止數(shù)據(jù)循環(huán)更新;
auto_increment_increment:這是循環(huán)鏡像里最重要的參數(shù)之一,表示自動增量為10,這將允許最多10臺數(shù)據(jù)庫加入這個循環(huán)鏡像的陣列,而自動遞增字段不會重復。
auto_increment_offset:這是循環(huán)鏡像里最重要的參數(shù)之一,表示偏移值,每個數(shù)據(jù)庫的偏移值必須唯一,且在1和auto_increment_increment之間。
master-host:主數(shù)據(jù)庫服務器的IP;
master-user:用于連接主數(shù)據(jù)庫的鏡像用戶名;
master-password:用于連接主數(shù)據(jù)庫的鏡像密碼;
report-host:提供給主數(shù)據(jù)庫用于反向連接的IP,因為主數(shù)據(jù)庫有時無法正確判斷從服務器的IP,所以這里最好填上從服務器自己的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ù)庫里。
應用:同一個Zen Cart網(wǎng)店的數(shù)據(jù)庫和程序,可以放置在不同的主機上,在任一臺主機上新增的訂單、客戶資料,都會同時加入其它的主機數(shù)據(jù)庫里。
要實現(xiàn)雙向或循環(huán)數(shù)據(jù)庫鏡像,首先要解決的就是防止數(shù)據(jù)庫中自動遞增(AUTO_INCREMENT)字段的沖突,以免多數(shù)據(jù)庫各自生成一樣的增量值。
下面以三臺主機循環(huán)鏡像為例,A是B的主鏡像,B是C的主鏡像,C是A的主鏡像。三臺主機上MySQL設置文件 /etc /my.cnf 中分別加入下面的參數(shù):
# 主機一:美國主機 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
# 主機二:中國主機 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
# 主機三:本地主機 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ù)庫標識,每個數(shù)據(jù)庫標識必須唯一;
replicate-same-server-id:設置為0,防止數(shù)據(jù)循環(huán)更新;
auto_increment_increment:這是循環(huán)鏡像里最重要的參數(shù)之一,表示自動增量為10,這將允許最多10臺數(shù)據(jù)庫加入這個循環(huán)鏡像的陣列,而自動遞增字段不會重復。
auto_increment_offset:這是循環(huán)鏡像里最重要的參數(shù)之一,表示偏移值,每個數(shù)據(jù)庫的偏移值必須唯一,且在1和auto_increment_increment之間。
master-host:主數(shù)據(jù)庫服務器的IP;
master-user:用于連接主數(shù)據(jù)庫的鏡像用戶名;
master-password:用于連接主數(shù)據(jù)庫的鏡像密碼;
report-host:提供給主數(shù)據(jù)庫用于反向連接的IP,因為主數(shù)據(jù)庫有時無法正確判斷從服務器的IP,所以這里最好填上從服務器自己的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ù)庫。
您可能感興趣的文章:
相關(guān)文章
mysql數(shù)據(jù)庫詳解(基于ubuntu 14.0.4 LTS 64位)
這篇文章主要介紹了mysql數(shù)據(jù)庫詳解(基于ubuntu 14.0.4 LTS 64位),具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
解決MySQL啟動報錯:ERROR 2003 (HY000): Can''t connect to MySQL serv
這篇文章主要介紹了解決MySQL啟動報錯:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061),本文解釋了如何解決該問題,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
win2003服務器下配置 MySQL 群集(Cluster)的方法
MySQL 群集是 MySQL 適合于分布式計算環(huán)境的高可用、高冗余版本。它采用了 NDB Cluster 存儲引擎,允許在 1 個群集中運行多個 MySQL 服務器。2010-12-12
mysql error 1130 hy000:Host''localhost''解決方案
本文將詳細提供mysql error 1130 hy000:Host'localhost'解決方案,需要的朋友可以參考下2012-11-11

