2008년 03월 28일
Slony-I 설치 (postgresql replication tool)
Slony-I은 마스터 멀티 슬레이브 기반의 리플리케이션 툴입니다. 데이타베이스 전체가 아니라 테이블 기반으로 replication을 합니다.
디비를 전체 replication 하거나 테이블이 많은 경우에는 다소 부적합한 툴입니다.장점은 활발하게 개발이 이루어지고 있고 차후의 postgresql 버전에서는 contrib나 core 부분에 추가 될수도 있다는 얘기가 돌고 있습니다. 큰 레코드로 테스트를 해보진 못했지만 거의 딜레이 없이 실시간으로 복제가 이루어집니다.(두서버가 같은 네트워크의 경우)
단점은 설정이 조금 복잡하고 replicaton에 테이블을 추가할 경우에는 테이블 추가한 뒤 기존의 설정과 병합해야 되는 번거로움이 있습니다. 그나마 pgadmin이라는 그래픽 툴을 활용하면 조금 더 쉽게 설정할 수 있습니다.
다른 posgresql replication tool은 링크를 참조하세요.
이제 본격적으로 설치 과정에 들어가겠습니다. http://slony.info/에 가서 최신 버전의 slony-I을 다운로드 받은 뒤 적당한 디렉터리에 압축을 풉어줍니다.
설치 참조 링크
http://www.powerdb.net/?inc=read&aid=5520&criteria=pgsql&subcrit=&id=&limit=20&keyword=&page=2
http://slony.info/documentation/
http://www.dbtool.co.kr/board/board_view.jsp?topmenu=2&leftmenu=4&no=441
1. 설치
#./configure --prefix=/usr/local/pgsql --with-pgconfigdir=/usr/local/pgsql/bin
#make;make install
2.환경 변수 설정 (마스터, 슬라이브 둘다 설정) -> postgres 계정에 .bash_profile에 설정
CLUSTERNAME=slony_example
MASTERDBNAME=slonytest
SLAVEDBNAME=slonytest
MASTERHOST=master
SLAVEHOST=slave
REPLICATIONUSER=postgres
export CLUSTERNAME MASTERDBNAME SLAVEDBNAME MASTERHOST SLAVEHOST REPLICATIONUSER
3. 마스터와 슬레이브에 DB 생성
postgres@localhost$createdb -O $REPLICATIONUSER -h $MASTERHOST $MASTERDBNAME
postgres@localhost$createdb -O $REPLICATIONUSER -h $SLAVEHOST $SLAVEDBNAME
4. pgbench를 이용해 마스터의 테스트 DB에 내용 채우기
postgres@localhost$pgbench -i -s 1 -U $REPLICATIONUSER -h $MASTERHOST $MASTERDBNAME
postgres@localhost$createlang plpgsql -h $MASTERHOST $MASTERDBNAME
혹시 비밀번호 인증을 사용할 경우 끝에 -W 옵션을 붙여주세요.
plpgsql이 추가되어 있다면 두번째 줄은 하지 않으셔도 됩니다.
pgbench가 없으면 contrib/pgbench 문서를 참조하세요.
5. 슬레이브 테스트 DB에 테이블 생성
postgres@localhost$pg_dump -s -U $REPLICATIONUSER -h $MASTERHOST $MASTERDBNAME | psql -U $REPLICATIONUSER -h $SLAVEHOST $SLAVEDBNAME
-s 옵션을 사용해서 데이터는 전송하지 않고 스키마만 덤프해서 슬레이브로 보냅니다.
혹시 비밀번호 인증을 사용할 경우 끝에 -W 옵션을 붙여주세요.
6.슬로니 관련 스키마 설정
적절한 디렉터리에 아래내용으로 slony_master.sh를 만들고 실행 권한을 줍니다.
#!/bin/sh
slonik <<_EOF_
#--
# define the namespace the replication system uses in our example it is
# slony_example
#--
cluster name = $CLUSTERNAME;
#--
# admin conninfo's are used by slonik to connect to the nodes one for each
# node on each side of the cluster, the syntax is that of PQconnectdb in
# the C-API
# --
node 1 admin conninfo = 'dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER';
node 2 admin conninfo = 'dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER';
#--
# init the first node. Its id MUST be 1. This creates the schema
# _$CLUSTERNAME containing all replication system specific database
# objects.
#--
init cluster ( id=1, comment = 'Master Node');
#--
# Because the history table does not have a primary key or other unique
# constraint that could be used to identify a row, we need to add one.
# The following command adds a bigint column named
# _Slony-I_$CLUSTERNAME_rowID to the table. It will have a default value
# of nextval('_$CLUSTERNAME.s1_rowid_seq'), and have UNIQUE and NOT NULL
# constraints applied. All existing rows will be initialized with a
# number
#--
table add key (node id = 1, fully qualified name = 'public.history');
#--
# Slony-I organizes tables into sets. The smallest unit a node can
# subscribe is a set. The following commands create one set containing
# all 4 pgbench tables. The master or origin of the set is node 1.
#--
create set (id=1, origin=1, comment='All pgbench tables');
set add table (set id=1, origin=1, id=1, fully qualified name = 'public.accounts', comment='accounts table');
set add table (set id=1, origin=1, id=2, fully qualified name = 'public.branches', comment='branches table');
set add table (set id=1, origin=1, id=3, fully qualified name = 'public.tellers', comment='tellers table');
set add table (set id=1, origin=1, id=4, fully qualified name = 'public.history', comment='history table', key = serial);
#--
# Create the second node (the slave) tell the 2 nodes how to connect to
# each other and how they should listen for events.
#--
store node (id=2, comment = 'Slave node');
store path (server = 1, client = 2, conninfo='dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER');
store path (server = 2, client = 1, conninfo='dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER');
_EOF_
./slony_master.sh -> 스크립트 실행
7. slon 데몬 구동
#마스터에서 구동
slon $CLUSTERNAME "dbname=$MASTERDBNAME user=$REPLICATIONUSER host=$MASTERHOST"
#슬레이브에서 구동
slon $CLUSTERNAME "dbname=$SLAVEDBNAME user=$REPLICATIONUSER host=$SLAVEHOST"
#slony_slave.sh 실행
#!/bin/sh
slonik <<_EOF_
# ----
# This defines which namespace the replication system uses
# ----
cluster name = $CLUSTERNAME;
# ----
# Admin conninfo's are used by the slonik program to connect
# to the node databases. So these are the PQconnectdb arguments
# that connect from the administrators workstation (where
# slonik is executed).
# ----
node 1 admin conninfo = 'dbname=$MASTERDBNAME host=$MASTERHOST user=$REPLICATIONUSER';
node 2 admin conninfo = 'dbname=$SLAVEDBNAME host=$SLAVEHOST user=$REPLICATIONUSER';
# ----
# Node 2 subscribes set 1
# ----
subscribe set ( id = 1, provider = 1, receiver = 2, forward = no);
_EOF_
8. replication test
#!/bin/sh
echo -n "**** comparing sample1 ... "
psql -U $REPLICATIONUSER -h $MASTERHOST $MASTERDBNAME >dump.tmp.1.$$ <<_EOF_
select 'accounts:'::text, aid, bid, abalance, filler
from accounts order by aid;
select 'branches:'::text, bid, bbalance, filler
from branches order by bid;
select 'tellers:'::text, tid, bid, tbalance, filler
from tellers order by tid;
select 'history:'::text, tid, bid, aid, delta, mtime, filler,
"_Slony-I_${CLUSTERNAME}_rowID"
from history order by "_Slony-I_${CLUSTERNAME}_rowID";
_EOF_
psql -U $REPLICATIONUSER -h $SLAVEHOST $SLAVEDBNAME >dump.tmp.2.$$ <<_EOF_
select 'accounts:'::text, aid, bid, abalance, filler
from accounts order by aid;
select 'branches:'::text, bid, bbalance, filler
from branches order by bid;
select 'tellers:'::text, tid, bid, tbalance, filler
from tellers order by tid;
select 'history:'::text, tid, bid, aid, delta, mtime, filler,
"_Slony-I_${CLUSTERNAME}_rowID"
from history order by "_Slony-I_${CLUSTERNAME}_rowID";
_EOF_
if diff dump.tmp.1.$$ dump.tmp.2.$$ >$CLUSTERNAME.diff ; then
echo "success - databases are equal."
rm dump.tmp.?.$$
rm $CLUSTERNAME.diff
else
echo "FAILED - see $CLUSTERNAME.diff for database differences"
fi
마지막으로 실제 데이타 업데이트 되는지 테스트 해봅니다.
이 글과 관련있는 글을 자동검색한 결과입니다 [?]
- Postgresql 설치 방법 by 부자아빠
- replication 개요 by 불근앙마
- Service (PostgreSQL) failed to start by Sigel
# by | 2008/03/28 09:35 | 리눅스강좌 | 트랙백 | 덧글(0)




☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]