Postgresql9でレプリケーション設定

Postgresql9.0 の新しいレプリケーション機能を試してみる。

環境
Postgresql9.0 のインストールするサーバ環境
master : Centos 5.4
stabdby : Ubunte 10.04

設定
 マスター1台 スタンバイ1台の環境で設定することにする
master
インストール場所  /user/local/pgsql90/
IP: 192.168.3.201
port: 5551
 /user/local/pgsql90/
 /home/postgres/data90
 /home/postgres/archive90

standby
インストール場所 /user/lo/var/lib/postgresql9以下
IP: 192.168.3.101
port: 5552
 /var/lib/postgresql9/
 /var/lib/postgresql9/data_slave
 /var/lib/postgresql9/archive_slave

Ubunteにインストールする


sudo mkdir /var/lib/postgresql9
sudo mkdir /var/lib/postgresql9/src
sudo chown -Rf postgres:postgres /var/lib/postgresql9

cd /var/lib/postgresql9/src
postgresql9.0 のソースを入手

wget http://***********/postgresql-9.0.0.tar.gz
tar -zxvf postgresql-9.0.0.tar.gz
cd ./postgresql-9.0.0

最初の./configure実行時に readline がないとのエラーが出たので
以下のコマンドでインストールした
apt-get install libreadline5
apt-get install libreadline5-dev

./configure --prefix = /var/lib/postgresql9
make
make install

Ubunteの 起動ファイル作成


cp ./contrib-start-sclipts/linux /etc/init.d/postgresql9_sb

 起動ファイルの以下を書き換える
  ・PREFIX = /var/lib/postresql9
 ・PGDATA = /var/lib/postresql9/data90


master(Centos側)を初期化する

cd /var/lib/postgresql9
./bin/initdb -D /home/postgres/data90 --no-locale --encoding=EUC_JP

アーカイブDIRを作成する

master: mkdir /home/postgres/archive90

マスタのpostgresql.confの編集
vi /home/postrges/data90/postgresql.conf


#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

listen_addresses = '*'
port = 5551

#------------------------------------------------------------------------------
# WRITE AHEAD LOG
#------------------------------------------------------------------------------

wal_level = hot_standby

archive_mode = on

archive_command = 'cp -i %p /home/postgresql/archive90/%f'

max_wal_senders = 1

マスターのpg_hba.confの編集
vi /home/postgres/data90/pg_hba.conf


host replication postgres 192.168.3.101/32 trust
host all all 192.168.3.0/24 trust
"replication" がStandbyサーバの為の設定

マスタを起動して接続してみる


# 起動
sudo /etc/init.d/postgresql90 start

# DB作成
/var/lib/postrgesql9/bin/createdb test

#接続
/var/lib/postrgesql9/bin/psql -p 5551 test

#適当にテーブルを作って データを入れる
create table test(id serial, name text );
insert into test(name) values('aaa');
insert into test(name) values('bbb');

マスタのバックアップモード開始


#接続
/var/lib/postrgesql9/bin/psql -p 5551 test
select pg_start_backup('init');

マスタのDataをStandbyにコピーする
マスタにsshで接続した状態で


scp -r /home/postgres/data90 102.168.3.101:/var/lib/postgresql9/
スレーブsshで接続した状態でコピー後のフォルダ名を変更する

mv /var/lib/postgresql9/data90 /var/lib/postgresql9/data_sb

マスタのバックアップモード停止


select pg_start_stop();

スタンバイのアーカイブDIRを作成する


stanbby: mkdir /var/lib/postgresql9/archive_sb

スタンバイのpostgresql.confの編集
元々マスタ側で設定したファイルがそのままあるので注意


vi /var/lib/postrgesql9/data_sb/postgresql.conf

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

listen_addresses = '*'
port = 5552

#------------------------------------------------------------------------------
# WRITE AHEAD LOG
#------------------------------------------------------------------------------

wal_level = hot_standby

archive_mode = on

archive_command = 'cp -i %p /var/lib/postgresql9/archive_sb/%f'

max_wal_senders = 1

スタンバイのpg_hba.confの編集
vi /home/postgres/data90/pg_hba.conf


host all all 192.168.3.0/24 trust

スタンバイのrecovery.confの作成と編集
recovery.conf.sample をコピーしてから編集する

cp /var/lib/postgresql9/share/recovery.conf.sample /var/lib/postgresql9/data90_sb/recovery.conf


# -------------------------------
# PostgreSQL recovery config file
# -------------------------------

restore_command = 'cp -i /var/lib/postgresql9/archive_sb/%f %p'

standby_mode = 'on'

primary_conninfo = 'host=192.168.3.201 port=5551 '

StandByの起動


sudo /etc/init.d/postgresql9_sb start
正常に起動されたかどうか、ログを確認してみる
more /var/lib/postgresql9//data_sb/serverlog

PgAdminⅢのインストール
Postgresql9 に対応したバージョン 1.12 をインストール
マスタとstandbyにそれぞれ接続して同じ校正になっていることを確認する
 OK だった

マスタの現在の確認


[postgres@dejimon ~]$ /usr/local/pgsql90/bin/psql -p 5551 test
psql (9.0.0)
Type "help" for help.

test=# select * from test;
id | data | dt

                                                                                    • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
(3 rows)

スタンバイのデータ確認


postgres@ubunts91:~$ /var/lib/postgresql9/bin/psql -p 5552 test
psql (9.0.0)
Type "help" for help.

test=# select * from test;
id | data | dt

                                                                                    • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
(3 rows)

OK同じになっている

マスタにデータを追加


test=#
test=# insert into test(data) values('add');
INSERT 0 1
test=# select * from test;
id | data | dt

                                                                                    • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
4 | add | 2010-09-24 06:38:34.456391+09
(4 rows)

スタンバイのデータ確認


test=# select * from test;
id | data | dt

                                                                                    • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
4 | add | 2010-09-24 06:38:34.456391+09
(4 rows)
データが追加され同じになっている

スタンバイにデータを挿入してみる


test=# insert into test(data) values('stand_by');
ERROR: cannot execute INSERT in a read-only transaction
エラーとなる

スタンバイを停止して、マスタDB更新後スタンバイを再起動して動作を確認

スタンバイを停止


postgres@ubunts91:~$ /etc/init.d/postgresql90_sb stop
Stopping PostgreSQL: パスワード:
ok
postgres@ubunts91:~$ /var/lib/postgresql9/bin/psql -p 5552 test
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5552"?

マスタ更新


test=# insert into test(data) values('master_add');
INSERT 0 1
test=# select * from test;
id | data | dt

                                                                                                • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
4 | add | 2010-09-24 06:38:34.456391+09
5 | master_add | 2010-09-24 06:52:46.930512+09
(5 rows)

スタンバイを停止を再起動して確認


postgres@ubunts91:~$ sudo /etc/init.d/postgresql90_sb start
Starting PostgreSQL: ok
postgres@ubunts91:~$ echo "select * from test" | /var/lib/postgresql9/bin/psql -p 5552 tes
t
id | data | dt

                                                                                                • -

1 | nino | 2010-09-23 19:06:42.906341+09
2 | aa | 2010-09-23 19:20:29.752359+09
3 | cc | 2010-09-23 19:20:38.496035+09
4 | add | 2010-09-24 06:38:34.456391+09
5 | master_add | 2010-09-24 06:52:46.930512+09
(5 rows)

きちんと更新されている