postgres主备库切换测试
主备库切换常用的有两种方式,第一种是使用触发文件,9.0之前的版本只能使用此种方式切换,第二个是使用命令promote的方式。
切换之前需要查看主备库的角色,查看角色的方式可以参考:
https://blog.csdn.net/m15217321304/article/details/86628843
文件触发方式的主要步骤如下:
1) 配置备库的recovery.conf文件trigger_file参数。
2)关闭主库,关闭方式建议使用 -m fast,干净的关闭
3)在备库上面创建trigger_file指定的文件,如果备库激活成功,recovery.conf会变成recovery.done
4) 原主库创建recovery.conf文件,然后按照配置备库的方式修改recovery.conf文件。
5)启动原主库(现在的备库)
1、主库IP 192.168.40.130 主机名:postgres 端口:5442
备库IP 192.168.40.131 主机名:postgreshot 端口:5442
2、查询当前数据库状态
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 43754
usesysid | 16384
usename | replica
application_name | walreceiver
client_addr | 192.168.40.131
client_hostname |
client_port | 36568
backend_start | 2019-01-24 16:01:03.500056-05
backend_xmin | 582
state | streaming
sent_lsn | 0/3033760
write_lsn | 0/3033760
flush_lsn | 0/3033760
replay_lsn | 0/3033760
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
3、配置备库的recovery.conf文件
[postgres@postgreshot pg11]$ hostname
postgreshot
[postgres@postgreshot pg11]$ cat recovery.conf |grep -iv '^#'
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.40.130 port=5442 user=replica' # e.g. 'host=localhost port=5432'
trigger_file = '/home/postgres/pg11/trigger'
[postgres@postgreshot pg11]$
备注:主备库的认证方式使用的.pgpass方式,建议不要把密码文件直接配置到recovery.conf文件里面。调整以上参数需要重启才能生效。
4、停止主库
[postgres@postgres ~]$ hostname
postgres
[postgres@postgres ~]$ pg_ctl stop -m fast
waiting for server to shut down.... done
server stopped
[postgres@postgres ~]$
5、在备库上激活成主库
激活之前的cluster state
[postgres@postgreshot pg11]$ pg_controldata | grep 'cluster'
Database cluster state: in archive recovery
[postgres@postgreshot pg11]$
创建激活需要的文件
[postgres@postgreshot pg11]$ touch /home/postgres/pg11/trigger
[postgres@postgreshot pg11]$ ls -ltr
total 140
drwx------ 4 postgres postgres 4096 Jan 24 08:03 pg_multixact
-rwx------ 1 postgres postgres 1636 Jan 24 08:03 pg_ident.conf
-rwx------ 1 postgres postgres 224 Jan 24 08:03 backup_label.old
-rwx------ 1 postgres postgres 88 Jan 24 08:03 postgresql.auto.conf
-rwx------ 1 postgres postgres 3 Jan 24 08:03 PG_VERSION
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_commit_ts
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_twophase
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_tblspc
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_serial
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_replslot
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_dynshmem
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_xact
drwx------ 2 postgres postgres 4096 Jan 24 08:03 pg_snapshots
drwx------ 2 postgres postgres 4096 Jan 24 08:15 pg_subtrans
-rwx------ 1 postgres postgres 24406 Jan 24 10:31 postgresql.conf
drwx------ 5 postgres postgres 4096 Jan 24 10:36 base
-rwx------ 1 postgres postgres 4705 Jan 24 11:40 pg_hba.conf
-rwx------ 1 postgres postgres 5923 Jan 24 20:47 recovery.done <=====从recovery.conf变成了done
注:触发文件的位置必须和recovery.conf中配置的一致,激活之后conf变成了done。
激活之后的cluster state
[postgres@postgreshot pg11]$ pg_controldata |grep 'cluster'
Database cluster state: in production
[postgres@postgreshot pg11]$
6、将新主库(主机名:postgreshot)的recovery.done文件拷贝到原主库
[postgres@postgreshot pg11]$ scp recovery.done postgres:/home/postgres/pg11/
recovery.done 100% 5923 5.8KB/s 00:00
[postgres@postgreshot pg11]$
7、把recovery.done修改成recovery.conf,并修改如下内容
[postgres@postgres pg11]$ cat recovery.conf |grep -iv '^#'
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.40.131 port=5442 user=replica' # e.g. 'host=localhost port=5432'
trigger_file = '/home/postgres/pg11/trigger'
[postgres@postgres pg11]$
8、启动备库
[postgres@postgres pg11]$
[postgres@postgres pg11]$ hostname
postgres
[postgres@postgres pg11]$ pg_ctl start
waiting for server to start....2019-01-24 21:00:59.533 EST [77641] LOG: listening on IPv4 address "0.0.0.0", port 5442
2019-01-24 21:00:59.533 EST [77641] LOG: listening on IPv6 address "::", port 5442
2019-01-24 21:00:59.541 EST [77641] LOG: listening on Unix socket "/tmp/.s.PGSQL.5442"
2019-01-24 21:00:59.555 EST [77642] LOG: database system was shut down at 2019-01-24 20:51:19 EST
2019-01-24 21:00:59.555 EST [77642] LOG: entering standby mode
2019-01-24 21:00:59.581 EST [77642] LOG: consistent recovery state reached at 0/30337D0
2019-01-24 21:00:59.581 EST [77642] LOG: invalid record length at 0/30337D0: wanted 24, got 0
2019-01-24 21:00:59.582 EST [77641] LOG: database system is ready to accept read only connections
2019-01-24 21:00:59.659 EST [77646] LOG: fetching timeline history file for timeline 2 from primary serverdone
server started
[postgres@postgres pg11]$ 2019-01-24 21:00:59.690 EST [77646] LOG: started streaming WAL from primary at 0/3000000 on timeline 1
2019-01-24 21:00:59.693 EST [77646] LOG: replication terminated by primary server
2019-01-24 21:00:59.693 EST [77646] DETAIL: End of WAL reached on timeline 1 at 0/30337D0.
2019-01-24 21:00:59.704 EST [77642] LOG: new target timeline is 2
2019-01-24 21:00:59.706 EST [77646] LOG: restarted WAL streaming at 0/3000000 on timeline 2
2019-01-24 21:00:59.973 EST [77642] LOG: redo starts at 0/30337D0[postgres@postgres pg11]$ ps -ef |grep postgres
root 64511 63718 0 19:05 pts/4 00:00:00 su - postgres
postgres 64512 64511 0 19:05 pts/4 00:00:00 -bash
postgres 77641 1 0 21:00 pts/4 00:00:00 /opt/pgsql11/bin/postgres
postgres 77642 77641 0 21:00 ? 00:00:00 postgres: startup recovering 000000020000000000000003
postgres 77643 77641 0 21:00 ? 00:00:00 postgres: checkpointer
postgres 77644 77641 0 21:00 ? 00:00:00 postgres: background writer
postgres 77645 77641 0 21:00 ? 00:00:00 postgres: stats collector
postgres 77646 77641 0 21:00 ? 00:00:00 postgres: walreceiver streaming 0/3033918
postgres 77683 64512 0 21:01 pts/4 00:00:00 ps -ef
postgres 77684 64512 0 21:01 pts/4 00:00:00 grep postgres
[postgres@postgres pg11]$
9、查询cluster state
[postgres@postgres pg11]$ pg_controldata |grep 'cluster'
Database cluster state: in archive recovery
[postgres@postgres pg11]$
可以看到已经切换成功。
二、第二种切换方式pg_ctl promote方式
命令格式如下:
pg_ctl promote [-D datadir]
promote命令发出后,运行中的备库将停止恢复模式并切换成读写模式的主库。
切换步骤:
1)关闭主库,建议使用-m fast的模式
2)在备库上面执行pg_ctl promote 命令激活成主库,如果recovery.conf 变成了recovery.done表示备库已切换成主库。
3)在原主库创建recovery.conf文件。
4)启动原主库
演示过程
因为上面已经把主库postgres切换成了备库,postgreshot备库已经切换成主库.
当前主库
[postgres@postgreshot pg11]$ hostname
postgreshot
[postgres@postgreshot pg11]$ pg_controldata | grep 'cluster'
Database cluster state: in production
[postgres@postgreshot pg11]$
当前备库
[postgres@postgres pg11]$ hostname
postgres
[postgres@postgres pg11]$ pg_controldata |grep 'cluster'
Database cluster state: in archive recovery
[postgres@postgres pg11]$
1、关闭主库
[postgres@postgreshot pg11]$ hostname
postgreshot
[postgres@postgreshot pg11]$ pg_ctl stop -m fast
waiting for server to shut down.... done
server stopped
[postgres@postgreshot pg11]$
2、在备库执行promote命名激活成主库
[postgres@postgres pg11]$ cat recovery.conf |grep -iv '^#'
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.40.131 port=5442 user=replica' # e.g. 'host=localhost port=5432'
[postgres@postgres pg11]$
[postgres@postgres pg11]$ pg_ctl promote -D /home/postgres/pg11
waiting for server to promote.... done
server promoted
[postgres@postgres pg11]$
[postgres@postgres pg11]$
[postgres@postgres pg11]$ ls -ltr recovery*
-rwx------ 1 postgres postgres 5923 Jan 24 20:59 recovery.done
[postgres@postgres pg11]$ pg_controldata |grep 'cluster'
Database cluster state: in production
[postgres@postgres pg11]$
已经切换成主备。
3、把原主库的recovery.done修改成recovery.conf
[postgres@postgreshot pg11]$ mv recovery.done recovery.conf
[postgres@postgreshot pg11]$ cat recovery.conf |grep -iv '^#'
recovery_target_timeline = 'latest'
standby_mode = on
primary_conninfo = 'host=192.168.40.130 port=5442 user=replica' # e.g. 'host=localhost port=5432'
[postgres@postgreshot pg11]$
4、启动原主库
[postgres@postgreshot pg11]$ pg_ctl start
done
server started
[postgres@postgreshot pg11]$
[postgres@postgreshot pg11]$ pg_controldata |grep 'cluster'
Database cluster state: in archive recovery
[postgres@postgreshot pg11]$
原主库变成了备库,切换成功。
postgres主备库切换测试相关推荐
- PostgreSQL 之 流复制主备库切换
作者:瀚高PG实验室 (Highgo PG Lab)- 波罗 在主从复制的两个节点中,当主节点数据库服务访问异常的时候后,可以手动通过promote 命令在从节点执行将从节点状态切换到可读写的状态,变 ...
- 物理DG主备库切换时遇到ORA-16139: media recovery required错误
物理DG主备库切换时遇到ORA-16139: media recovery required错误 在物理DG主备库切换时遇到ORA-16139: media recovery required错误 S ...
- 应用程序主备库切换方案
应用程序主备库切换方案 1. 概述 目的:异地容灾库已经搭建完毕,使用Oracle GoldenGate实现主库和备库的数据实时同步.如果主库存储发生故障,应用程序如何快速切换至备库. 主库和备库SC ...
- Oracle Dataguard 主备库的切换方法
主备库的切换主要在两种情况下切换,Switchover和Failover,这两种切换都需要手工执行完成. 一是Switchover(计划中的切换,不会丢失数据) 二是Failover(当主库出现故障的 ...
- postgresql 主备及切换-恢复方案
前言 前期的gitlab 已经开始推广测试,最近对postgresql 做了主备,这里说下方案及在实施过程中遇到的坑. postgresql 的具安装不在此介绍. 基础信息 primary_ip: 1 ...
- oracle主备库sync模式,Oracle 探索DG备库undo工作模式
模拟备库出现 ORA-01555 分析备库 undo 工作模式 一: 修改主库 备库 undo 表空间 1.在主库创建undo表空间(会自动同步到备库) SYS@prod>create undo ...
- Oracle11g DG实战配置(Windows版)(五)主备库DG配置
5.1 DG主库配置(主库操作) 5.1.1 配置Oracle Data Guard # 登录数据库 sqlplus / as sysdba # 查看数据库归档模式,确认Archivelog已启用 s ...
- oracle 查看日志组切换状态_Oracle 11g ADG 快照备库切换步骤
作者 | JiekeXu 来源 | JiekeXu之路(ID: JiekeXu_IT) 转载请联系授权 | (微信ID:xxq1426321293) 大家好,我是 JiekeXu,很高兴又和大家见面了 ...
- hadoop主节点切换_hadoop2.0 HA的主备自动切换
在<hadoop2.0 QJM方式的HA的配置>一文中介绍了HA的配置,是通过手工进行主备切换的.本文在这基础上,继续介绍HA的主备自动切换(automatic failover)的配置. ...
最新文章
- 【转载】C语言变量详解
- 性能测试总结(一)---基础理论篇
- nginx php-fpm display_errors,php5-fpm的display_errors不能使用nginx
- MAC apache配置
- 微信分享朋友圈固定缩略图 php,微信转发或分享朋友圈带缩略图、标题和描述的实现方法...
- linux下使用ThinkPHP注意大小写问题
- 【golang】从源代码编译golang编译器
- 【flume】flume案例集锦
- windows服务器管理(1)——WinServer2012 开启中文语言包(无需下载补丁)
- ubuntu下点击安装VMware tools,没有压缩包文件
- 2020年 显卡天梯图 / Top Graphics Ranking
- 基于同义词词林的词语间相似度计算
- 【一文搞懂】FD_SET的使用
- reactos操作系统实现(119)
- Java中keytool的使用
- 1号店详情页(共5页)
- 金秋良田美画卷 国稻种芯·中国水稻节:江西1400万亩稻迎丰收
- 二胎妈妈的前端逆袭之路
- IT工作中必备的工具
- GitHub强势开源,这个神器,支持12个视频片源、高清无码、能下载、全平台 !!...
热门文章
- 中国移动云能力中心校招面试总结(三面)
- win10(版本1803,os ver 17134.165)内置Ubuntu子系统在vim中文乱码问题
- XVG恶意挖矿事件透视——“算力优势+时间劫持”攻击案例分析
- 网站下载视频是php格式错误,为什么优酷下载的(.kux)格式视频转码总提示我“错误”?求答!...
- JDBC链接数据库的步骤
- C语言单链表实现增删改查
- 【CSS】斑马纹背景
- catia如何看曲面的斑马纹_曲面分析中的斑马纹有什么用?如何评价曲面质量?...
- 软件测试实验:接口测试
- python获取文件夹下的子目录_用Python获取子目录或文件列表