PostgreSQLストリーミングレプリケーション 第2回 インストールと初期設定

2014/11/11 技術系 投稿者:

前回ではストリーミングレプリケーションの概要についてお話しましたが、第2回ではPostgreSQLのインストールとレプリケーション初期設定について解説いたします。

プライマリ(更新・参照可能)-セカンダリ型(参照のみ)の2台構成で進めます。

用意したもの

  • サーバ2台(CentOS 6.6 64bit版をインストール済み)

以下のような構成です。

それでは早速PostgreSQLのインストールと設定を行っていきましょう。

PostgreSQLのインストール

このサーバにPostgreSQLをsourceからインストールします。
マスターとスレーブ共通です。

# cd /usr/local/src
# yum install readline-devel zlib-devel perl-ExtUtils-Embed
# wget http://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.gz
# useradd postgres
# mkdir /usr/local/pgsql
# chown -R postgres:postgres /usr/local/pgsql
# tar zxf postgresql-9.3.5.tar.gz
# chown -R postgres:postgres /usr/local/src/postgresql-9.3.5
# su - postgres

$ cd /usr/local/src/postgresql-9.3.5
$ ./configure
$ make
$ make install

$ vi ~postgres/.bashrc
-----------------------------------
# PostgreSQL Config
PG=/usr/local/pgsql
PATH="$PATH":$PG/bin
export MANPATH="$MANPATH":$PG/man
export PGLIB=$PG/lib
export PGDATA=/usr/local/pgsql/data
-----------------------------------
$ source ~/.bashrc
$ initdb --encoding=UTF-8 --no-locale
$ exit

/* 起動スクリプトの設置 */
# cd /usr/local/src/postgresql-9.3.5
# cp contrib/start-scripts/linux /etc/rc.d/init.d/postgres
# chmod +x /etc/rc.d/init.d/postgres
# chkconfig postgres on

# /etc/rc.d/init.d/postgres start

PostgreSQLが起動すればこれでひとまずインストールは完了。

これをマスターとスレーブの2台分行います。

マスターのレプリケーション初期設定

続いてレプリケーションを使用するための設定を行います。

マスターのpostgresql.confを修正

# su - postgres
$ cd /usr/local/pgsql/data
$ vi postgresql.conf
---------------------------------------------
listen_addresses = 'localhost,192.168.1.201'
port = 5432
wal_level = hot_standby

/* ここでレプリケーション構成を設定しますが、ひとまず完全同期に設定 */
synchronous_commit = on

max_wal_senders = 3
synchronous_standby_names = '*'

/* ログの出力も設定 */
log_destination = 'syslog'
syslog_facility = 'LOCAL0'
syslog_ident = 'postgres'
---------------------------------------------

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

ユーザー名は何でも良いのですが、今回は「replicationuser」というユーザーでパスワードを「pass1234」と設定します。

$ psql
postgres=# CREATE ROLE replicationuser LOGIN REPLICATION PASSWORD 'pass1234';
CREATE ROLE
postgres=# \q

pg_hba.confを編集しアクセス設定を行う

$ cd /usr/local/pgsql/data
$ vi pg_hba.conf
-----------------------------------------------------------------------------
host    replication     replicationuser             192.168.1.0/24        md5
-----------------------------------------------------------------------------
$ exit

ちなみに「host replication replicationuser」ではなく「host all replicationuser」にしたら、後述のpg_basebackupでpg_hba.confのエラーが出てしまいました。

設定を反映

# /etc/rc.d/init.d/postgres restart

スレーブのレプリケーション初期設定

pg_basebackupによるデータコピー

PosgreSQLを停止し、レプリケーションするためにpg_basebackupというコマンドを使用して、マスターのdata領域を持ってきます。

# /etc/rc.d/init.d/postgres stop
# su - postgres
$ rm -rf /usr/local/pgsql/data
$ pg_basebackup -h 192.168.1.201 -D /usr/local/pgsql/data --xlog
$ cd /usr/local/pgsql/data
$ vi /usr/local/pgsql/data/postgresql.conf
---------------------------------------------
listen_addresses = 'localhost,192.168.1.202'
hot_standby = on
---------------------------------------------

recovery.confの設定

$ vi /usr/local/pgsql/data/recovery.conf
------------------------------------------------------------------------------------------------------------
standby_mode = 'on'

/* ここでは自分自身をapplication_nameで「slave」という名前を指定し
  先ほど作成した「replicationuser」ユーザーでマスターに接続する設定を行います。 */
primary_conninfo = 'host=192.168.1.201 port=5432 user=replicationuser password=pass1234 application_name=slave'
------------------------------------------------------------------------------------------------------------
$ exit

PostgreSQLを起動

# /etc/rc.d/init.d/postgres start

動作確認

マスター側でtest1というデータベースを作成し、スレーブに反映されるか確認してみます。

マスター側でデータベース作成

# su - postgres
$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | 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
(3 rows)

$ createdb test1
$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | 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
 test1     | postgres | UTF8     | C       | C     |
(4 rows)

test1というデータベースが作成されました。

スレーブ側で反映を確認

# su - postgres
$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | 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
 test1     | postgres | UTF8     | C       | C     |
(4 rows)

スレーブ側でも無事test1というデータベースが作成されていることが確認できました。

ちなみにスレーブは参照専用なので、例えばスレーブ側でtest1データベースを削除しようとしてもリードオンリーだというエラーが出て削除できません。

$ dropdb test1
dropdb: database removal failed: ERROR:  cannot execute DROP DATABASE in a read-only transaction

まとめ

というわけで、無事レプリケーション環境構築ができました。実際にちゃんと動作すると嬉しいですね。

次回は「第3回 レプリケーション構成によるパフォーマンス比較」です。

今回設定したレプリケーションがレプリケーション構成別にどのようなパフォーマンスを発揮するのかを比較したいと思います。お楽しみに!

カレンダー

    123
45678910
11121314151617
18192021222324
25262728293031
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
     12
3456789
10111213141516
17181920212223
2425262728  
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
   1234
567891011
12131415161718
19202122232425
26272829   
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
  12345
6789101112
13141516171819
20212223242526
2728     
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
1234567
891011121314
15161718192021
22232425262728
       
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
     12
3456789
10111213141516
17181920212223
24252627282930
31      
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
     12
3456789
10111213141516
17181920212223
242526272829 
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
    123
45678910
11121314151617
18192021222324
25262728   
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
   1234
567891011
12131415161718
19202122232425
262728    
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
     12
3456789
10111213141516
17181920212223
24252627282930
31      
   1234
567891011
12131415161718
19202122232425
2627282930  
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728     
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
1234567
891011121314
15161718192021
22232425262728
29      
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
  12345
6789101112
13141516171819
20212223242526
27282930   
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
      1
2345678
9101112131415
16171819202122
232425262728 
       
   1234
567891011
12131415161718
19202122232425
262728293031 
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
     12
3456789
10111213141516
17181920212223
24252627282930
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
1234567
891011121314
15161718192021
22232425262728
2930     
       
    123
45678910
11121314151617
18192021222324
25262728293031
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
 123456
78910111213
14151617181920
21222324252627
282930    
       
     12
3456789
10111213141516
17181920212223
2425262728  
       
  12345
6789101112
13141516171819
20212223242526
2728293031  
       
      1
2345678
9101112131415
16171819202122
23242526272829
3031     
    123
45678910
11121314151617
18192021222324
252627282930 
       
 123456
78910111213
14151617181920
21222324252627
28293031   
       
      1
2345678
9101112131415
16171819202122
23242526272829
30      
   1234
567891011
12131415161718
19202122232425
262728293031 
       
1234567
891011121314
15161718192021
22232425262728
293031    
       
U・Iターン転職しませんか?
群馬データセンター
群馬の法人ITサポートサービス Wide Net[ワイドネット]
Wide Netのクラウドバックアップ
クラウド型ファイル共有サービスRushDrive
ワイドオフィス
ネディアのSDGsへの取り組み
健康経営優良法人
IT-commons(ITコモンズ)