主從切換操作:
1》主庫宕機或者測試主備切換情況下停掉主庫:systemctl stop postgres
從庫會報日志錯誤信息:
[root@db02 /]# cd /var/postgresql/logfile/
[root@db02 logfile]# ll
total 4
-rw-rw-r--. 1 postgres postgres 3074 Sep 29 16:02 serverlog
[root@db02 logfile]# tail -f serverlog
TCP/IP connections on port 5432?
2018-09-29 08:02:11.873 GMT [10205] FATAL: could not connect to the primary server: could not connect to server: Connection refused
Is the server running on host "172.17.0.3" and accepting
TCP/IP connections on port 5432?
2》原從庫操作(原主庫宕機情況下將其作為主庫操作):
在之前備機上的recovery.conf中配置trigger_file='/var/postgresql/logfile/trigger.unl'
touch /var/postgresql/logfile/trigger.unl
修改 pg_hba.conf:
增加
host replication replica 0.0.0.0/0 md5
重啟從庫: systemctl restart postgresql
查看是否切換成功:/usr/pgsql9.3.4/bin/pg_controldata -》Database cluster state: in production 表示是主庫
recovery.conf文件名字變成了recovery.done
3》原主庫操作(恢復原主庫為從庫):
cp /usr/local/src/postgresql-10.5/share/recovery.conf.sample /var/postgresql/data/recovery.conf
修改recovery.conf:
standby_mode = on
primary_conninfo = 'host=172.17.0.4 port=5432 user=replica password=replica'
recovery_target_timeline = 'latest'
修改postgresql.conf文件:
hot_standby = on
啟動原主庫(當前從庫):systemctl restart postgresql
4》檢查主從是否切換成功:
在新的主庫上執行:
postgres=# select * from pg_stat_replication;
pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn |
replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state
-------+----------+---------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+
------------+-----------+-----------+------------+---------------+------------
10361 | 16385 | replica | walreceiver | 172.17.0.3 | | 50896 | 2018-09-29 08:24:28.192643+00 | | streaming | 0/70181D8 | 0/70181D8 | 0/70181D8 |
0/70181D8 | | | | 0 | async
(1 row)
postgres=#
表明切換成功