postgresql 高可用 repmgr 的使用之三 1 Primary + 1 Standby 安裝
阿新 • • 發佈:2018-12-11
os:ubunbu 16.04 postgresql:9.6.8 repmgr:4.1.1
192.168.56.101 node1 192.168.56.102 node2
安裝 postgresql 9.6 軟體
node1、node2都需要使用 apt install 安裝 postgreql9.6.8,最後一位代表的是補丁號
# apt install postgresql-plpython-9.6 postgresql-plperl-9.6 postgresql-pltcl-9.6 postgresql-doc-9.6 postgresql-contrib-9.6 postgresql-client-9.6 postgresql-client-common postgresql-common postgresql-9.6 postgresql-server-dev-9.6 libpq-dev libpq5
使用 apt 或者 yum 的好處就是依賴包都會被安裝上,比較省心些。 安裝好後都會自動initdb初始化資料庫叢集,需要主要的是node2上的PG_DATA下的檔案需要刪除,因為資料需要從node1同步。
更多詳細過程略。
設定PG_BINDIR
node1、node2都需要設定
# vi /etc/profile
PG_BINDIR=/usr/lib/postgresql/9.6/bin
安裝 repmgr
node1、node2都需要安裝2ndQuadrant public apt repository for Debian/Ubuntu
# curl https://dl.2ndquadrant.com/default/release/get/deb | sudo bash # apt install postgresql-9.6-repmgr repmgr-common
建立user、database
node1 上操作
$ createuser -s repmgr
$ createdb repmgr -O repmgr
$ psql -c "alter user repmgr with password 'repmgrrepmgr'"
$ psql -c "alter user repmgr set search_path to repmgr, \"\$user\", public";
修改 pg_hba.conf
node1、node2都需要操作,其實stream replication環境的各個節點的該配置檔案應該相同,方便切換.
$ vi /etc/postgresql/9.6/main/pg_hba.conf local repmgr repmgr trust host repmgr repmgr 127.0.0.1/32 trust host repmgr repmgr 192.168.56.0/24 trust local replication repmgr trust host replication repmgr 127.0.0.1/32 trust host replication repmgr 192.168.56.0/24 trust
修改postgresql.conf
node1、node2都需要操作,其實stream replication環境的各個節點的該配置檔案應該相同,方便切換. 需要設定成 stream replicator 建議的引數,有個引數需要注意修改下,免得後面修改後又要重啟例項。
$ vi /etc/postgresql/9.6/main/postgresql.conf
shared_preload_libraries = 'repmgr,pg_stat_statements'
/etc/repmgr.conf
node1 節點的檔案內容如下:
$ vi /etc/repmgr.conf
node_id=1
node_name=node1
conninfo='host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/9.6/main'
node2 節點的檔案內容如下:
$ vi /etc/repmgr.conf
node_id=2
node_name=node2
conninfo='host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/var/lib/postgresql/9.6/main'
Register the primary server
node1 節點上操作
$ repmgr -f /etc/repmgr.conf primary register
$ repmgr -f /etc/repmgr.conf cluster show
$ psql -U repmgr
repmgr=# SELECT * FROM repmgr.nodes;
Clone the standby server
node2 節點上操作
$ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
$ repmgr -h 192.168.56.101 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone
–dry-run 意思就是
--dry-run show what would happen for action, but don't execute it
Verify replication is functioning
node1 節點上檢視
$ psql -U repmgr
repmgr=# SELECT * FROM pg_stat_replication;
node2 節點上檢視
$ psql -U repmgr
repmgr=# select * from pg_stat_wal_receiver;
Register the standby
$ repmgr -f /etc/repmgr.conf standby register
$ repmgr -f /etc/repmgr.conf cluster show
ID | Name | Role | Status | Upstream | Location | Connection string
----+-------+---------+-----------+----------+----------+-----------------------------------------------------------------
1 | node1 | primary | * running | | default | host=192.168.56.101 user=repmgr dbname=repmgr connect_timeout=2
2 | node2 | standby | running | node1 | default | host=192.168.56.102 user=repmgr dbname=repmgr connect_timeout=2
可以看到 cluster show 的結果符合預期。