备库如何激活

在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. Matlab基本用法

    转至:http://blog.sina.com.cn/s/blog_8354dda801012dyn.html 目录: 一.说明 二.数据类型及基本输入输出 三.流程控制 四.循环 五.数组.数组运算 ...

  2. css关于定位那些事情

    css绝对定位.相对定位和文档流的那些事 前言 接触html.和css时间也不短了,但每次用div+css布局的时候心里还是有点儿虚,有时候干脆就直接用table算了,很多时候用div会出现些不可预料 ...

  3. How to revert your file&folder by "FOUND.000"

    当你在硬盘分区间复制很多文件时,当你使用下载软件时,当你用Word写作时,如果忽然遇见停电.Windows失去响应或者系统自动重新启动,在看着屏幕一黑的瞬间你会有何感受?只能希望在重新启动以后重要的文 ...

  4. 【1】JVM-内存模型

    本篇其实就是一个读书笔记,书是<深入理解JAVA虚拟机>,在网上搜索JAVA内存,说的比较好的其实很多都源自这本书,作为一个JAVA程序员,理解虚拟机是通向高级程序员的必经道路.本篇中的图 ...

  5. (笔记)Mysql命令delete from:删除记录

    delete from命令用于删除表中的数据. delete from命令格式:delete from 表名 where 表达式 例如,删除表 MyClass中编号为1 的记录:    mysql&g ...

  6. 锐捷 Fat/Fit Ap切换

    工作中要使用锐捷的AP和AC进行组网.记录一下RG-AP220-E配置成瘦AP的方法. 使用console口连接,baudrate rate: 9600, 8n1 瘦AP:console密码是ruij ...

  7. Spring Boot 启用Gzip压缩

    有两点需要注意 1.需要在application.properties里启用压缩,并设置压缩支持的格式(默认支持text/html等,但不支持application/json) server.comp ...

  8. Linux-HA实战(3)— 基于Pacemaker搭建TFS Nameserver HA

    上篇文章简单介绍了下基于Heartbeat的虚拟IP配置,个人觉得简单易用已经能够应付大部分场景了.但是既然花时间研究HA,如果仅限于一个虚拟IP飘来飘去未免有点糊弄任务了,因此这篇文章打算介绍下基于 ...

  9. e606. Determining Which Component or Window Has the Focus

    // null is returned if none of the components in this application has the focus Component compFocusO ...

  10. Python——eventlet.websocket

    使用该模块可以方便地创建 websocket 服务器,要创建一个websocket服务器,只需要将一个句柄函数用装饰器 WebSocketWSGI 装饰即可,然后这个函数就可以当做一个WSGI应用: ...