Bucardo 是基于表復制的系統(tǒng)
Bucardo 可以實現(xiàn)PostgreSQL數(shù)據(jù)庫的雙master/多master的方案
Bucardo的核心是一個Perl守護進程,它偵聽通知請求并對其進行操作,方法是連接到遠程數(shù)據(jù)庫并來回復制數(shù)據(jù)。
守護進程需要的所有特定信息都存儲在主bucardo數(shù)據(jù)庫中,包括復制所涉及的所有數(shù)據(jù)庫的列表以及如何到達這些數(shù)據(jù)庫、要復制的所有表以及如何復制每個表。
運行Bucardo的第一步是向主Bucardo數(shù)據(jù)庫添加兩個或更多數(shù)據(jù)庫。
完成此操作后,將添加關于要復制哪些表的信息以及表的任何分組。然后添加同步。
同步被稱為復制操作,將一組特定的表從一臺服務器復制到另一臺服務器或一組服務器。
一旦設置好Bucardo,觸發(fā)器就開始存儲有關所有相關的表中哪些行被更改的信息。對于多主機。
過程是這樣的:1、對表進行更改并記錄在bucardo_delta表中。2、向主Bucardo守護進程發(fā)送通知,讓它知道表已經(jīng)更改。3、守護進程通知控制器進行同步,然后返回偵聽。4、控制器創(chuàng)建一個“kid”來處理復制,或者通知已經(jīng)存在的復制。5、孩子開始一個新的事務,禁用相關表上的觸發(fā)器和規(guī)則。6、然后,它收集自上次復制以來哪些行發(fā)生了更改的列表,然后比較這兩個行以確定應該做什么。7、如果存在沖突,則會運行標準沖突處理程序,或者為每個表設置的自定義處理程序來對內(nèi)容進行排序。8、觸發(fā)器和規(guī)則被重新啟用,事務被提交。9、如果事務失敗,則運行任何自定義異常處理程序。10、子程序向控制器發(fā)出它已經(jīng)完成的信號。
|
|
Bucardo 軟件依賴包有如下:
build, test, and install Perl 5 (at least 5.8.3)build, test, and install Postgres (at least 8.2)build, test, and install the DBI module (at least 1.51)build, test, and install the DBD::Pg module (at least 2.0.0)build, test, and install the DBIx::Safe module (at least 1.2.4)
本次系統(tǒng)linux版本是6.5 Perl是linux系統(tǒng)自帶;默認版本是v5.10.1;可行
[root@node3 Bucardo]# perl -vThis is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
在編譯的過程需要帶上 --with-perl;
安裝DBI 依賴 Test-Simple;
# 安裝 Test-SimpleTest-Simple下載地址 https://metacpan.org 搜索 Test-Simple# 解壓Test-Simple源碼包tar -zxvf Test-Simple-1.302183.tar.gz# 進入Test-Simple目錄進行安裝cd Test-Simple-1.302183perl Makefile.PLmakemake install# 接下來安裝DBI# 下載地址:http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.630.tar.gztar -zxvf DBI-1.630.tar.gzcd DBI-1.630perl Makefile.PLmakemake install
安裝最新的DBD::Pg;需要依賴 Time::HiRes
# 安裝 Time::HiRes下載地址: https://metacpan.org 搜索 Time::HiRestar -zxvf Time-HiRes-1.9764.tar.gz cd Time-HiRes-1.9764perl Makefile.PLmakemake install# 接下來安裝DBD::Pg# 安裝DBD::Pg;需要確定已經(jīng)安裝 PostgreSQL 環(huán)境;即pg_config輸出# 加載 PostgreSQL 環(huán)境變量tar -zxvf DBD-Pg-3.14.2.tar.gzcd DBD-Pg-3.14.2perl Makefile.PLmakemake install
# 下載地址:https://bucardo.org/DBIx-Safe/tar -zxvf DBIx-Safe-1.2.5.tar.gzcd DBIx-Safe-1.2.5perl Makefile.PLmakemake install
|
# 在網(wǎng)站https://bucardo.org/Bucardo/下載tar -zxvf Bucardo-5.6.0.tar.gzcd Bucardo-5.6.0perl Makefile.PLmakemake install# 查看Bucardo版本[postgres@node3 ~]$ bucardo --versionbucardo version 5.6.0
|
元數(shù)據(jù)庫存儲bucardo復制的配置信息,使用 bucardo install 命令創(chuàng)建;
|
在系統(tǒng)用戶home目錄下創(chuàng)建;內(nèi)容如下:
log_conflict_file = /home/postgres/bucardo/log/bucardo_conflict.logpiddir = /home/postgres/bucardo/runreason_file = /home/postgres/bucardo/log/bucardo.restart.reason.logwarning_file = /home/postgres/bucardo/log/bucardo.warning.logsyslog_facility = LOG_LOCAL1
|
mkdir -p /home/postgres/bucardo/logmkdir -p /home/postgres/bucardo/run
|
準備工作完成;開始執(zhí)行 bucardo install 命令創(chuàng)建 元數(shù)據(jù)庫;
# /usr/local/share/bucardo/bucardo.schema[postgres@node3 ~]$ bucardo installThis will install the bucardo database into an existing Postgres cluster.Postgres must have been compiled with Perl support,and you must connect as a superuserCurrent connection settings:1. Host: <none>2. Port: 60003. User: bucardo4. Database: bucardo5. PID directory: /home/postgres/bucardo/runEnter a number to change it, P to proceed, or Q to quit: 1-- 推薦使用unix套接字方式連接數(shù)據(jù);可以使用dbuser:postgres;dbname:postgres去連接。Change the host to: /tmpChanged host to: /tmpCurrent connection settings:1. Host: /tmp2. Port: 60003. User: postgres4. Database: postgres5. PID directory: /home/postgres/bucardo/runEnter a number to change it, P to proceed, or Q to quit: PAttempting to create and populate the bucardo database and schemaDatabase creation is completeUpdated configuration setting "piddir"Installation is now complete.If you see errors or need help, please email bucardo-general@bucardo.orgYou may want to check over the configuration variables next, by running:bucardo show allChange any setting by using: bucardo set foo=bar
|
本次簡單演示下;在同個數(shù)據(jù)庫服務器下不同的實例;
用數(shù)據(jù)庫master、slave來演示
[postgres@node3 ~]$ createdb master[postgres@node3 ~]$ createdb slave
|
執(zhí)行bucardo add db命令;記錄同步數(shù)據(jù)庫的連接信息
[postgres@node3 ~]$ bucardo add db db1 host=/tmp dbname=master user=postgresAdded database "db1"[postgres@node3 ~]$ bucardo add db db2 host=/tmp dbname=slave user=postgresAdded database "db2"#這里推薦使用unix套接字;若元數(shù)據(jù)庫跟需要復制的數(shù)據(jù)不是在同一臺;則需要使用TCP/IP連接的方式。bucardo add db db1 host=IP port=PGPORT dbname=PGDB user=PGUSRR password=PGPASSWD
|
添加 dbgroup 數(shù)據(jù)庫組; 在一套復制系統(tǒng)中可以有多個源數(shù)據(jù)庫、目標數(shù)據(jù)庫。指定哪個數(shù)據(jù)庫是源數(shù)據(jù)庫,哪個是目標數(shù)據(jù)庫
[postgres@node3 ~]$ bucardo add dbgroup dbgrp01 db1:source db2:targetCreated dbgroup "dbgrp01"Added database "db1" to dbgroup "dbgrp01" as sourceAdded database "db2" to dbgroup "dbgrp01" as target
|
# 創(chuàng)建復制表tbl_lottu01[postgres@node3 ~]$ psql master postgres -c "create table tbl_lottu01(id int primary key, info text, reg_time timestamp)"CREATE TABLE[postgres@node3 ~]$ psql slave postgres -c "create table tbl_lottu01(id int primary key, info text)"CREATE TABLE# 添加 relgroup 復制集;即要復制的表、序列的集合[postgres@node3 ~]$ bucardo add relgroup relgrp01 tbl_lottu01Created relgroup "relgrp01"Added the following tables or sequences: public.tbl_lottu01 (DB: db1)The following tables or sequences are now part of the relgroup "relgrp01": public.tbl_lottu01
|
添加同步;在創(chuàng)建同步需要指定dbgroup 和relgroup
[postgres@node3 ~]$ bucardo add sync sync01 relgroup=relgrp01 dbgroup=dbgrp01 conflict_strategy=bucardo_latest2020-11-03 17:08:51.494 CST [6988] LOG: Starting validate_sync for sync012020-11-03 17:08:51.494 CST [6988] CONTEXT: PL/Perl function "validate_sync"SQL statement "SELECT validate_sync('sync01')"PL/Perl function "validate_sync"2020-11-03 17:08:51.494 CST [6988] STATEMENT: INSERT INTO bucardo.sync (herd,name,dbs,conflict_strategy) VALUES ($1,$2,$3,$4)2020-11-03 17:08:51.619 CST [6988] LOG: Ending validate_sync for sync012020-11-03 17:08:51.619 CST [6988] CONTEXT: PL/Perl function "validate_sync"SQL statement "SELECT validate_sync('sync01')"PL/Perl function "validate_sync"2020-11-03 17:08:51.619 CST [6988] STATEMENT: INSERT INTO bucardo.sync (herd,name,dbs,conflict_strategy) VALUES ($1,$2,$3,$4)Added sync "sync01"
由于bucardo是解決PostgreSQL雙主/多主的方案??赡軙嬖谥麈I/唯一約束的沖突
"conflict_strategy" The conflict resolution strategy to use in the sync. Supported values: "bucardo_source" The rows on the "source" database always "win". In other words, in a conflict, Bucardo copies rows from source to target. "bucardo_target" The rows on the "target" database always win. "bucardo_skip" Any conflicting rows are simply not replicated. Not recommended for most cases. "bucardo_random" Each database has an equal chance of winning each time. This is the default. "bucardo_latest" The row that was most recently changed wins. "bucardo_abort" The sync is aborted on a conflict.
|
啟動bucardo服務
[postgres@node3 ~]$ bucardo startChecking for existing processesStarting Bucardo[postgres@node3 ~]$ 2020-11-03 17:21:07.440 CST [7178] LOG: Starting validate_sync for sync012020-11-03 17:21:07.440 CST [7178] CONTEXT: PL/Perl function "validate_sync"2020-11-03 17:21:07.440 CST [7178] STATEMENT: SELECT validate_sync('sync01')2020-11-03 17:21:07.494 CST [7178] LOG: Ending validate_sync for sync012020-11-03 17:21:07.494 CST [7178] CONTEXT: PL/Perl function "validate_sync"2020-11-03 17:21:07.494 CST [7178] STATEMENT: SELECT validate_sync('sync01')[postgres@node3 ~]$ ps -ef | grep bucardopostgres 7178 6376 0 17:21 ? 00:00:00 postgres: bucardo bucardo [local] idlepostgres 7184 6376 0 17:21 ? 00:00:00 postgres: bucardo bucardo [local] idlepostgres 7187 6376 0 17:21 ? 00:00:00 postgres: bucardo bucardo [local] idlepostgres 7191 6376 0 17:21 ? 00:00:00 postgres: bucardo bucardo [local] idlepostgres 7196 7066 0 17:21 pts/1 00:00:00 grep bucardo
在源庫插入記錄
[postgres@node3 ~]$ psql master postgrespsql (12.2)Type "help" for help.master=# insert into tbl_lottu01 values (1, 'lottu01');INSERT 0 1 master=# insert into tbl_lottu01 values (2, 'lottu02');INSERT 0 1master=# \q在目標庫查看記錄[postgres@node3 ~]$ psql slave postgrespsql (12.2)Type "help" for help.slave=# select * from tbl_lottu01; id | info ----+--------- 1 | lottu01 2 | lottu02(2 rows)
|
|
Bucardo 可以實現(xiàn)PostgreSQL數(shù)據(jù)庫的雙master/多master的方案;
其實邏輯復制可以設計雙主架構;在原生邏輯流復制針對沖突檢查需要手動處理;但是在pglogical有對沖突處理。bucardo也有沖突處理。
以上面為例
# 添加數(shù)據(jù)庫組;指定db2是源數(shù)據(jù)庫,db1是目標數(shù)據(jù)庫bucardo add dbgroup dbgrp02 db2:source db1:target# 添加同步bucardo add sync sync02 relgroup=relgrp01 dbgroup=dbgrp02 conflict_strategy=bucardo_latest這樣可以完成雙主架構master=# insert into tbl_lottu01 values (3, 'lottu03');INSERT 0 1slave=# insert into tbl_lottu01 values (4, 'lottu04');INSERT 0 1查看master/slave結果是=# select * from tbl_lottu01; id | info ----+--------- 1 | lottu01 2 | lottu02 4 | lottu04 3 | lottu03(4 rows)
|
在生產(chǎn)環(huán)境數(shù)據(jù)庫;都應有測試環(huán)境;有時需要在測試環(huán)境需要最新的數(shù)據(jù)驗證新加的代碼正確性。
# 關閉bucardo服務bucardo stop# 更新同步為增量同步;bucardo update sync sync01 onetimecopy=2 "onetimecopy" 0: 關閉 1: fullcopy;采用delete/copy的方式 2: 增量copy;# 啟動bucardo服務bucardo start
聯(lián)系客服