备库如何激活

在PostgreSQL(HOT-Standby)如主库出现异常。备库如何激活;来替换主库工作。有下列2种方式

  1. 备库在recovery.conf文件中有个配置项trigger_file。它是激活standby的触发文件。当它存在;就会激活standby。
  2. 使用pg_ctl promote来激活。

演示场景

模拟在主库异常挂掉。备库来替换主库工作一段时间。并原主库切换成新备库并恢复操作。
环境
主机名 IP地址 角色 数据目录
postgres202 192.168.1.202 primary /home/postgres/data
postgres203 192.168.1.203 standby /home/postgres/data postgres=# select * from pg_stat_replication ;
-[ RECORD ]----+------------------------------
pid |
usesysid |
usename | replica
application_name | walreceiver
client_addr | 192.168.1.203
client_hostname |
client_port |
backend_start | -- ::26.249452+
backend_xmin |
state | streaming
sent_location | /B0000D0
write_location | /B0000D0
flush_location | /B0000D0
replay_location | /B0000D0
sync_priority |
sync_state | async 2.1 模拟主库关机
[postgres@postgres202 ~]$ pg_stop
waiting for server to shut down........ done
server stopped 2.2 激活备库;
模拟新主库工作一段时间。操作删除T2表和新建T3表
[postgres@postgres203 data]$ pg_ctl promote
server promoting [postgres@postgres203 ~]$ psql lottu lottu
psql (9.6.)
Type "help" for help. lottu=> \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
lottu | t | table | lottu
lottu | t2 | table | lottu
( rows) lottu=> drop table t2;
DROP TABLE lottu=> create table t3 as select * from t;
SELECT #切换日志;进行归档。
[postgres@postgres203 ~]$ psql
psql (9.6.)
Type "help" for help. postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
/C018FC0
( row) postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
/D000078
( row) postgres=# select pg_switch_xlog();
pg_switch_xlog
----------------
/E000000
( row) 2.3 恢复主库
. 用pg_rewind命令同步新备库。
[postgres@postgres203 ~]$ pg_rewind -?
pg_rewind resynchronizes a PostgreSQL cluster with another copy of the cluster. Usage:
pg_rewind [OPTION]... Options:
-D, --target-pgdata=DIRECTORY existing data directory to modify
--source-pgdata=DIRECTORY source data directory to synchronize with
--source-server=CONNSTR source server to synchronize with
-n, --dry-run stop before modifying anything
-P, --progress write progress messages
--debug write a lot of debug messages
-V, --version output version information, then exit
-?, --help show this help, then exit Report bugs to <pgsql-bugs@postgresql.org>. [postgres@postgres202 ~]$ pg_rewind --target-pgdata $PGDATA --source-server='host=192.168.1.203 port=5432 user=postgres dbname=postgres' -P
connected to server
servers diverged at WAL position /C000098 on timeline
rewinding from last common checkpoint at /C000028 on timeline
reading source file list
reading target file list
reading WAL in target
need to copy MB (total source directory size is MB)
/ kB (%) copied
creating backup label and updating control file
syncing target data directory
Done! [postgres@postgres202 data]$ mv recovery.done recovery.conf
[postgres@postgres202 data]$ vi recovery.conf
standby_mode = on # 这个说明这台机器为从库
primary_conninfo = 'host=192.168.1.203 port=5432 user=replica password=replica' # 这个说明这台机器对应主库的信息
recovery_target_timeline = 'latest' # 这个说明这个流复制同步到最新的数据 由于postgresql.conf是同步过来的;所有不做修改。也有几个地方要进行修改
max_connections = # 一般查多于写的应用从库的最大连接数要比较大
hot_standby = on # 说明这台机器不仅仅是用于数据归档,也用于数据查询
max_standby_streaming_delay = 30s # 数据流备份的最大延迟时间
wal_receiver_status_interval = 10s # 多久向主报告一次从的状态,当然从每次数据复制都会向主报告状态,这里只是设置最长的间隔时间
hot_standby_feedback = on # 如果有错误的数据复制,是否向主进行反馈 【验证】
[postgres@postgres202 data]$ pg_start
server starting
[postgres@postgres202 data]$ psql lottu lottu
psql (9.6.)
Type "help" for help. lottu=> \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
lottu | t | table | lottu
lottu | t3 | table | lottu
( rows) [postgres@postgres203 data]$ psql
psql (9.6.)
Type "help" for help. postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD ]----+------------------------------
pid |
usesysid |
usename | replica
application_name | walreceiver
client_addr | 192.168.1.202
client_hostname |
client_port |
backend_start | -- ::08.390662+
backend_xmin |
state | streaming
sent_location | /F01EEA8
write_location | /F01EEA8
flush_location | /F01EEA8
replay_location | /F01EEA8
sync_priority |
sync_state | async

参考文献:http://www.cnblogs.com/songyuejie/p/4976171.html

PostgreSQL主备切换的更多相关文章

  1. 在Azure云上实现postgres主备切换

    以下是工作上实现postgres主备切换功能所用到的代码和步骤,中间走了不少弯路,在此记录下.所用到的操作系统为centos 7.5,安装了两台服务器,hostname为VM7的为Master,VM8 ...

  2. KingbaseES R6 集群主机锁冲突导致的主备切换案例

    ​ 案例说明: 主库在业务高峰期间,客户执行建表等DDL操作,主库产生"AccessExclusiveLock "锁,导致大量的事务产生锁冲突,大量的会话堆积,客户端session ...

  3. Spark系列(五)Master主备切换机制

    Spark Master主备切换主要有两种机制,之中是基于文件系统,一种是基于Zookeeper.基于文件系统的主备切换机制需要在Active Master挂掉后手动切换到Standby Master ...

  4. Nginx+Keepalived主备切换(包含nginx服务停止)

    原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4 ...

  5. 在Windows Azure上配置VM主备切换(1)——Linux篇

    对任何一个上线系统来说,高可用设计是不可或缺的一个环节,这样才可以确保应用可以持续.稳定的运行,而不是频繁的掉线.停机.高可用设计的核心思路很简单,就是消除一切单点故障,将单点链路或者节点升级为多点. ...

  6. (摘)DataGuard物理standby管理 - 主备切换

    DataGuard物理standby管理 - 主备切换 Dataguard的切换分为两种,switchover和failover. switchover一般用于数据库或硬件升级,这时只需要较短时间中断 ...

  7. 测试redis+keepalived实现简单的主备切换【转载】

    转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...

  8. Redis安装,主从,主备切换

    网络环境: 主:10.187.120.5 从:10.187.69.58 从:10.187.69.59 一.安装 mv redis-2.8.19.tar.gz /export/servers/ cd / ...

  9. MySQL 复制 - 性能与扩展性的基石 4:主备切换

    一旦使用 MySQL 的复制功能,就很大可能会碰到主备切换的情况.也许是为了迭代升级服务器,或者是主库出现问题时,将一台备库转换成主库,或者只是希望重新分配容量.不过出于什么原因,都需要将新主库的信息 ...

随机推荐

  1. 【android】环形进度条实现

    先上效果图(压缩尺寸后出现锯齿,原图边缘很细腻的喂~) 特性: 1:支持环形带字 .环形不带字(中间盖上圆形图片,实现天天动听播放器在通知栏播放进度的效果).实心 2:线程安全,不需要写handler ...

  2. Mac 系统上安装Protocol buffer

    1. cd /Software/protobuf-2.5.0 2.sudo ./configure --prefix=$/Software/protobuf-2.5.0 3.sudo make 4.s ...

  3. SpagoBI 教程 Lesson 2: OLAP with JPIVOT

    SpagoBI Lesson 2: OLAP with JPIVOT Online Analytical Processing Online Analytical Processing (OLAP) ...

  4. MFC绘图小实验(2)

    1,以正五边形的5个顶点为基础,隔点存储构成五角星.填充模式采用WINDING.五角星边界线为5个像素宽的蓝色实线,内部使用红色填充. CRect rect; //定义矩形 GetClientRect ...

  5. TCP/IP,http,socket,长连接,短连接——小结(转)

    概要: 之前对这几个概念有点糊涂,查阅了些资料,稍微概括下他们的区别吧.如有错误,请拍~~~ 先看图: TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.    在 ...

  6. 创建Swing的步骤

    (1)导入Swing包 (2)选择界面风格 (3)设置顶层容器 (4)设置按钮和标签 (5)将组件放到容器上 (6)为组件增加边框 (7)处理事件 (8)辅助技术支持 package Com.MySw ...

  7. 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)

    错误: 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_A ...

  8. Visual Studio Code自动识别编码

    将设置中的"files.autoGuessEncoding"项的值改为true即可. 详情请转知乎:https://www.zhihu.com/question/34415763

  9. linux vi命令详解2

    刚开始学着用linux,对vi命令不是很熟,在网上转接了一篇. vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指 ...

  10. C# IP地址与数字之间的互转

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Te ...