备库如何激活

在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. 【转】Grafana系列教程–Grafana基本概念

    在上面几篇文章中,我们介绍了Grafana的安装配置以及运行的方法,本篇文章我们就来介绍下Grafana的基本概念. 一.Data Source — 数据源 Grafana支持多种不同的时序数据库数据 ...

  2. 关于Unity中的光照(五)

    Mobile Diffuse Unity自带的一种shader,用的比较多,性能还可以.我们默认创建的unit shader基本和它一致,但是没有参与光照计算,看起来和Mobile Diffuse有区 ...

  3. android地址位置

    有精度纬度确定地理位置 latitude: 23.1352888---longitude: 113.3297445 调用Google API接口: URL: http://maps.google.cn ...

  4. IDEA MAVEN Failed to create a Maven project 'C:/gitProjects/mayProj/pom.xml' already exists in VFS

    When adding the module to an existing module that already has a POM, it is necessary to manually spe ...

  5. Unity导入3D模型的过程与方法

    一.介绍 资源是游戏开发中的原材料,也就是组成游戏的模块. Unity只是一个游戏开发引擎,而并不是一个资源开发软件.这就意味着在游戏中需要的资源通常是由一些设计者使用其他软件开发出来的,然后设计者会 ...

  6. 用不上索引的SQL语句

    下面介绍六种建立索引后不起作用的sql语句. 1.使用不等于操作符(<>, !=) SELECT * FROM dept WHERE staff_num <> 1000; × ...

  7. 获取Java正在执行的方法

    new Object(){}.getClass().getEnclosingMethod().getName(); Thread.currentThread().getStackTrace()

  8. C++编程经验-返回局部变量的讨论(转)

    返回局部变量没问题 如果返回局部变量有问题,函数的意义还有吗? 全局变量还用返回吗?  返回指向局部变量的指针才有问题, 函数退栈之后,局部变量消失, 指针将指向未知区域,所以出现问题.   返回局部 ...

  9. 【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式

    boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #includ ...

  10. Redis集群的操作

    1.原始集群 1.1    启动Redis集群 [root@cent03 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/ ...