2010年11月5日金曜日

PostgreSQL 9.0でレプリケーション

PostgreSQL 9.0でレプリケーション機能が追加されたので、試してみました。

環境は、OSにubuntu 10.04 LTS Serverを使ってみました。
ESXi4.1に仮想PCを2台用意して、それぞれにubuntuを入れて、PostgreSQLをソースからビルドしておきます。

■レプリケーション元のPostgreSQLの設定を変更
まず、PostgreSQLを停止します。
$ sudo /etc/init.d/postgresql stop
PostgreSQLの管理ユーザーになって、設定ファイルを編集します。
$ su - postgres
$ cd /usr/local/pgsql/data
postgresql.confに次の設定を追加します。
listen_addresses = '*'
port = 5432
wal_level = hot_standby
archive_mode = on
archive_command = 'cp %p /usr/local/pgsql/data/pg_archive/%f'
max_wal_senders = 3
pg_hba.confに次の設定を追加します。
host    replication     all             192.168.168.0/24        trust
アーカイブディレクトリを作成します。
$ mkdir pg_archive
PostgreSQLの管理ユーザーを抜けます。
$ exit

■レプリケーション先のPostgreSQLの設定を変更
まず、PostgreSQLを停止します。
$ sudo /etc/init.d/postgresql stop
PostgreSQLの管理ユーザーになって、設定ファイルを編集します。
$ su - postgres
$ cd /usr/local/pgsql/data
postgresql.confに次の設定を追加します。
listen_addresses = '*'
port = 5432
wal_level = hot_standby
archive_mode = on
archive_command = 'cp %p /usr/local/pgsql/data/pg_archive/%f'
max_wal_senders = 3
hot_standby = on
pg_hba.confに次の設定を追加します。
host    replication     all             192.168.168.0/24        trust
recovery.confというファイルを作成して、次の設定を記述します。
standby_mode = 'on'
primary_conninfo = 'host=レプリケーション元のホスト port=5432 user=postgres'
restore_command = 'cp /usr/local/pgsql/data/pg_archive/%f %p'
trigger_file = '/usr/localpgsql/data/trigger'
アーカイブディレクトリを作成します。
$ mkdir pg_archive
PostgreSQLの管理ユーザーを抜けます。
$ exit

■レプリケーション元のPostgreSQLを起動して、データベースを作成
$ sudo /etc/init.d/postgresql start
$ su - postgres
$ createdb testdb
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 postgres  | postgres | UTF8     | C         | C     |
 template0 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C         | C     |
(4 rows)

■レプリケーション元のデータをレプリケーション先にコピー
レプリケーション元のデータをレプリケーション先にコピーします。
$ cd /usr/local/pgsql
$ tar zcvf data.tar.gz data
$ scp data.tar.gz レプリケーション先のホスト:/usr/local/pgsql/
レプリケーション先で
$ su - postgres
$ cd /usr/local/pgsql
設定ファイルのバックアップを取っておきます。
$ mkdir ~/conf_backup
$ cp -a data/*.conf ~/conf_backup/
レプリケーション元のデータを展開します。
$ tar zxvf data.tar.gz
バックアップした設定ファイルを戻します。
$ cp -a ~/conf_backup/*.conf data/
プロセスIDは削除します。
$ rm -f data/postmaster.pid
PostgreSQLを起動します。
$ exit
$ sudo /etc/init.d/postgresql start

■確認
レプリケーション元でtestdbにテーブルを作成してみます。
$ su - postgres
$ psql testdb
testdb=# create table table1 (id integer, create_date timestamp default current_timestamp);
testdb=# \d
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 public | table1 | table | postgres
(1 row)
testdb=# \d table1
                   Table "public.table1"
   Column    |            Type             |   Modifiers
-------------+-----------------------------+---------------
 id          | integer                     |
 create_date | timestamp without time zone | default now()
レプリケーション先で確認します。
$ su - postgres
$ psql testdb
testdb=# \dt
         List of relations
 Schema |  Name  | Type  |  Owner
--------+--------+-------+----------
 public | table1 | table | postgres
(1 row)
testdb=# \d table1
                   Table "public.table1"
   Column    |            Type             |   Modifiers
-------------+-----------------------------+---------------
 id          | integer                     |
 create_date | timestamp without time zone | default now()
レプリケーション元でデータベースを作成します。
$ createdb testdb2
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 postgres  | postgres | UTF8     | C         | C     |
 template0 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C         | C     |
 testdb2   | postgres | UTF8     | C         | C     |
(5 rows)
レプリケーション先で確認します。
$ psql -l
                              List of databases
   Name    |  Owner   | Encoding | Collation | Ctype |   Access privileges
-----------+----------+----------+-----------+-------+-----------------------
 postgres  | postgres | UTF8     | C         | C     |
 template0 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C         | C     | =c/postgres          +
           |          |          |           |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C         | C     |
 testdb2   | postgres | UTF8     | C         | C     |
(5 rows)

割と簡単に設定できました。
そのうち、レプリケーション先の追加や、レプリケーション先の昇格などやってみたいと思います。

0 コメント :

コメントを投稿