Slony-I同步复制配置简单步骤

我们配置一个最简单的同步场景:
环境说明
只有一台主机,在这台主机中建两个数据库pgbench和pgbenchslave,
同步就在这两个数据库之间进行,而pgbench数据库是直接使用pgbench工具生成的测试数据库。
我的数据库版本为PostgreSQL8.4.3,PostgreSQL的用户为Postgres。
我安装的PostgreSQL的主目录为/usr/local/pgsql
 
编译安装slony-I
下载安装包slony1-2.0.3.tar.bz2到/usr/src目录:
tar jxvf slony1-2.0.3.tar.bz2
cd slony1-2.0.3.tar.bz2
export PGMAIN=/usr/local/pgsql
./configure --with-pgconfigdir=$PGMAIN/bin --with-perltools
gmake all
gmake install
 
 
配置过程
 
为了方便我们增加一些环境变量在postgres用户下的.bash_profile文件中:
CLUSTERNAME=slony_example
MASTERDBNAME=pgbench
SLAVEDBNAME=pgbenchslave
MASTERHOST=localhost
SLAVEHOST=localhost
REPLICATIONUSER=postgres
PGBENCHUSER=pgbench
export CLUSTERNAME=tang_rep
 
一般来说,我们使用slony提供的一些脚本工具进行配置,这样可以大大减少我们配置的时间。
使用脚本工具需要配置脚本工具的配置文件/usr/local/etc/slon_tools.conf:
cp slon_tools.conf-sample /usr/local/etc/slon_tools.conf
 
然后编译/usr/local/etc/slon_tools.conf文件,首先是设置CUSTER_NAME,把文件中的$CLUSTER_NAME名字改成我们需要的名字,我这里使用了“ tangrep”:
$CLUSTER_NAME = 'tangrep';
 
然后就是节点配置,把文件里面不需要的add_node的部分删除,加上我们的配置:
    add_node(node     => 1,
             host     => 'localhost',
             dbname   => 'pgbench',
             port     => 5432,
             user     => 'postgres',
             password => '');
    add_node(node     => 2,
             host     => 'localhost',
             dbname   => 'pgbenchslave',
             port     => 5432,
             user     => 'postgres',
             password => '');
 
最后就是数据集的配置了,也就是配置我们需要同步的表或sequence:
        "pkeyedtables" => [
                           'pgbench_accounts',
                           'pgbench_branches',
                           'pgbench_history',
                           'pgbench_tellers',
                           ],
 
原本文件中还有对sequnece、无主键表同步的配置示例,把这些都注释掉。
 
生成pgbench测试数据库及相关的表:
createuser -A -D $PGBENCHUSER
createdb -O $PGBENCHUSER -h $MASTERHOST $MASTERDBNAME
createdb -O $PGBENCHUSER -h $SLAVEHOST $SLAVEDBNAME
pgbench -i -s 1 -U $PGBENCHUSER -h $MASTERHOST $MASTERDBNAME
因为pgbench建立的表pgbench_history 没有主键,我们给它建上主键:
ALTER TABLE pgbench_history ADD primary key(tid,aid,mtime);
 
slony-I会用到plpgsql,所以要建plpgsql语言:
createlang -h $MASTERHOST plpgsql $MASTERDBNAME
 
把在pgbench数据库建好的表同步到pgbenchslave数据库中:
pg_dump -s -U $REPLICATIONUSER -h $MASTERHOST $MASTERDBNAME | psql -U $REPLICATIONUSER -h $SLAVEHOST $SLAVEDBNAME
 
运行pgbench工具,生成一些测试数据库在pgbench数据库中:
pgbench -s 1 -c 5 -t 1000 -U $PGBENCHUSER -h $MASTERHOST $MASTERDBNAME
 
进到pgbench数据库中,看pgbench_accounts表中有100000条数据:
pgbench=# select count(*) from pgbench_accounts; 
 count 
-------
 100000
(1 row)
pgbench=#
 
进到pgbenchslave数据库中,看pgbench_accounts表中有0条数据:
pgbenchslave=# select count(*) from pgbench_accounts; 
 count 
-------
 0
(1 row)
pgbenchslave=#
 
 
配置slony集群:
$ slonik_init_cluster  | slonik 
启动slon守护程序:
$ slon_start 1    
$ slon_start 2
1和2就是节点号。
创建数据集
$ slonik_create_set 1 | slonik             
添加消费者
$ slonik_subscribe_set 1 2 | slonik
上面命令中的“1”是数据集编号,2是节点编号。
添加完消费者后,pgbench数据库中的数据就同步到pgbenchslave数据库中去了,这时去pgbenchslave中去检查pgbench_accounts表中的数据数:
pgbenchslave=# select count(*) from pgbench_accounts; 
 count 
-------
 100000
(1 row)
pgbenchslave=#
 
这里把pbbench数据库中pgbench_accounts 删除一条记录,然后到pgbenchslave数据库中的pgbench_accounts中去看,也可以看到这条记录也会没有了:
pgbench=# delete from pgbench_accounts where aid=2;         
DELETE 1
pgbench=# select * from pgbench_accounts where aid=2;
 aid | bid | abalance | filler 
-----+-----+----------+--------
(0 rows)
pgbench=#/c pgbenchslave
pgbenchslave=#select * from pgbench_accounts where aid=2;
 aid | bid | abalance | filler 
-----+-----+----------+--------
(0 rows)
 
这里就可以看到两个数据库之间已以同步了。
 
 
slony的switchover操作:
也就是把主节点改成从节点,从节点升级为主节点:
slonik_move_set set1 node1 node2 | slonik
 
slony的failver操作:
slonik_failover node1 node2 | slonik
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值