使用Bucardo搭建PG数据库同步
1、Bucardo介绍
Bucardo是针对PostgreSQL数据库开发的同步软件,可以实现PostgreSQL数据库的主从、主主、多主同步方案,不过bucardo中的同步都是异步的,它是通过触发器记录变化,甚至可以以postgresql为源库,可以和oracle、mysql、mongodb等很多数据库进行数据异步同步。
2、Bucardo搭建前准备
2.1搭建环境
版本要求:
Perl 5                (at least 5.8.3)
Postgres              (at least 8.2)
DBI module            (at least 1.51)perl怎么下载
DBD::Pg module        (at least 2.0.0)
DBIx::Safe module     (at least 1.2.4)
Linux版本
cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
2.2 Perl安装(所有节点都需要安装)
本次系统linux版本是7.6 Perl是linux系统自带;默认版本是v5.16.1;可行
[root@node3 Bucardo]# perl -v
This is perl, v5.16.1 (*) built for x86_64-linux-thread-multi
如果perl版本低于5.8则执行如下操作
####需要安装 perl-5 /src/README.html
wget /src/5.0/perl-5.28.
tar -zxvf perl-5.28.
cd perl-5.28.0
./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
2.3 安装postgreSQL(所有节点都需要安装)
在编译的过程需要带上 with-perl;
./configure --prefix=/pgsql --with-perl
2.4安装Pl/PerlU(所有节点都安装)
之前安装Pg时已经加入了--with-perl则不需要再在此安装)
# yum install postgresql-plperl
3、安装依赖包(主节点安装)
3.1 安装DBI module
安装DBI 依赖 Test-Simple;
# 安装 Test-Simple
Test-Simple下载地址 搜索 Test-Simple
# 解压Test-Simple源码包
tar -zxvf Test-Simple-1.
# 进入Test-Simple目录进行安装
cd Test-Simple-1.302183
perl Makefile.PL
make
make install
# 接下来安装DBI
# 下载地址:/CPAN/authors/id/T/TI/TIMB/DBI-1.
tar -zxvf DBI-1.
cd DBI-1.630
perl Makefile.PL
make
make install
3.2 安装DBD::Pg module
安装最新的DBD::Pg;需要依赖 Time::HiRes
# 安装 Time::HiRes
下载地址: 搜索 Time::HiRes
tar -zxvf Time-HiRes-1. 
cd Time-HiRes-1.9764
perl Makefile.PL
make
make install
# 接下来安装DBD::Pg
# 安装DBD::Pg;需要确定已经安装 PostgreSQL 环境;即pg_config输出
# 加载 PostgreSQL 环境变量
tar -zxvf DBD-Pg-3.14.
cd DBD-Pg-3.14.2
perl Makefile.PL
make
make install
3.3 安装DBIx::Safe
# 下载地址:/DBIx-Safe/
tar -zxvf DBIx-Safe-1.2.
cd DBIx-Safe-1.2.5
perl Makefile.PL
make
make install
4、安装Bucardo
# 在网站/Bucardo/下载
tar -zxvf Bucardo-5.6.
cd Bucardo-5.6.0
perl Makefile.PL
make
make install
# 查看Bucardo版本
[postgres@node3 ~]$ bucardo --version
bucardo version 5.6.0
5、创建bucardo元数据库
5.1创建.bucardorc文件
在系统用户home目录下创建;内容如下:
log_conflict_file         = /home/postgres/bucardo/log/bucardo_conflict.log
piddir                    = /home/postgres/bucardo/run
reason_file               = /home/postgres/bucardo/ason.log
warning_file              = /home/postgres/bucardo/log/bucardo.warning.log
syslog_facility           = LOG_LOCAL1
5.2创建所需的目录及数据库
mkdir -p /home/postgres/bucardo/log
mkdir -p /home/postgres/bucardo/run
mkdir -p /var/run/bucardo
chmod 777 /var/run/bucardo
$ psql
postgres=# create user bucardo with superuser password 'bucardo';
postgres=# create database bucardo with owner = bucardo;
5.3初始化bucardo管理库
准备工作完成;开始执行 bucardo install 命令创建 元数据库;
# /usr/local/share/bucardo/bucardo.schema
[postgres@node3 ~]$ bucardo install
This will install the bucardo database into an existing Postgres cluster.
Postgres must have been compiled with Perl support,
and you must connect as a superuser
Current connection settings:
1. Host:           <none>
2. Port:           5432
3. User:           bucardo
4. Database:       bucardo
5. PID directory:  /home/postgres/bucardo/run
Enter a number to change it, P to proceed, or Q to quit: 1
-- 推荐使用unix套接字方式连接数据;可以使用dbuser:postgres;dbname:postgres去连接。
Change the host to: /tmp
Changed host to: /tmp
Current connection settings:
1. Host:           /tmp
2. Port:           6000
3. User:           postgres
4. Database:       postgres
5. PID directory:  /home/postgres/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
You may want to check over the configuration variables next, by running:
bucardo show all