Chinaunix首页 | 论坛 | 博客
  • 博客访问: 161984
  • 博文数量: 43
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-19 22:14
个人简介

三十功名尘与土、八千里路云和月

文章分类

全部博文(43)

文章存档

2017年(3)

2016年(2)

2015年(38)

分类: 系统运维

2015-09-18 10:38:15

bucardo是PostgreSQL中双master解决方案,bucardo可以做两个数据库之间的双向异步同步,
从有双边都同时更新一条记录时,会自动解决冲突,冲突解决策略有:
source : 为源为准

target :为目录为准

skip   : 跳过

random :随机

latest :最后更新的那条记录为准

abort  :出现冲突,程序退出。
一般我们都采用latest。

===============安装bucardo===========================

bucardo是一个perl脚本,需要一些perl的包,先安装这些包:

Test-Simple-0.98.tar.gz

ExtUtils-MakeMaker-6.56.tar.gz

DBI-1.616.tar.gz

version-0.91.tar.gz

DBD-Pg-2.18.1.tar.gz

DBIx-Safe-1.2.5.tar.gz



perl包的安装方法都是到找到这个包,下载下来,一般习惯解压到/usr/src目录:

然后:

perl Makefile.PL

make

make install



安装perl的包

先安装DBI-1.616

/>
安装DBI前需要安装:

... -Simple-0.98.tar.gz

... keMaker-6.56.tar.gz

否则会报:

Warning: prerequisite ExtUtils::MakeMaker 6.48 not found. We have 6.30.

Warning: prerequisite Test::Simple 0.90 not found. We have 0.62.



安装DBD-pg前需要先安装version-0.91

... version-0.91.tar.gz

再安装DBD-pg:

... BD-Pg-2.18.1.tar.gz



安装

/>


安装Bucardo5.0.0

http://blog.endpoint.com/2011/06 ... for-postgresql.html



===================bucardo4.4.4的配置====================================

在用户的HOME目录下建立一个.bucardorc文件,在这个文件中设定一些参数:

default_email_from        = ctr-db3@alibaba-inc.com

default_email_host        = vip-smtp.vip.xyi.cn.alidc.net

default_email_to          = cheng.tangc@alibaba-inc.com

log_conflict_file         = /home/postgres/log/bucardo_conflict.log

piddir                    = /home/postgres/run

reason_file               = /home/postgres/log/bucardo.restart.reason.log

warning_file              = /home/postgres/log/bucardo.warning.log

syslog_facility           = LOG_LOCAL1



bucardo会在所需要同步的数据库中建一个名叫bucardo的数据库,在其中存一些配置信息,这个过

程是由bucardo_ctl install命令完成的:

bucardo_ctl install



完成后,我们把bucardo数据库的搜索路径上加上bucardo这个schema名,以便连接到这个数据库

后就可以使用\d看到这些配置表:

alter database bucardo set search_path="$user",public,bucardo;



下面的配置的环境:

主机1: 192.168.1.31

主机2: 192.168.1.32

我们在这两台机器上的postgresql做双向同步:

这两台机器上的postgresql数据库已经装好。



操作步骤:

1. 在两台机器上:

bucardo_ctl install



2. 在两台机器上,先建我们要同步的数据库:

createdb mydb

createlang plpgsql

注意需要plpgsql装到数据库中。

使用pgbench 建一些测试表。

pgbench -i mydb -s 300

我们同步就在pgbench_accounts pgbench_branches pgbench_tellers上进行:



3. bucardo中增加要同步的数据库,主要是配置bucardo如何连接这些数据库:

在第一台机器上:

bucardo_ctl add db db1 dbname=mydb sourcelimit=100 targetlimit=100

bucardo_ctl add db db2 dbname=mydb host=192.168.1.32 sourcelimit=100 targetlimit=100

这个命令中可以带的参数,基本上都是数据库连接参数:

name: the internal name used by Bucardo to refer to this database

port: the port this database runs on. Defaults to 5432.

host: the host this database is on. Defaults to no host (Unix socket)

user: the user to connect as. Defaults to 'bucardo'

pass: the password to connect with. Don't use this, use a .pgpass file instead!

conn: Any additional information add to the connection string, e.g. sslmode=require

sourcelimit: The maximum number of replication events that can run at one time using this database as a source. Defaults to 0 (no limit)

targetlimit: The maximum number of replication events that can run at one time using this database as a target. Defaults to 0 (no limit)

pgpass: Full path and filename of a Bucardo/pgpass file to use for this connection





由于我们是双向同步,需要指定同步的默认的冲突处理方法:

alter table bucardo.goat alter standard_conflict set default 'latest';

冲突解决方法可为:

source

target

skip

random

latest

abort





4. 增加哪些表要同步:

在第一台机器上:

bucardo_ctl add table pgbench_accounts pgbench_branches pgbench_tellers db=db1 --herd=herd1 --verbose

这个步骤其实是在表上建触发器,做完这个步骤后所产生的变化都会被同步



5. 增加群,群是为了便于管理同步中表的一个集合。

在第一台机器上:

bucardo_ctl add herd herd1 pgbench_accounts pgbench_branches pgbench_tellers



6. 增加同步

在第一台机器上:

bucardo_ctl add sync sync1 source=herd1 targetdb=db2 type=swap ping=yes

加同步的时候,实际上是把源数据库上的群(群中包含了一些表)同步到目标数据库中。

type为swap,就是两个数据库做双向同步。

type 还可以有的值为:

pushdelta: master/slave模式,只有主库把数据同步到备库

fullcopy:  master/slave模式,全量同步



7. 如果初始时,源数据库上有数据,而目标数据库上没有数据,需要开始时做一次全量同步,这种

方式在bucardo中叫onetimecopy,要执行ontimecopy,需要源数据中的表至少有一条数据,而

目标数据库中的表不能有数据:



bucardo_ctl update sync sync onetimecopy=2

这可我们把同步的onetimecopy属性设置为2,这样当同步启动后,会自动做一次全量同步,然后再

慢慢增量同步。

全量同步完后,会自动把状态改为0。



8. 启动同步:

bucardo_ctl start



9. 查看同步状态:

bucardo_ctl status



bucardo_ctl status sync1



============================= 日常维护操作 ===============================



1. bucardo使用触发器把变化行的主键记到一张表中,所以这张表会变得越来越大,我们需要做一

个定时任务来清理这个表的内容:

0 2 * * * /usr/local/bin/psql -X -q -d mydb -U postgres -c "SELECT bucardo.bucardo_purge_delta('24 hours'::interval)"



2. 临时停止和启动同步的方法:

bucardo_ctl activate <syncname>

bucardo_ctl deactivate <syncname>



bucardo.sync中有:track_rates

设置为true。



3. onetimecopy:

bucardo_ctl update sync <syncname> onetimecopy=2

重启bucardo_ctl可以让其开始复制

bucardo_ctl reload sync1好象也可以



4. 新增表的步骤:

bucardo_ctl add all tables

bucardo_ctl update sync <syncname> add table <tab1> <tab2> ...

bucardo_ctl validate <syncname>

bucardo_ctl update sync onetimecopy=2

bucardo_ctl update sync onetimecopy=2 rebuild_index=1

bucardo_ctl reload <syncname>



5. 移除表:

bucardo_ctl update sync <syncname> remove table foobar

bucardo_ctl update sync <syncname> remove sequence foobar_seq

bucardo_ctl reload <syncname>
阅读(1979) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~