导航

PostgreSQL手动主从切换

Posted on 2018-09-29 16:36  许爱琪  阅读(1697)  评论(0编辑  收藏  举报

主从切换操作:

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=#

表明切换成功