PostgreSQL的OGG——Bucardo搭建手顺
1、搭建前准备
bucardo是PostgreSQL数据库中实现双向同步的软件,可以实现PostgreSQL数据库的双master的方案,不过bucardo中的同步都是异步的,它是通过触发器记录变化,程序是perl写的.bucardo可以实现postgresql的多主复制、主从同步,甚至可以以postgresql为源库,可以和oracle、mysql、mongodb等很多数据库进行数据异步同步。
而pg原生的stream replication;虽可以同步;只能单向同步。只能是只读操作。而bucardo不支持DDL的同步; 所以说 相当于对stream replication的关系;相当于同oracle中 ogg对dg。
需要在两个节点都执行。
1.1、perl安装
由于bucardo是一个perl语言编写的程序,其依赖PG数据库的plperl语言组件,进而严格依赖perl的版本,所以这里建议安装新版本的perl。
$ tar zxvf perl-5.24.4.tar.gz
$ cd perl-5.24.4
./Configure
make
make install
进行Configure时务必不要指定-d参数,其中有2个重要选项不能采用默认配置:
Build a shared libperl.so (y/n) [n] 这里要选Y
Build a threading Perl? [n] 这里要选Y
安装完成后登陆数据库查看plperl语言组件是否能正确运行:
create language plperlu;
create language plperl;
1.2、bucardo和相关依赖包安装
依次安装test-simple、extutils-makemaker、version,DBI、DBD:Pg、DBIx-Safe:
perl Makefile.PL
make
make install
2、bucardo同步配置
先在两个节点上都建立需要同步的数据库:
createdb bctest
然后为了测试,使用pg_bench工具创建演示的表:
现在主库运行:
pg_bench -i bctest -s 100
在备库同样执行:
pg_bench -i bctest
由于备库是需要同步的表,可以先清除掉备库中表里面的数据:
truncate table pgbench_accounts;
truncate table pgbench_branches;
truncate table pgbench_history;
truncate table pgbench_tellers;
至此,实验环境就已经搭建完成,下面开始同步配置:
2.1、初始化bucardo
首先在当前操作系统用户的HOME目录下面建立文件 .bucardorc
[email protected]> vi .bucardorc
default_email_from = [email protected]
default_email_host = mailhost.myinc.com
default_email_to = [email protected]
log_conflict_file = /home/bill/bucardo/bucardo_conflict.log
piddir = /home/bill/bucardo/run
reason_file = /home/bill/bucardo/log/bucardo.restart.reason.log
warning_file = /home/bill/bucardo/log/bucardo.warning.log
syslog_facility = LOG_LOCAL2
注:需要把对应的目录和文件先手动创建。
然后运行bucardo install命令进行初始化:
Changed database name to: bctest
Current connection settings:
- Host: /home/bill/pgdata
- Port: 1921
- User: bill
- Database: bctest
- PID directory: /home/bill/bucardo/run
Enter a number to change it, P to proceed, or Q to quit: P
Attempting to create and populate the bucardo database and schema
Database creation is complete
Updated configuration setting “piddir”
Installation is now complete.
If you see errors or need help, please email [email protected]
You may want to check over the configuration variables next, by running:
bucardo show all
Change any setting by using: bucardo set foo=bar
这里的user和database需要根据前面设置的进行修改。
2.2、添加数据库信息
bill@docker-> bucardo add db db1 dbname=bctest user=bill
Added database "db1"
bill@docker-> bucardo add db db2 dbname=bctest host=192.168.7.177 user=bill password=bill
Added database "db2"
注:这里添加db2的时候要注意需要先设置bill用户的密码。
2.3、添加表
bill@docker-> bucardo add all tables
New tables added: 4
bill@docker-> bucardo add all sequences
Sorry, no sequences were found
New sequences added: 0
如果要添加某一个表,则可以bucardo add table tablename
2.4、添加群组
bill@docker-> bucardo add relgroup relgroup1 pgbench_branches pgbench_accounts;
Created relgroup "relgroup1"
The following tables or sequences are now part of the relgroup "relgroup1":
public.pgbench_accounts
public.pgbench_branches
bill@docker-> bucardo add dbgroup dbgroup1 db1:source db2:target
Created dbgroup "dbgroup1"
Added database "db1" to dbgroup "dbgroup1" as source
Added database "db2" to dbgroup "dbgroup1" as target
2.5、添加同步
bill@docker-> bucardo add sync sync1 relgroup=relgroup1 dbs=dbgroup1
Added sync "sync1"
这里注意的是;同步到表需要主键约束;不然同步是添加不了的。这个是跟触发器有关系;bucardo是建立在触发器之上的。
2.6、启动bucardo
[email protected]> bucardo start
Checking for existing processes
Removing file "/home/bill/bucardo/run/fullstopbucardo"
Starting Bucardo
–查看状态
bill@docker-> bucardo status
PID of Bucardo MCP: 5796
Name State Last good Time Last I/D Last bad Time
=======+========+============+========+===========+===========+=======
sync1 | Good | 08:29:40 | 4m 52s | 0/0 | none |
至此就已经完成bucardo同步,需要注意bucardo无法同步DDL操作。
上一篇: 不带头结点的单链表之增删查改
下一篇: 文档(流程)