6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[PostgreSQL] DBレプリケーション環境の構築

Last updated at Posted at 2015-04-15

前提

  • PostgreSQL 9.2系 Linux版 (この記事では 9.2.10 を使用しています)
  • 各ホストでPostgreSQLのインストールが完了してあること
  • マスタ側では、データベースが構築済みであること

マスタの構築

1. 設定の変更

レプリケーション用の設定をします

postgresql.conf
hot_standby = on
synchronous_commit = remote_write
wal_level = hot_standby
max_wal_senders = 32     # slaveの数+1は最低限必要
wal_keep_segments = 128
replication_timeout = 20s
wal_receiver_status_interval = 5s
hot_standby_feedback = on
max_standby_streaming_delay = -1
max_standby_archive_delay = -1

2. スタンバイ系ホストの許可

レプリケーション接続を許可するために、pg_hba.confにスタンバイホストを設定します

pg_hba.conf
host    replication   repli_user     ${STANBY_HOSTS}       md5

3. PostgreSQLを起動

変更した設定を保存した後は、PostgreSQLを起動します

 # su - postgres -c "pg_ctl -D ${DB_DATA_DIR} start"

4. レプリケーション用ユーザの作成

データーベースの管理ユーザー(postgres)や、アクセス用のユーザーとは別にレプリケーション専用のユーザーを作成します。

 # su - postgres -c "psql -h ${DB_HOST} -p ${DB_PORT} -c \"CREATE USER repli_user REPLICATION PASSWORD 'password'\""

※補足

  • ここでは repli_user という名前にしていますが、別の名前でも構いません。別の名前にした場合は pg_basebackup の引数や、 pg_hba.conf、recovery.conf の設定は適宜置き換えてください
  • レプリケーションのパスワードは password になってます。適宜読み替えてください

スタンバイの構築

スタンバイ側はDBが動いていない状態から始めます。(PostgreSQLのプロセスは停止した状態)
以前に動かしていたDBファイルが残っている場合は一旦削除してから行ってください

1. マスタからデータベースをダンプ

# su - postgres  ← 以降の手順はpostgresユーザーで実行
$ mkdir ${DB_DATA_DIR}
$ chmod 700 ${DB_DATA_DIR}
$ pg_basebackup -p ${DB_PORT} -h ${DB_MASTER_HOST} -U repli_user -D ${DB_DATA_DIR} --xlog --checkpoint=fast --progress

※パスワードが聞かれますので、レプリケーションのパスワードを入れてください

2. recovery.confの作成

recovery.conf ファイルを ${DB_DATA_DIR} に作成

recovery.conf
standby_mode = on
primary_conninfo = 'host=${DB_MASTER_HOST} port=${DB_PORT} user=repli_user password=password application_name=${STANDBY_HOSTNAME}'

※補足
マスタとスタンバイの違いは、recovery.confがあるか無いかです。recovery.confを削除すればマスタとして動作するようになります。

3. PostgreSQLを起動

PostgreSQLを起動します

$ pg_ctl -D ${DB_DATA_DIR} start

レプリケーションが正常に動作していることの確認

マスタ側で pg_stat_replication テーブルの内容を確認します


$ psql -x -U ${DB_USER} -d ${DB_NAME} -p ${DB_PORT} -c "SELECT * FROM pg_stat_replication"

実行例


$ psql -x -U postgres -d testdb -p 5434 -c "SELECT * FROM pg_stat_replication"
-[ RECORD 1 ]----+------------------------------
pid              | 13849
usesysid         | 16402
usename          | repli_user
application_name | vm-2
client_addr      | 192.168.1.2
client_hostname  |
client_port      | 39206
backend_start    | 2015-09-08 07:24:42.168487+09
state            | streaming
sent_location    | 0/70000E0
write_location   | 0/70000E0
flush_location   | 0/70000E0
replay_location  | 0/70000E0
sync_priority    | 1
sync_state       | sync
  • state が streaming になっていればマスタの更新がリアルタイムにスタンバイに反映されています
  • 各_location の値が同じになれば、マスタとスタンバイで完全に同期が取れています
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?