作者:强压谷攻 | 来源:互联网 | 2023-09-07 12:52
我已经测试了在PostgreSQL 9.4.1版本中进行切换和切回的不同方案。
场景1:- PostgreSQL Switchover and Switchback in 9.4.1
场景2:- Is it mandatory parameter recover_target_timeline='latest' in switchover and switchback in PostgreSQL 9.4.1?
方案3:-在此页面上
要测试方案3,请按照以下步骤执行。
1)停止连接到主服务器的应用程序。
2)确认所有应用程序均已停止并且所有线程已与主DB断开连接。
@ 192.x.x.129(主要)
3)使用以下方法清除关机主服务器
pg_ctl -D$PGDATA stop --mf
@DR(192.x.x.128)侧面检查同步状态:
postgres=# select pg_last_xlog_receive_location(),pg_last_xlog_replay_location();
-[ RECORD 1 ]-----------------+-----------
pg_last_xlog_receive_location | 4/57000090
pg_last_xlog_replay_location | 4/57000090
4)停止DR服务器.DR(192.x.x.128)
pg_ctl -D $PGDATA stop -mf
pg_log:
2019-12-02 13:16:09 IST LOG: received fast shutdown request
2019-12-02 13:16:09 IST LOG: aborting any active transactions
2019-12-02 13:16:09 IST LOG: shutting down
2019-12-02 13:16:09 IST LOG: database system is shut down
@ 192.x.x.128(DR)
5)在灾难恢复服务器上进行以下更改。
mv recovery.conf recovery.conf_bkp
6)在192.x.x.129(主要)中进行更改:
[postgres@localhost data]$ cat recovery.conf
standby_mode = 'on'
primary_cOnninfo= 'user=replication password=postgres host=192.x.x.128 port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'
restore_command = 'cp %p /home/postgres/restore/%f'
trigger_file='/tmp/promote'
7)将DR启动为读写模式:
pg_ctl -D $DATA start
pg_log:
2019-12-02 13:20:21 IST LOG: database system was shut down in recovery at 2019-12-02 13:16:09 IST
2019-12-02 13:20:22 IST LOG: database system was not properly shut down; automatic recovery in progress
2019-12-02 13:20:22 IST LOG: consistent recovery state reached at 4/57000090
2019-12-02 13:20:22 IST LOG: invalid record length at 4/57000090
2019-12-02 13:20:22 IST LOG: redo is not required
2019-12-02 13:20:22 IST LOG: database system is ready to accept connections
2019-12-02 13:20:22 IST LOG: autovacuum launcher started
(END)
我们可以在上面的日志中看到OLD primary现在是Primary的DR(以前是OLD DR),并且未显示任何错误,因为新主数据库上的时间轴ID相同,而该ID已在新DR中退出。
8)将Primary作为只读模式启动:-
pg_ctl -D$PGDATA start
日志:
2019-12-02 13:24:50 IST LOG: database system was shut down at 2019-12-02 11:14:50 IST
2019-12-02 13:24:51 IST LOG: entering standby mode
cp: cannot stat ‘pg_xlog/RECOVERYHISTORY’: No such file or directory
cp: cannot stat ‘pg_xlog/RECOVERYXLOG’: No such file or directory
2019-12-02 13:24:51 IST LOG: consistent recovery state reached at 4/57000090
2019-12-02 13:24:51 IST LOG: record with zero length at 4/57000090
2019-12-02 13:24:51 IST LOG: database system is ready to accept read only connections
2019-12-02 13:24:51 IST LOG: started streaming WAL from primary at 4/57000000 on timeline 9
2019-12-02 13:24:51 IST LOG: redo starts at 4/57000090
(END)
问题1:-在这种情况下,我仅执行切换以显示给您。使用这种方法,我们可以进行切换和切回。但是使用下面的方法进行切换是可行的,那么为什么PostgreSQL社区发明了recovery_target_timeline = latest并应用补丁,请参见博客:https://www.enterprisedb.com/blog/switchover-switchback-in-postgresql-9-3从PostgrSQL 9.3 ...到最新版本。
问题2:-在上面的日志cp: cannot stat ‘pg_xlog/RECOVERYHISTORY’: No such file or directory
中要说什么?
问题3:-我想从方案1和方案3中确定哪种方法/方案是进行切换和切回的正确方法?因为方案2出现错误,因为我们必须使用所有社区专家都知道的recover_target_timeline=latest
。
答案:
-
如果完全关闭备用数据库,然后删除recovery.conf
并重新启动它,它将出现,但必须执行崩溃恢复(database system was not properly shut down
)。
将备用数据库提升为主数据库的正确方法是使用触发文件或运行pg_ctl promote
(或者从v12开始,通过运行SQL函数pg_promote
)。这样您就无需停机,也无需执行崩溃恢复。
升级备用数据库将使其选择新的时间轴,因此,如果要新的备用数据库遵循该时间轴切换,则需要recovery_target_timeline = 'latest'
。
-
这是由您的restore_command
引起的。
-
上面1.中显示的方法是正确的方法。