Chinaunix首页 | 论坛 | 博客
  • 博客访问: 838650
  • 博文数量: 150
  • 博客积分: 5123
  • 博客等级: 大校
  • 技术积分: 1478
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 10:03
文章分类

全部博文(150)

文章存档

2011年(2)

2010年(139)

2009年(9)

分类:

2010-04-11 21:58:50

我们配置一个最简单的同步场景:
环境说明
只有一台主机,在这台主机中建两个数据库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
 
 
 
 
 
 
 
阅读(4822) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~