ユーザ用ツール

サイト用ツール


01_linux:11_データベース:02_postgresql:01_streaming_replica

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
01_linux:11_データベース:02_postgresql:01_streaming_replica [2022/07/11 06:03] matsui01_linux:11_データベース:02_postgresql:01_streaming_replica [2022/07/11 08:08] (現在) matsui
行 3: 行 3:
 WALをスタンバイサーバへ転送する事で高可用性と読取に対するスケールアウトを実現するストリーミングレプリケーションを作成 WALをスタンバイサーバへ転送する事で高可用性と読取に対するスケールアウトを実現するストリーミングレプリケーションを作成
  
 +
 +^hostname^IP^
 +|pg1001|172.16.0.51|
 +|pg1002|172.16.0.52|
 +|pg1003|172.16.0.53|
 ===== インストール ===== ===== インストール =====
  
行 193: 行 198:
            ├─19397 postgres: walsender postgres 172.16.0.52(53948) streaming 0/6000060            ├─19397 postgres: walsender postgres 172.16.0.52(53948) streaming 0/6000060
            └─19404 postgres: walsender postgres 172.16.0.53(41534) streaming 0/6000060            └─19404 postgres: walsender postgres 172.16.0.53(41534) streaming 0/6000060
 +</code>
 +
 +Primaryではwalsenderプロセスが存在する。
 +<code>
 +# ps auxw| grep [w]alsender
 +postgres 19068  0.0  0.0 398732  4288 ?        Ss   06:36   0:00 postgres: walsender postgres 172.16.0.53(43426) streaming 0/8000148
 +postgres 19071  0.0  0.0 398228  3768 ?        Ss   06:37   0:00 postgres: walsender postgres 172.16.0.51(39280) streaming 0/8000148
 </code> </code>
  
行 213: 行 225:
            ├─ 8159 postgres: stats collector             ├─ 8159 postgres: stats collector 
            └─18601 postgres: walreceiver streaming 0/6004FA0            └─18601 postgres: walreceiver streaming 0/6004FA0
 +</code>
 +
 +
 +Secondaryでは、walreceiverも動いている
 +<code>
 +# ps auxw| grep [w]alreceiver
 +postgres 19711  0.0  0.0 408884  4712 ?        Ss   06:37   0:03 postgres: walreceiver streaming 0/8000148
 +</code>
 +
 +==== 更新確認 ====
 +
 +=== Primary更新 ===
 +
 +Primaryが更新されると、Secondaryもちゃんとデータ更新されている。
 +<code>
 +$ psql -h 172.16.0.51 -U postgres  testdb -c "UPDATE actor set first_name='BBB' where actor_id=1"
 +UPDATE 1
 +
 +
 +$ psql -h 172.16.0.52 -U postgres  testdb -c "SELECT * FROM actor where actor_id=1"
 + actor_id | first_name | last_name |        last_update         
 +----------+------------+-----------+----------------------------
 +        1 | BBB        | Guiness   | 2022-07-11 06:02:21.621425
 +(1 row)
 +
 +$ psql -h 172.16.0.53 -U postgres  testdb -c "SELECT * FROM actor where actor_id=1"
 + actor_id | first_name | last_name |        last_update         
 +----------+------------+-----------+----------------------------
 +        1 | BBB        | Guiness   | 2022-07-11 06:02:21.621425
 +(1 row)
 +
 +</code>
 +
 +Primary以外では更新できない。
 +<code>
 +ubuntu@jump:~$ psql -h 172.16.0.52 -U postgres  testdb -c "UPDATE actor set first_name='BBB' where actor_id=1"
 +ERROR:  cannot execute UPDATE in a read-only transaction
 +</code>
 +
 +===== Primary障害時 =====
 +
 +pg1001が障害発生で、落ちた事を想定
 +
 +  [root@pg1001 ~]# systemctl stop postgresql-13.service
 +
 +
 +pg1002,pg1003ではデータのReadだけはできる
 +<code>
 +$ psql -h 172.16.0.52 -U postgres  testdb -c "SELECT * FROM actor where actor_id=1"
 + actor_id | first_name | last_name |        last_update         
 +----------+------------+-----------+----------------------------
 +        1 | CCC        | Guiness   | 2022-07-11 06:19:15.283699
 +(1 row)
 +
 +$ psql -h 172.16.0.53 -U postgres  testdb -c "SELECT * FROM actor where actor_id=1"
 + actor_id | first_name | last_name |        last_update         
 +----------+------------+-----------+----------------------------
 +        1 | CCC        | Guiness   | 2022-07-11 06:19:15.283699
 +</code>
 +
 +pg1002をPrimaryへ昇格させる
 +
 +Secondaryの要素をコメントアウト
 +<code>
 +[root@pg1002 ~]# vi /data/postgresql.auto.conf
 +#primary_conninfo =
 +</code>
 +
 +Primaryの要素を追加
 +<code>
 +[root@pg1002 ~]# vi /data/postgresql.conf
 +synchronous_standby_names = 'postgresql'
 +</code>
 +
 +
 +Primaryへ昇格
 +<code>
 +[root@pg1002 ~]# systemctl reload postgresql-13.service
 +[root@pg1002 ~]# su - postgres
 +-bash-4.2$ /usr/pgsql-13/bin/pg_ctl promote
 +waiting for server to promote.... done
 +server promoted
 +</code>
 +
 +他のSecondaryの接続先を変更しリロード
 +<code>
 +[root@pg1003 ~]# vi /data/postgresql.auto.conf 
 +host=172.16.0.52
 +
 +host=172.16.0.51
 +
 +[root@pg1003 ~]# systemctl reload postgresql-13.service
 </code> </code>
 ===== 参考 ===== ===== 参考 =====
01_linux/11_データベース/02_postgresql/01_streaming_replica.1657519401.txt.gz · 最終更新: 2022/07/11 06:03 by matsui