標籤:
來自:http://bbs.chinaunix.net/thread-955564-1-1.html
參考文檔
http://bbs.chinaunix.net/viewthr ... page%3D1#pid6997819
http://topic.csdn.net/t/20050617/12/4089010.html
postgresql 資料同步
slony1是基於postgresql的非同步通知機製做的複製技術, 其同步速度非常快。 採用這個複製技術來做備份,呵呵,除了配置稍微複雜點,非常好用!
1. 總體需求
1.1. 當前情況
隨著軟體系統複雜度的提高,分布式部署成為軟體部署流行的方式。對於系統的結構,
程式和資料是支撐系統的兩大要素。程式的分布式部署已經有很多很好的方案,在這裡
我說一下資料的分布式部署。資料的分布式部署其實就是資料庫的分布式部署。
1.2. 系統內容
在這裡,我以下面的環境詳細講部署的過程。
主要資料庫伺服器 (master)
RHEL3
ip 192.168.0.200
mask 255.255.255.0
從資料庫伺服器 (slave)
RHEL3
ip 192.168.30.201
mask 255.255.255.0
需要保證兩台機器互連互連.
請到指定目的地下載指定的軟體包:
db postgresql 8.2.4.tar.gz(http://www.postgresql.org/download/)
slony1 slony1-1.2.6.tar(http://www.postgresql.org/download/)
以上的網址是一個入口地址,請選擇合適的正確的來源程式包。
1.3. 系統安裝
1.3.1 主要資料庫伺服器
1.3.1.1 安裝資料庫
Groupadd postgres
Useradd postgres –g postgres –d /home/postgres
解壓,命令 tar -xvzf postgresql 8.2.4.tar.gz
進入對應的 postgresql-8.2.4 目錄 ,命令 :cd postgresql-8.2.4
./configure --prefix=/usr/local/pgsql –localstatedir=/home/postgres/data
gmake,命令: gmake
注意,是 gmake
安裝,命令 gmake install
為 postgres 使用者增加環境參數如下:
Vi /home/postgres/.bash_profile
PGLIB=/usr/local/pgsql/lib
PGDATA=/test/spescso/data
PATH=$PATH:/usr/local/pgsql/bin
MANPATH=$MANPATH:/usr/local/pgsql/man
export PGLIB PGDATA PATH MANPATH
chown postgres.postgres /usr/local/pgsql –R
Su – postgres
Mkdir data
使用 postgres 建立 資料庫叢集
/usr/local/pgsql/bin/initdb -E UTF-8 /home/postgres/data/
/usr/local/pgsql/bin/createuser -a -d ssuser
/usr/local/pgsql/bin/createlang plpgsql template1
使用 postgres 建立 log 目錄
mkdir /test/spescso/data/log
修改 /test/spescso/data/postgresql.conf 檔案
主要配置記錄檔
log_destination = ‘stderr‘
redirect_stderr = true
log_directory = ‘/test/spescso/data/log/‘
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log‘
修改 /test/spescso/data/pg_hba.conf,主要搞定雙機相互認證問題,否則不能相互訪問
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.0.0/24 trust
# IPv6 local connections:
host all all ::1/128 trust
如果不能理解上面的意思,請仔細閱讀 postgresql 資料庫安全認證文檔。
使用 postgres 使用者後台啟動 postmaser 資料庫主進程
/usr/local/pgsql/bin/postmaster -i -D /test/spescso/data/ -p 5432 &
1.3.1.2 安裝 slony1 資料同步工具(主從都需要安裝)
解壓,命令 tar -xvjf slony1-1.2.6.tar
注意,使用 -j 參數,該網站壓縮包有點不一致。
進入對應的 slony1-1.2.6 目錄 ,命令 :cd slony1-1.2.6
./configure --with-pgsourcetree=<postgresql 原始碼目錄>
gmake,命令: gmake
注意,是 gmake
安裝,命令 gmake install
1.3.2 從資料庫伺服器
和主要資料庫伺服器的安裝方式一致。
1.3.3 建立資料庫和資料表
以下以在 主要資料庫伺服器上建立主要資料庫和資料表 test 為例見解,其他資料庫和資料表請參考建立.
su -c "/usr/local/pgsql/bin/createdb -U ssuser -E UTF-8 test -p 5432" -l postgres
su -c "/usr/local/pgsql/bin/psql -f /home/hzh/share/sql.txt -p 5432 -dtest -Ussuser" -l postgres
(注意,sql.txt 是建立資料表的命令,請自行編寫。sql.txt 檔案最好是 UTF-8 格式,特別是存在中文字元時)
例:sql.txt
CREATE TABLE tb_depart(
Id int primary key,
Name char();
在主要資料庫機器上依次建立 testslave1,testslave2.
在從資料庫機器上 5431 連接埠建立 testslave3
保證testslave1/testslave2/testsalve3中建立和test中相同的表tb_depart/tb_manager/tb_user……..
1.4 配置同步
1.4.1. 主機配置
編寫 configmaster sheul 指令檔,設定其可執行屬性 ,命令 chmod a+x+w+r configmaster,檔案內容如下:
#!/bin/bash
basebin=/usr/local/pgsql/bin
#叢集名稱
CLUSTER=slony_test1
# 參與同步的資料庫名稱,master db 是test,其他三個是slave
DBSERVER=test
DBSLAVE1=testslave1
DBSLAVE2=testslave2
DBSLAVE3=testslave3
# 參與同步的機器地址
HOSTSERVER=192.168.0.200
HOSTSLAVE1=192.168.0.200
HOSTSLAVE2=192.168.0.200
HOSTSLAVE3=192.168.0.201
# 參與同步的資料庫使用者名稱
DBSERVER_USER=ssuser
DBSLAVE1_USER=ssuser
DBSLAVE2_USER=ssuser
DBSLAVE3_USER=ssuser
# 同步發布配置,以下是命令 slonik 的參數
$basebin/slonik<<_EOF_
cluster name=$CLUSTER;
# 定義複製節點
node 1 admin conninfo=‘dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432‘;
node 2 admin conninfo=‘dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER port=5432‘;
node 3 admin conninfo=‘dbname=$DBSLAVE2 host=$HOSTSLAVE2 user=$DBSLAVE2_USER port=5432‘;
node 4 admin conninfo=‘dbname=$DBSLAVE3 host=$HOSTSLAVE3 user=$DBSLAVE3_USER port=5431‘;
# 初始化叢集,id從1開始
init cluster ( id=1, comment=‘Node 1‘ );
# 設定參與同步的資料表
#先建立一個複製集,id也是從1開始
#向自己的複製集種添加表,每個需要複製的表一條set命令
#id從1開始,逐次遞加,步進為1;
#fully qualified name是表的全稱:模式名.表名
#這裡的複製集id需要和前面建立的複製集id一致
create set ( id=1, origin=1, comment=‘All test tables‘ );
set add table ( set id=1, origin=1,id=1, fully qualified name=‘public.tb_depart‘,comment=‘Table tb_depart‘ );
set add table ( set id=1, origin=1,id=2, fully qualified name=‘public.tb_user‘,comment=‘Table tb_user‘ );
set add table ( set id=1, origin=1,id=3, fully qualified name=‘public.tb_manager‘,comment=‘Table tb_manager‘ );
#假如某個表沒有主鍵,但是有唯一鍵字,那麼可以用key關鍵字
#指定其為複製鍵字,如下面的key參數
#set add table ( set id = 1, origin = 1,id = 4, fully qualified name = ‘public.history‘,key = "column",comment = ‘Table history‘ );
#對於沒有唯一列的表,需要這樣處理,這一句放在 create set 的前面
#table add key (node id = 1, fully qualified name = ‘public.history‘);
# 這樣設定結果集
#set add table (set id=1, origin=1, id=4, fully qualified name = ‘public.history‘, comment=‘history table‘, key = serial);
# 設定儲存節點
store node ( id=2, comment=‘Node 2‘ );
store node ( id=3, comment=‘Node 3‘ );
store node ( id=4, comment=‘Node 4‘ );
# 設定儲存路徑
store path ( server=1, client=2,conninfo=‘dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432‘);
store path ( server=2, client=1,conninfo=‘dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER port=5432‘);
store path ( server=1, client=3,conninfo=‘dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432‘);
store path ( server=3, client=1,conninfo=‘dbname=$DBSLAVE2 host=$HOSTSLAVE2 user=$DBSLAVE2_USER port=5432‘);
store path ( server=1, client=4,conninfo=‘dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432‘);
store path ( server=4, client=1,conninfo=‘dbname=$DBSLAVE3 host=$HOSTSLAVE3 user=$DBSLAVE3_USER port=5431‘);
#設定偵聽事件和訂閱者向,複製中角色,主節點是原始提供者,從節點是接受者
store listen ( origin=1, provider=1, receiver=2 );
store listen ( origin=2, provider=2, receiver=1 );
store listen ( origin=1, provider=1, receiver=3 );
store listen ( origin=3, provider=3, receiver=1 );
store listen ( origin=1, provider=1, receiveR=4 );
store listen ( origin=4, provider=4, receiver=1 );
_EOF_
1.4.2. 提交資料集合
編寫 commitdata shell 指令檔,賦予可執行許可權,內容如下:
#!/bin/bash
basebin=/usr/local/pgsql/bin
CLUSTER=slony_test1
DBSERVER=test
DBSLAVE1=testslave1
DBSLAVE2=testslave2
DBSLAVE3=testslave3
HOSTSERVER=192.168.0.200
HOSTSLAVE1=192.168.0.200
HOSTSLAVE2=192.168.0.200
HOSTSLAVE3=192.168.0.201
DBSERVER_USER=ssuser
DBSLAVE1_USER=ssuser
DBSLAVE2_USER=ssuser
DBSLAVE3_USER=ssuser
$basebin/slonik<<_EOF_
cluster name=$CLUSTER;
#提供串連參數
node 1 admin conninfo=‘dbname=$DBSERVER host=$HOSTSERVER user=$DBSERVER_USER port=5432‘;
node 2 admin conninfo=‘dbname=$DBSLAVE1 host=$HOSTSLAVE1 user=$DBSLAVE1_USER port=5432‘;
node 3 admin conninfo=‘dbname=$DBSLAVE2 host=$HOSTSLAVE2 user=$DBSLAVE2_USER port=5432‘;
node 4 admin conninfo=‘dbname=$DBSLAVE3 host=$HOSTSLAVE3 user=$DBSLAVE3_USER port=5431‘;
# 提交訂閱複製集
subscribe set ( id=1, provider=1, receiver=2, forward=no);
subscribe set ( id=1, provider=1, receiver=3, forward=no);
subscribe set ( id=1, provider=1, receiver=4, forward=no);
_EOF_
1.4.3. 配置同步過程
在主機上執行配置命令 ./configmaster
在主機上開啟slon 後台進程,啟動主要資料庫複製,命令
/usr/local/pgsql/bin/slon slony_test1 "dbname=test host=192.168.0.200 user=ssuser port=5432" &
在主機上開啟slon 後台進程,啟動第一個從資料庫複寫,命令
/usr/local/pgsql/bin/slon slony_test1 "dbname=testslave1 host=192.168.0.200 user=ssuser port=5432" &
在主機上開啟slon 後台進程,啟動第二個從資料庫複寫,命令
/usr/local/pgsql/bin/slon slony_test1 "dbname=testslave2 host=192.168.0.200 user=ssuser port=5432" &
在從機上開啟slon 後台進程,啟動第三個從資料庫複寫,命令
/usr/local/pgsql/bin/slon slony_test1 "dbname=testslave3 host=192.168.0.201 user=ssuser port=5431" &
在主機上執行提交命令 ./commitdata
POSTGRESQL同步——SLONY-I配置