CentOS5.5上でPostgreSQL9.0のレプリケーションを試す
・プライマリサーバ 192.168.14.101/24
・ホットスタンバイサーバ 192.168.14.102/24
まず、プライマリーサーバで作業を行ないます。
# su - postgres $ createdb -E UTF8 -O 'postgres' sample $ exit →テスト用のsampleデータベースをpostgresのものとして作成 # mkdir /var/lib/pgsql/9.0/archive # chown -R postgres:postgres /var/lib/pgsql/9.0/archive →アーカイブログの置き場所の作成 # vi /var/lib/pgsql/9.0/data/pg_hba.conf # vi /var/lib/pgsql/9.0/data/postgres.conf →内容については下記に記載したものを参照 # /etc/init.d/postgresql-9.0 restart postgresql-9.0 サービスを停止中: [ OK ] postgresql-9.0 サービスを開始中: [ OK ] →設定の反映 # su - postgres $ psql psql (9.0.0) "help" でヘルプを表示します. postgres=# select pg_start_backup('init'); pg_start_backup ----------------- 0/1000020 (1 行) postgres=# \q $ exit →バックアップモードをスタート # scp /var/lib/pgsql/9.0/data 192.168.14.102:/var/lib/pgsql/9.0/ →ホットスタンバイ側にファイルを転送。バックアップモード中なのでファイルコピーOK。 # su - postgres $ psql psql (9.0.0) "help" でヘルプを表示します. postgres=# select pg_stop_backup(); NOTICE: pg_stop_backup complete, all required WAL segments have been archived pg_stop_backup ---------------- 0/10000A0 (1 行) postgres=# exit postgres-# \q $ exit →ファイルコピー後にバックアップモードを抜ける。
プライマリーサーバの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 # IPv6 local connections: #host all all ::1/128 trust host all all 0.0.0.0/0 md5 host replication postgres 192.168.14.102/32 md5
プライマリーサーバのpostgres.conf connection
# - Connection Settings - #listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost', '*' = all listen_addresses = '*'
プライマリーサーバのpostgres.conf settings
# - Settings - #wal_level = minimal # minimal, archive, or hot_standby wal_level = hot_standby
プライマリーサーバのpostgres.conf archiving
# - Archiving - #archive_mode = off # allows archiving to be done # (change requires restart) #archive_command = '' # command to use to archive a logfile segment #archive_timeout = 0 # force a logfile segment switch after this # number of seconds; 0 disables archive_mode = on archive_command = 'cp %p /var/lib/pgsql/9.0/archive/%f'
プライマリーサーバのpostgres.conf Streaming Replication
# - Streaming Replication - #max_wal_senders = 0 # max number of walsender processes #wal_sender_delay = 200ms # walsender cycle time, 1-10000 milliseconds #wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables #vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed max_wal_senders = 5 wal_keep_segments = 100
上記でプライマリサーバでの準備が終わりましたので
ホットスタンバイサーバ側で作業をします。
# chown -R postgres:postgres /var/lib/pgsql/9.0/data # mkdir /var/lib/pgsql/9.0/archive # chown -R postgres:postgres /var/lib/pgsql/9.0/archive # rm /var/lib/pgsql/9.0/data/postmaster.pid # vi /var/lib/pgsql/9.0/data/postgresql.conf # vi /var/lib/pgsql/9.0/data/recovery.conf # /etc/init.d/postgresql-9.0 start
ホットスタンバイサーバの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 # IPv6 local connections: #host all all ::1/128 trust host all all 0.0.0.0/0 md5 host replication postgres 192.168.14.101/32 md5
ホットスタンバイサーバのpostgres.conf settings
# - Settings - #wal_level = minimal # minimal, archive, or hot_standby wal_level = hot_standby
ホットスタンバイサーバのpostgres.conf archiving
# - Archiving - #archive_mode = off # allows archiving to be done # (change requires restart) #archive_command = '' # command to use to archive a logfile segment #archive_timeout = 0 # force a logfile segment switch after this # number of seconds; 0 disables archive_mode = on archive_command = 'cp %p /var/lib/pgsql/9.0/archive/%f'
ホットスタンバイサーバのpostgres.conf Streaming Replication
# - Streaming Replication - #max_wal_senders = 0 # max number of walsender processes #wal_sender_delay = 200ms # walsender cycle time, 1-10000 milliseconds #wal_keep_segments = 0 # in logfile segments, 16MB each; 0 disables #vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed max_wal_senders = 5 wal_keep_segments = 100
ホットスタンバイサーバのpostgres.conf hot_standby
# - Standby Servers - #hot_standby = off # "on" allows queries during recovery #max_standby_archive_delay = 30s # max delay before canceling queries # when reading WAL from archive; # -1 allows indefinite delay #max_standby_streaming_delay = 30s # max delay before canceling queries # when reading streaming WAL; # -1 allows indefinite delay hot_standby = on
ホットスタンバイサーバのrecovery.conf
primary_conninfo = 'host=192.168.14.101 port=5432 user=postgres password=postgres' standby_mode = 'on' restore_command = 'cp /var/lib/pgsql/9.0/archive/%f %p' trigger_file = '/var/lib/pgsql/9.0/data/trigger_file'
構築後の検証を軽くしてみます。
◯その1 ホットスタンバイサーバにinsertしてみる。
→結果NG。正しい!
# su - postgres -bash-3.2$ psql sample psql (9.0.0) "help" でヘルプを表示します. sample=# insert into test values(2); ERROR: cannot execute INSERT in a read-only transaction sample=# \q -bash-3.2$ exit logout
◯その2 プライマリサーバでテーブルを作成し、insertしてみた後、
スタンバイサーバにselect。
→テーブルが出来ていてselect出来た!
# su - postgres -bash-3.2$ psql sample psql (9.0.0) "help" でヘルプを表示します. sample=# select * from test; ID ---- 1 (1 行) sample=# \q -bash-3.2$ exit logout
◯その3 スタンバイサーバを停止後、プライマリサーバでinsert。
その後、スタンバイサーバを起動してselect。
→起動後に期待通りのselect結果が返ってた。非同期なので正しい動き。
※select結果に2がないのは手動insert時に忘れて飛ばしてしまいました><
# su - postgres -bash-3.2$ psql sample psql (9.0.0) "help" でヘルプを表示します. sample=# select * from test; ID ---- 1 3 4 (3 行) sample=# \q -bash-3.2$ exit logout
◯その4 トリガーファイルをホットスタンバイサーバに設置して
プライマリサーバに昇格させる。元ホットスタンバイサーバで
insertを実行。
→ホットスタンバイサーバでinsertが成功する。
また、/var/lib/pgsql/9.0/archiveにWALログがはかれるようになる。
# su - postgres -bash-3.2$ psql sample psql (9.0.0) "help" でヘルプを表示します. sample=# insert into test values (5); INSERT 0 1 sample=# select * from test; ID ---- 1 3 4 5 (4 行) sample=# \q -bash-3.2$ exit logout # ls /var/lib/pgsql/9.0/archive 000000010000000000000012 000000010000000000000013 00000002.history
という事で構築後の連携はslonyと違ってDDLも伝播してくるので楽です。
pgpool-IIとの連携や冗長化等を今度やってみたいと思います。