十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用Bucardo5怎么實(shí)現(xiàn)PostgreSQL主數(shù)據(jù)庫復(fù)制,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
為牟定等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及牟定網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、牟定網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
為了演示方便,我使用了亞馬遜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賬戶通過SSH協(xié)議登陸到服務(wù)器,這時(shí)就可以開始安裝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ù)庫:
$ sudo chmod 777 /var/run/postgresql $ pg_ctl -D btest -l logfile start $ createdb shake1
接下來我們就可以進(jìn)行數(shù)據(jù)庫復(fù)制了!為了得到樣例數(shù)據(jù),我使用了開放源代碼的Shakespeare項(xiàng)目。它有一個(gè)易于裝載的小型的、可任意使用的、簡(jiǎn)單的數(shù)據(jù)庫模式。github上的這個(gè)小型項(xiàng)目就包含了一個(gè)現(xiàn)成的PostgreSQL數(shù)據(jù)庫模式,現(xiàn)在我們將可以把它裝載到新的數(shù)據(jù)庫了:
$ 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ù)庫的副本,這些副本可被當(dāng)作其他數(shù)據(jù)源。換個(gè)說法,這些服務(wù)器擁有相同的數(shù)據(jù)而且可以寫入。實(shí)現(xiàn)這些非常簡(jiǎn)單:
$ createdb shake2 -T shake1 $ createdb shake3 -T shake1
Bucardo需要安裝一些依賴包。如果你安裝的操作系統(tǒng)發(fā)布不同,那么你可能要安裝的依賴包就不同:下面是我寫這篇文章的時(shí)候Amazon Linux需要安裝的依賴包。(如果幸運(yùn)的話,你的發(fā)布包可能已經(jīng)包含了Bucardo,在這種情況下,下面的執(zhí)行步驟就不需要執(zhí)行了,你只要運(yùn)行"yum install bucard"就可以了-不過要確定一下你使用的是版本5或者更好的版本!(通過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軟件倉庫里不包含Perl模塊DBIx::Safe,因此我們需要通過CPAN來安裝這個(gè)模塊。一旦上面的所有依賴都安裝成功,這時(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ù)庫,其中包含Bucardo服務(wù)進(jìn)程所需的信息:
$ mkdir pid $ echo -e "piddir=pid\nlogdest=." > .bucardorc $ bucardo install --batch --quiet Creating superuser 'bucardo'
現(xiàn)在已經(jīng)安裝好Bucardo,接下來就準(zhǔn)備復(fù)制了。此時(shí),我們有了三個(gè)可以彼此復(fù)制的數(shù)據(jù)庫。下面我們只使用了兩條命令就可以實(shí)現(xiàn)三數(shù)據(jù)庫彼此復(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ù)庫,我們告訴Bucardo數(shù)據(jù)庫的名字,然后Bucardo把這三個(gè)數(shù)據(jù)庫看作(s1,s2,s3)。你還可以指定端口和主機(jī),不過在這個(gè)例子里,默認(rèn)的端口為5432,而且不需要主機(jī)(采用的是Unix Socket通信機(jī)制)。
第二條命令創(chuàng)建了一個(gè)已命名的復(fù)制系統(tǒng),其sync名稱為bard。Bucardo需要知道復(fù)制到哪兒和如何復(fù)制,因此我們告訴它使用三個(gè)數(shù)據(jù)庫s1,s2和s3。每一個(gè)數(shù)據(jù)庫都可以作為源數(shù)據(jù)庫,因此我們給它們添加了這樣的信息。最后我們需要知道要復(fù)制什么。在這個(gè)例子里,我們需要復(fù)制的是所有表(或者更精確點(diǎn),復(fù)制具有主鍵或者唯一索引的所有數(shù)據(jù)庫)。注意: Bucardo總是把數(shù)據(jù)庫和表放在命名組里-在這個(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ù)庫了:
$ 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ù)庫上首先運(yùn)行的是delete,然后才運(yùn)行insert(技術(shù)上采用的COPY)。接下來我們看看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ù)庫是獲勝者,現(xiàn)在所有三個(gè)數(shù)據(jù)庫具有與最后一個(gè)更新數(shù)據(jù)庫相同的行。
$ 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
我們開發(fā)這個(gè)示例的時(shí)候,Bucardo有時(shí)運(yùn)行的非??欤詻]有發(fā)生沖突。也就是說,因?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)換。如果你有任何問題,請(qǐng)?jiān)谙旅娴脑u(píng)論里說明,或者寫一封短信給Bucardo郵件列表bucardo-general@bucardo.org。
上述就是小編為大家分享的使用Bucardo5怎么實(shí)現(xiàn)PostgreSQL主數(shù)據(jù)庫復(fù)制了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。