一、 简介:

         PG在9.*版本后热备提供了新的一个功能,那就是Stream Replication的读写分离,是PG高可用性的一个典型应用。其中备库是只读库;若主库出现故障;备库这个时候需要占出来替代主库工作。这才是hot standby的高可用性应用。如何替代了;这个就是本次测试的内容。
 

二、系统环境

系统平台:CentOS 6.2

   PostgreSQL版本:9.0.3
   master :   192.168.1.202
   slave  :   192.168.1.201 
 

三、搭建步骤

    环境数据库搭建参考地址http://www.cnblogs.com/lottu/p/5584923.html

四、模拟主库出现故障

    这里只是将主库关闭
    在238库上面执行 pg_stop
 

五、激活备库

   现环境主库238已经停掉了。备库目前只有只读功能。现在将备库激活;只需一步简单操作就行。
   根据其中$PGDATA/recovery.conf内容中trigger_file = '/data/pgdata/trigger_standby'
   创建一个空文件就行。
     touch /data/pgdata/trigger_standby
   过一会儿该文件recovery.conf 会变成 recovery.done;这个时候表明已经激活备库了。此时备库可以读写操作。替代原主库工作。
 

六、激活原主库

   现环境的229库是没有备库;为了提供环境中高可用性;添加备库。若原主库故障解决了。可以替换成备库。
   方案一:像搭建hot standby 一样操作。即参考http://www.cnblogs.com/lottu/p/5584923.html
   方案二:比方案一操作简单多了。在238上面操作;即现备库
   新建 $PGDATA/recovery.conf
recovery_target_timeline = 'latest'
standby_mode = 'on'
primary_conninfo = 'host=192.168.1.201 port=5432 user=repuser password=li0924'
trigger_file = '/data/pgdata/trigger_standby'
其中注意有两点:
      1. 需要修改postgresql.conf文件里面的 hot_standby = on ;

2. 假如其现主库运行一段时间;需要同步日志;可以直接将$PGDATA目录下面的pg_xlog同步过去。

3. 在第2个延伸;在9.5版本提供一个特性-pg_rewind;用它来同步wal日志

pg_rewind --target-pgdata $PGDATA --source-server='host=192.168.1.201 port=5432 user=postgres dbname=mydb'

七、检测切换结果

   在现主库229上面查看进程
[postgres@sdserver40_222 ~]$ ps -ef | grep postgres
postgres  1206 32271  0 11:57 pts/0    00:00:00 ps -ef
postgres  1207 32271  0 11:57 pts/0    00:00:00 grep postgres
root     32269 31857  0 11:09 pts/0    00:00:00 su - postgres
postgres 32271 32269  0 11:09 pts/0    00:00:00 -bash
postgres 32347     1  0 11:11 pts/0    00:00:00 /opt/pgsql/bin/postgres
postgres 32357 32347  0 11:11 ?        00:00:00 postgres: checkpointer process
postgres 32358 32347  0 11:11 ?        00:00:00 postgres: writer process
postgres 32359 32347  0 11:11 ?        00:00:00 postgres: wal writer process
postgres 32360 32347  0 11:11 ?        00:00:00 postgres: autovacuum launcher process
postgres 32361 32347  0 11:11 ?        00:00:00 postgres: archiver process
postgres 32362 32347  0 11:11 ?        00:00:00 postgres: stats collector process
postgres 32371 32347  0 11:11 ?        00:00:00 postgres: wal sender process repuser 192.168.1.202(59806) streaming 0/2F0008B0

查看现备库进程

[postgres@sdserver40_210 pgdata]$ ps -ef | grep postgres
root      1193 31003  0 11:09 pts/1    00:00:00 su - postgres
postgres  1194  1193  0 11:09 pts/1    00:00:00 -bash
postgres  1280     1  0 11:11 pts/1    00:00:00 /opt/pgsql95/bin/postgres
postgres  1287  1280  0 11:11 ?        00:00:00 postgres: startup process   recovering 00000007000000000000002F
postgres  1288  1280  0 11:11 ?        00:00:00 postgres: checkpointer process
postgres  1289  1280  0 11:11 ?        00:00:00 postgres: writer process
postgres  1290  1280  0 11:11 ?        00:00:00 postgres: stats collector process
postgres  1291  1280  0 11:11 ?        00:00:00 postgres: wal receiver process   streaming 0/2F0008B0
postgres  2625  1194  0 11:58 pts/1    00:00:00 ps -ef
postgres  2626  1194  0 11:58 pts/1    00:00:00 grep postgres
[postgres@sdserver40_210 pgdata]$ 

参考地址:

手动切换: http://francs3.blog.163.com/blog/static/405767272011724103133766/

自动切换:德哥:https://github.com/digoal/PostgreSQL_HA_with_primary_standby_2vip

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

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

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

  2. Oracle 12C 物理Standby 主备切换switchover

    Oracle 12C 物理Standby 主备切换switchover Oracle 12C 物理Standby 主备切换switchover Table of Contents 1. 简述 2. 切 ...

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

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

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

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

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

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

  6. Oracle 11.2.0.4.0 Dataguard部署和日常维护(5)-Datauard 主备切换和故障转移篇

    1. dataguard主备切换   1.1. 查看当前主备库是否具备切换条件 on slave select sequence#,first_time,next_time,archived,appl ...

  7. Postgres主备切换

    主备查询 主备不会自动切换(即需要实现线上环境主数据库宕掉之后,从数据库能够自动切换为主数据库,需要借用第三方软件,例如heartbeat等) (1)如何查看是primary还是standby 方法1 ...

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

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

  9. Oracle DataGuard主备切换(switchover)

    Oracle DataGuard主备切换可以使用传统的手动命令切换,也可以使用dgmgr切换,本文记录手动切换. (一)将主库切换为物理备库 STEP1:查看主库状态 SQL> SELECT O ...

随机推荐

  1. mysql语句 索引操作

    创建索引:(help create index;) CREATE INDEX indexName ON tableName(Coll,Coll....); ALTER TABLE tableName ...

  2. python生成器

    eg1: >>> (i*i for i in range(5))<generator object <genexpr> at 0x16b8fa0>>&g ...

  3. 模拟状态为inactive的日志损坏的恢复实验(完全恢复)

    1查看当前日志状态 从这里可以看到我们现在有三组日志,每组日志中只有1个成员.为了演示这个实验,我们为每个组增加1个成员. 2为每组增加组成员 添加后我们验证一下目前各日志成员的状态: 从上面的视图中 ...

  4. shell 常用正则

    shell常用正则表达式   “^\d+$” //非负整数(正整数 + 0)   “^[0-9]*[1-9][0-9]*$” //正整数   “^((-\d+)|(0+))$” //非正整数(负整数 ...

  5. MySQL配置文件改变了datadir值

    从Noinstall Zip Archive中安装MySQL正在从Noinstall软件包安装MySQL的用户可以使用这个说明来手动安装MySQL.从Zip archive 中安装MySQL的 步骤如 ...

  6. python 的重载

    python 的重载主要包括方法重载和运算符重载.1.python 方法重载: 其他的语言一般对于方法重载的话,主要是根据参数的类型不同或者是数量不同来区分同名的方法.而python则比较特殊,它本身 ...

  7. JS控制打印指定div

    <html><head><script language="javascript">function printdiv(printpage){v ...

  8. jquery动态刷新select的值,后台传过来List<T>,前台解析后填充到select的option中

    jquery动态刷新select的值:将后台传来的List<T>赋值到select下的option. 第一个select选择后出发该方法refreshMerchant(params),传递 ...

  9. TI BLE协议栈软件框架分析

    看源代码的时候,一般都是从整个代码的入口处开始,TI  BLE 协议栈源码也不例外.它的入口main()函数就是整个程序的入口,由系统上电时自动调用. 它主要做了以下几件事情: (一)底层硬件初始化配 ...

  10. 《linux内核设计与实现》读书笔记第三章

    第3章 进程管理 3.1 进程 1.进程 进程就是处于执行期的程序. 进程包括: 可执行程序代码 打开的文件 挂起的信号 内核内部数据 处理器状态 一个或多个具有内存映射的内存地址空间 一个或多个执行 ...