Slony-I可以来实现PostgreSQL数据库的主从复制。
下面是Slony-I 的安装配置简明指南,实现主副数据库的同步。后面我会再介绍Pgbouncer的安装和配置
1. 主副数据库机器
Master: hostname: M_DB inet addr:10.0.0.11 OS: Linux 2.6.9-42.ELsmp CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz MemTotal: 254772 kB PgSQL: postgresql-8.3.0
Slave: hostname:S_DB inet addr:10.0.0.12 OS: Linux 2.6.9-42.ELsmp CPU:Intel(R) Xeon(R) CPU L5320 @ 1.86GHz MemTotal: 514440 kB PgSQL: postgresql-8.3.0
sudo -u postgres /home/y/pgsql/bin/createlang plpgsql URT
2. 安装Slony-I
tar xfj slony1-1.2.13.tar.bz2 cd slony1-1.2.13 ./configure –with-pgconfigdir=/home/y/pgsql/bin gmake all sudo gmake install
3. Slony Config
创建urt_replica_init.sh文件:
SLONIK=/home/y/pgsql/bin/slonik
CLUSTER=URT
SET_ID=1
MASTER=1
HOST1=M_DB
DBNAME1=URT
SLONY_USER=postgres
SLAVE=2
HOST2=S_DB
DBNAME2=URT
PGBENCH_USER=postgres
$SLONIK <<EOF
cluster name = $CLUSTER;
node $MASTER admin conninfo = ‘dbname=$DBNAME1 host=$HOST1 user=$SLONY_USER ‘; node $SLAVE admin conninfo = ‘dbname=$DBNAME2 host=$HOST2 user=$PGBENCH_USER ‘;
init cluster ( id = $MASTER, comment = ‘Primary Node’ );
store node ( id = $SLAVE, comment = ‘Slave Node’ );
store path ( server = $MASTER, client = $SLAVE, conninfo = ‘dbname=$DBNAME1 host=$HOST1 user=$SLONY_USER ‘);
store path ( server = $SLAVE, client = $MASTER, conninfo = ‘dbname=$DBNAME2 host=$HOST2 user=$PGBENCH_USER ‘);
store listen ( origin = $MASTER, provider = 1, receiver = 2 ); store listen ( origin = $SLAVE, provider = 2, receiver = 1 );
create set ( id = $SET_ID, origin = $MASTER, comment = ‘All pgbench tables’ );
set add table ( set id = $SET_ID, origin = $MASTER, id = 1, fully qualified name = ‘public.accounts’, comment = ‘Table accounts’ );
EOF
./urt_replica_init.sh
4. Slony Start
创建Master.slon文件:
cluster_name=”URT” conn_info=”dbname=URT host=M_DB user=postgres”
创建Slave.slon文件:
cluster_name=”URT” conn_info=”dbname=URT host=S_DB user=postgres”
/home/y/pgsql/bin/slon -f master.slon >> master.log &
/home/y/pgsql/bin/slon -f slave.slon >> slave.log &
5. Slony Subscribe
创建urt_replica_subscribe.sh文件:
node $MASTER admin conninfo = ‘dbname=$DBNAME1 host=$HOST1 user=$SLONY_USER’; node $SLAVE admin conninfo = ‘dbname=$DBNAME2 host=$HOST2 user=$PGBENCH_USER ‘;
subscribe set ( id = $SET_ID, provider = $MASTER, receiver = $SLAVE, forward = no); EOF
./urt_replica_subscribe.sh
6. 测试 修改M_DB上URT数据里的accounts表,S_DB上的accounts表也会随之改变