標籤:postgresql 非同步流複製 standby自動啟用
節點 |
IP |
角色 |
citus-master |
10.10.100.1 |
master |
citus-standby |
10.10.100.2 |
standby |
master上建立流複製所需要的使用者。
CREATE ROLE replication WITH REPLICATION PASSWORD ‘replication‘ LOGIN;
修改master的pg_hba.conf檔案,設定replication使用者遠端存取許可權
## vim /data/pgsql/data/pg_hba.conf,追加下面一行host replication replication 10.10.0.0/16 md5
master上設定與複製相關的參數
wal_level = hot_standbymax_wal_senders = 5wal_keep_segments = 32
master上重啟pgsql
/etc/init.d/postgresql-9.5 restart
standby上使用pg_basebackup對master做一次全備並將備份拉到standby上來
pg_basebackup -h 10.10.100.1 -D /var/lib/pgsql/9.5/data/ -P -U replication -R --xlog-method=stream
修改standby節點上的postgresql.conf檔案,設定備庫為standby的狀態
## vim /data/pgsql/data/postgresql.confhot_standby = on
修改standby的recovery.done檔案,設定master節點的同步資訊及trigger檔案
standby_mode = ‘on‘primary_conninfo = ‘user=replication password=replication host=10.10.100.1 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres‘trigger_file = ‘/data/pgsql/data/postgresql.trigger.1973‘
standby上重啟pgsql
/etc/init.d/postgresql-9.5 restart
可以在master上建立一張測試表並插入資料,來驗證standby上是否同步過來資料。當master掛掉之後,在standby上建立trigger_file參數聲明的檔案,就會觸發standby的啟用,會自動把standby提升為master。
當master掛掉之後,standby上建立trigger file之後,recovery.conf將會重新命名為recovery.done。這時standby將會接管master進入可讀寫狀態。緊接著可以將原來的master設定為現在的standby,過程如下
## 建立recovery.conf,寫入下面內容recovery_target_timeline = ‘latest‘standby_mode = ‘on‘primary_conninfo = ‘user=replication password=replication host=10.10.100.2 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres‘
## vim postgresql.conf檔案,開啟hot_standbyhot_standby = on
最後重啟pgsql,並查看日誌
/etc/init.d/postgresql-9.5 restarttail -9f pg_log/postgresql-Tue.log< 2016-11-01 16:51:53.590 CST >LOG: shutting down< 2016-11-01 16:51:53.594 CST >LOG: database system is shut down< 2016-11-01 16:51:54.679 CST >LOG: database system was shut down in recovery at 2016-11-01 16:51:53 CST< 2016-11-01 16:51:54.679 CST >LOG: entering standby mode< 2016-11-01 16:51:54.692 CST >LOG: consistent recovery state reached at 0/F001430< 2016-11-01 16:51:54.692 CST >LOG: redo starts at 0/F001430< 2016-11-01 16:51:54.692 CST >LOG: database system is ready to accept read only connections< 2016-11-01 16:51:54.692 CST >LOG: invalid record length at 0/F001510< 2016-11-01 16:51:54.697 CST >LOG: started streaming WAL from primary at 0/F000000 on timeline 3
本文出自 “勇敢向前,堅決向左” 部落格,謝絕轉載!
postgresql非同步流複製搭建