下一代異步多個(gè)主數(shù)據(jù)庫(kù)復(fù)制系統(tǒng)Bucardo 5發(fā)布了。這個(gè)版本刪除了老版本中兩個(gè)數(shù)據(jù)庫(kù)源的限制,允許有更多的源數(shù)據(jù)庫(kù)(即主數(shù)據(jù)庫(kù))以及更多的目標(biāo)數(shù)據(jù)庫(kù)(即備份數(shù)據(jù)庫(kù))。Bucardo還可以復(fù)制到其他類(lèi)型的目標(biāo)數(shù)據(jù)庫(kù),其中包括MySQL、MariaDB、Oracle、SQLite、MongoDB和Redis。Bucardo已經(jīng)被完全重寫(xiě)了,這個(gè)版本比前一版本Bucardo 4功能更強(qiáng)大,效率更高。你可以訪問(wèn)Bucardo wiki查找最新版本的Bucardo。
這篇文章快速的介紹了一下Bucardo。以后的博客文章將會(huì)介紹Bucardo的強(qiáng)大功能,現(xiàn)在我們將介紹如何簡(jiǎn)單地實(shí)現(xiàn)多主數(shù)據(jù)庫(kù)復(fù)制。
為了演示方便,我使用了亞馬遜Web服務(wù)(AWS)提供的可快速創(chuàng)建、隨意使用的服務(wù)器,即運(yùn)行Amazon Linux的基本t1.micro服務(wù)器。如果你按照提示繼續(xù)的話,它將免費(fèi)而且簡(jiǎn)單地給你創(chuàng)建一個(gè)服務(wù)器實(shí)例。一旦實(shí)例創(chuàng)建成功,我們就可以使用ec2-user賬戶通過(guò)SSH協(xié)議登陸到服務(wù)器,這時(shí)就可以開(kāi)始安裝PostgreSQL和Bucardo了。
# Always a good idea:
$ sudo yum update
# This also installs other postgresql packages:
$ sudo yum install postgresql-plperl
# Create a new Postgres cluster:
$ initdb btest
此時(shí),我們?nèi)匀徊荒芷诖赌銈€(gè)PostgreSQL,因?yàn)檫@個(gè)發(fā)布版的socket通信目錄使用的是/var/run/postgresql和/tmp。我們調(diào)整了第一個(gè)目錄的權(quán)限后就可以啟動(dòng)PostgreSQL了,然后創(chuàng)建第一個(gè)測(cè)試數(shù)據(jù)庫(kù):
$ sudo chmod 777 /var/run/postgresql
$ pg_ctl -D btest -l logfile start
$ createdb shake1
接下來(lái)我們就可以進(jìn)行數(shù)據(jù)庫(kù)復(fù)制了!為了得到樣例數(shù)據(jù),我使用了開(kāi)放源代碼的Shakespeare項(xiàng)目。它有一個(gè)易于裝載的小型的、可任意使用的、簡(jiǎn)單的數(shù)據(jù)庫(kù)模式。github上的這個(gè)小型項(xiàng)目就包含了一個(gè)現(xiàn)成的PostgreSQL數(shù)據(jù)庫(kù)模式,現(xiàn)在我們將可以把它裝載到新的數(shù)據(jù)庫(kù)了:
$ sudo yum install git
$ git clone -q https://github.com/catherinedevlin/opensourceshakespeare.git
$ psql shake1 -q -f opensourceshakespeare/shakespeare.sql
# You can safely ignore the 'role does not exist' errors
我們打算創(chuàng)建這個(gè)數(shù)據(jù)庫(kù)的副本,這些副本可被當(dāng)作其他數(shù)據(jù)源。換個(gè)說(shuō)法,這些服務(wù)器擁有相同的數(shù)據(jù)而且可以寫(xiě)入。實(shí)現(xiàn)這些非常簡(jiǎn)單:
$ createdb shake2 -T shake1
$ createdb shake3 -T shake1
Bucardo需要安裝一些依賴(lài)包。如果你安裝的操作系統(tǒng)發(fā)布不同,那么你可能要安裝的依賴(lài)包就不同:下面是我寫(xiě)這篇文章的時(shí)候Amazon Linux需要安裝的依賴(lài)包。(如果幸運(yùn)的話,你的發(fā)布包可能已經(jīng)包含了Bucardo,在這種情況下,下面的執(zhí)行步驟就不需要執(zhí)行了,你只要運(yùn)行"yum install bucard"就可以了-不過(guò)要確定一下你使用的是版本5或者更好的版本!(通過(guò)yum info bucardo查看))
$ sudo yum install perl-ExtUtils-MakeMaker perl-DBD-Pg \
> perl-Encode-Locale perl-Sys-Syslog perl-boolean \
> perl-Time-HiRes perl-Test-Simple perl-Pod-Parser
$ sudo yum install cpan
$ echo y | cpan DBIx::Safe
在這個(gè)系統(tǒng)的yum軟件倉(cāng)庫(kù)里不包含Perl模塊DBIx::Safe,因此我們需要通過(guò)CPAN來(lái)安裝這個(gè)模塊。一旦上面的所有依賴(lài)都安裝成功,這時(shí)我們就準(zhǔn)備安裝Bucardo。我們將獲取官方壓縮包,驗(yàn)證、解壓,接著安裝:
$ wget -nv http://bucardo.org/Bucardo.tar.gz
$ wget -nv http://bucardo.org/Bucardo.tar.gz.asc
$ gpg -q --keyserver pgp.mit.edu --recv-key 14964AC8
$ gpg --verify Bucardo.tar.gz.asc
$ tar xfz Bucardo.tar.gz $ ln -s Bucardo-5.0.0 bucardo
$ cd bucardo
$ perl Makefile.PL
$ make
$ sudo make install
我們對(duì)bucardorc文件(設(shè)置某些全局信息的文件)進(jìn)行某些小的調(diào)整。然后運(yùn)行"bucardo install",這條命令將創(chuàng)建bucardo的主數(shù)據(jù)庫(kù),其中包含Bucardo服務(wù)進(jìn)程所需的信息:
$ mkdir pid
$ echo -e "piddir=pid\nlogdest=." > .bucardorc
$ bucardo install --batch --quiet
Creating superuser 'bucardo'
現(xiàn)在已經(jīng)安裝好Bucardo,接下來(lái)就準(zhǔn)備復(fù)制了。此時(shí),我們有了三個(gè)可以彼此復(fù)制的數(shù)據(jù)庫(kù)。下面我們只使用了兩條命令就可以實(shí)現(xiàn)三數(shù)據(jù)庫(kù)彼此復(fù)制:
bucardo add dbs s1,s2,s3 dbname=shake1,shake2,shake3
Added databases "s1","s2","s3"
$ bucardo add sync bard dbs=s1:source,s2:source,s3:source tables=all
Added sync "bard"
Created a new relgroup named "bard"
Created a new dbgroup named "bard"
Added table "public.chapter"
Added table "public.character"
Added table "public.character_work"
Added table "public.paragraph"
Added table "public.wordform"
Added table "public.work"
第一條命令,我們告訴Bucardo如何連接到三個(gè)數(shù)據(jù)庫(kù),我們告訴Bucardo數(shù)據(jù)庫(kù)的名字,然后Bucardo把這三個(gè)數(shù)據(jù)庫(kù)看作(s1,s2,s3)。你還可以指定端口和主機(jī),不過(guò)在這個(gè)例子里,默認(rèn)的端口為5432,而且不需要主機(jī)(采用的是Unix Socket通信機(jī)制)。
第二條命令創(chuàng)建了一個(gè)已命名的復(fù)制系統(tǒng),其sync名稱(chēng)為bard。Bucardo需要知道復(fù)制到哪兒和如何復(fù)制,因此我們告訴它使用三個(gè)數(shù)據(jù)庫(kù)s1,s2和s3。每一個(gè)數(shù)據(jù)庫(kù)都可以作為源數(shù)據(jù)庫(kù),因此我們給它們添加了這樣的信息。最后我們需要知道要復(fù)制什么。在這個(gè)例子里,我們需要復(fù)制的是所有表(或者更精確點(diǎn),復(fù)制具有主鍵或者唯一索引的所有數(shù)據(jù)庫(kù))。注意: Bucardo總是把數(shù)據(jù)庫(kù)和表放在命名組里-在這個(gè)例子里我們只是硬編碼其為10,然而通常這個(gè)值是表格視圖控制器數(shù)組的長(zhǎng)度?,F(xiàn)在例子里,這一切都是自動(dòng)進(jìn)行的,dbgroup和relgroup都是以sync的名字命名的。
我們驗(yàn)證一下復(fù)制是否運(yùn)行,即檢查一下更新行是否復(fù)制到sync里包含的所有數(shù)據(jù)庫(kù)了:
$ bucardo start
$ psql shake1 -c \
> "update character set speechcount=123 where charname='Hamlet'"
UPDATE 1
$ for i in {1,2,3}; do psql shake$i -tc "select \
> current_database(), speechcount from character \
> where charname='Hamlet'"; done | grep s
shake1 | 123
shake2 | 123
shake3 | 123
我們還可以查看Bucardo的日志文件"log.bucardo",看看是否有復(fù)制操作:
$ tail -2 log.bucardo
(25181) KID (bard) Delta count for s1.public."character": 1
(25181) KID (bard) Totals: deletes=2 inserts=2 conflicts=0
上面出現(xiàn)了兩條delete和兩條insert命令,這是因?yàn)楦乱恍幸馕吨谄渌麅蓚€(gè)數(shù)據(jù)庫(kù)上首先運(yùn)行的是delete,然后才運(yùn)行insert(技術(shù)上采用的COPY)。接下來(lái)我們看看Bucardo是怎么處理沖突的。我們將對(duì)所有服務(wù)器上的同一行進(jìn)行更新,這樣就會(huì)產(chǎn)生沖突:
$ for i in {1,2,3}; do psql shake$i -tc \
> "update character set speechcount=$i$i$i \
> where charname='Hamlet'"; done
UPDATE 1
UPDATE 1
UPDATE 1
查看日志表明確實(shí)存在沖突,而且也很好的解決了沖突。默認(rèn)的沖突解決方案表明:最后一個(gè)更新的數(shù)據(jù)庫(kù)是獲勝者,現(xiàn)在所有三個(gè)數(shù)據(jù)庫(kù)具有與最后一個(gè)更新數(shù)據(jù)庫(kù)相同的行。
$ tail log.bucardo
(25181) KID (bard) Delta count for s1.public."character": 1
(25181) KID (bard) Delta count for s2.public."character": 1
(25181) KID (bard) Delta count for s3.public."character": 1
(25181) KID (bard) Conflicts for public."character": 1
(25181) KID (bard) Conflicts have been resolved
(25181) KID (bard) Totals: deletes=2 inserts=2 conflicts=1
$ for i in {1,2,3}; do psql shake$i -tc \
> "select current_database(), speechcount \
> from character where charname='Hamlet'"; done | grep s
shake1 | 333
shake2 | 333
shake3 | 333
我們開(kāi)發(fā)這個(gè)示例的時(shí)候,Bucardo有時(shí)運(yùn)行的非??欤詻](méi)有發(fā)生沖突。也就是說(shuō),因?yàn)楦聲r(shí)順序執(zhí)行的。所以在下一個(gè)更新之前,存在一個(gè)時(shí)間窗口可以讓Bucardo完成更新的復(fù)制。另外,“暫停sync"功能也非常方便,只要在你需要暫時(shí)停止運(yùn)行sync的情況下,運(yùn)行下面命令即可:
$ bucardo pause bard
Syncs paused: bard
$ psql shake1 -c "update character set speechcount=1234 where charname='Hamlet'"
UPDATE 1
$ psql shake2 -c "update character set speechcount=4321 where charname='Hamlet'"
UPDATE 1
$ bucardo resume bard
Syncs resumed: bard
$ tail log.bucardo
(27344) KID (bard) Delta count for s1.public."character": 1
(27344) KID (bard) Delta count for s2.public."character": 1
(27344) KID (bard) Conflicts for public."character": 1
(27344) KID (bard) Conflicts have been resolved
(27344) KID (bard) Totals: deletes=2 inserts=2 conflicts=1
Bucardo 5比我們?cè)谶@兒演示的功能多很多。以后的博客文章里我們將包含它可以完成的其他功能,從復(fù)制到比如Oracle、Mysql或者M(jìn)ongoDB等非PostgreSQL系統(tǒng)到使用自定義的沖突解決方案。以及復(fù)制時(shí)對(duì)正在運(yùn)行的數(shù)據(jù)實(shí)行轉(zhuǎn)換。如果你有任何問(wèn)題,請(qǐng)?jiān)谙旅娴脑u(píng)論里說(shuō)明,或者寫(xiě)一封短信給Bucardo郵件列表bucardo-general@bucardo.org。
這么多年,如果沒(méi)有許多人貢獻(xiàn)代碼、提出漏洞、測(cè)試Bucardo以及詢(xún)問(wèn)(或者回答?。┲卮髥?wèn)題,就不可能有這個(gè)重大版本的發(fā)布。查看 Changes文件,你就可以看到部分貢獻(xiàn)者的列表。謝謝你們所有人,特別感謝Jon Jensen,是他在很久之前就開(kāi)啟了這個(gè)項(xiàng)目。
您可能感興趣的文章:- PostgreSQL中Slony-I同步復(fù)制部署教程
- Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法
- Windows下PostgreSQL安裝圖解
- 15個(gè)postgresql數(shù)據(jù)庫(kù)實(shí)用命令分享
- PostgreSQL 安裝和簡(jiǎn)單使用
- PostgreSQL 數(shù)據(jù)庫(kù)性能提升的幾個(gè)方面
- PostgreSQL新手入門(mén)教程
- Postgresql主從異步流復(fù)制方案的深入探究