加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL 解决双机方案:

发布时间:2020-12-13 17:42:42 所属栏目:百科 来源:网络整理
导读:Bucardo 是一个 PostgreSQL 的异步同步工具,它支持任意方式的主从同步, multi-master 或者 multi-slave 等。新版 Bucardo 可将数据同步到其他存储系统,如 MongoDB 。 bucardo是PostgreSQL数据库中实现双向同步的软件,可以实现PostgreSQL数据库的双master

Bucardo是一个PostgreSQL的异步同步工具,它支持任意方式的主从同步,multi-master或者multi-slave等。新版Bucardo可将数据同步到其他存储系统,如MongoDB

bucardo是PostgreSQL数据库中实现双向同步的软件,可以实现PostgreSQL数据库的双master的方案,不过bucardo中的同步都是异步的,它是通过触发器记录变化,程序是perl写的。

===============安装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包的安装方法都是到http://search.cpan.org找到这个包,下载下来,一般习惯解压到/usr/src目录:

然后:

perl Makefile.PL

make

make install

安装perl的包

先安装DBI-1.616

http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.616.tar.gz

安装DBI前需要安装:

http://search.cpan.org/CPAN/authors/id/M/MS/MSCHWERN/Test-Simple-0.98.tar.gz

http://search.cpan.org/CPAN/authors/id/M/MS/MSCHWERN/ExtUtils-MakeMaker-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

http://search.cpan.org/CPAN/authors/id/J/JP/JPEACOCK/version-0.91.tar.gz

再安装DBD-pg:

http://search.cpan.org/CPAN/authors/id/T/TU/TURNSTEP/DBD-Pg-2.18.1.tar.gz

安装

http://bucardo.org/downloads/DBIx-Safe-1.2.5.tar.gz

安装Bucardo4.4.4

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


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

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

default_email_from = myhost@myinc.com

default_email_host = mailhost.myinc.com

default_email_to = osdba@mailhost.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.195.35

主机2: 192.168.195.36

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

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

操作步骤:

1. 在两台机器上:

bucardo_ctl install

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

createdb aligputf8

createlang plpgsql

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

使用pgbench 建一些测试表。

pgbench -i aligputf8 -s 300

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

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

在第一台机器上:

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

bucardo_ctl add db db2 dbname=aligputf8 host=192.168.195.36 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 aligputf8 -U postgres -c "SELECT bucardo.bucardo_purge_delta('10 minutes'::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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读