备库如何激活

在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. Change Data template dynamically

    1. Attached Property bound to task state. Any change will dynamically set data template.2. Visual St ...

  2. ryu学习笔记(2) 之 ryu-manager运行报错

    http://blog.csdn.net/haimianxiaojie/article/details/48769653 ryu在使用的时候最常出现的报错是:address already in us ...

  3. 【转】Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置:  String strValue ...

  4. 关于阅读JDK源码的准备

    说明:本篇是给自己看的. 笑 最近突然有冲动 想研究下JDK的源码,搜索了一番,基本上推荐从集合开始,精华部分包括:集合.IO.多线程.网络编程. 虚拟机部分先放一放吧,感觉现在不适合我这种半路出家的 ...

  5. 切图psd to html页面制作技术学习与总结

    切图流程和注意事项 切图的正确流程做法是: 1 写框架.拿到psd后,先不要做别的,直接在文本编辑器中将网页的框架写出来,不要假设这块将来css要去怎么渲染,完全自然化的标签,不加任何的css. 2 ...

  6. e613. Modifying the Focus Traversal Order

    JFrame frame = new JFrame(); JButton component1 = new JButton("1"); JButton component2 = n ...

  7. C# 符合备忘录

    ~ 按位求补符:! 非逻辑运算符:% 求余运算符:^ 异或位运算符:& 且位运算符:|  或位运算符:* 既可以用作乘法符号,还可以表示为指针:+ 表示数学运算符相加:= 用来表示赋值操作:\ ...

  8. ZooKeeper系列

    Zookeeper系列(一) ZooKeeper系列(二) ZooKeeper系列(三) ZooKeeper系列(四)

  9. 随手记录一下 Vue 下来框搜索 select2 封装成vue

    引入布局文件 <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css& ...

  10. jq dom不存在时绑定事件

    $( "a.offsite" ).live( "click", function() { alert( "Goodbye!" ); // j ...