PostgreSQL主备切换
备库如何激活
在PostgreSQL(HOT-Standby)如主库出现异常。备库如何激活;来替换主库工作。有下列2种方式
- 备库在recovery.conf文件中有个配置项trigger_file。它是激活standby的触发文件。当它存在;就会激活standby。
- 使用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主备切换的更多相关文章
- 在Azure云上实现postgres主备切换
以下是工作上实现postgres主备切换功能所用到的代码和步骤,中间走了不少弯路,在此记录下.所用到的操作系统为centos 7.5,安装了两台服务器,hostname为VM7的为Master,VM8 ...
- KingbaseES R6 集群主机锁冲突导致的主备切换案例
案例说明: 主库在业务高峰期间,客户执行建表等DDL操作,主库产生"AccessExclusiveLock "锁,导致大量的事务产生锁冲突,大量的会话堆积,客户端session ...
- Spark系列(五)Master主备切换机制
Spark Master主备切换主要有两种机制,之中是基于文件系统,一种是基于Zookeeper.基于文件系统的主备切换机制需要在Active Master挂掉后手动切换到Standby Master ...
- Nginx+Keepalived主备切换(包含nginx服务停止)
原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4 ...
- 在Windows Azure上配置VM主备切换(1)——Linux篇
对任何一个上线系统来说,高可用设计是不可或缺的一个环节,这样才可以确保应用可以持续.稳定的运行,而不是频繁的掉线.停机.高可用设计的核心思路很简单,就是消除一切单点故障,将单点链路或者节点升级为多点. ...
- (摘)DataGuard物理standby管理 - 主备切换
DataGuard物理standby管理 - 主备切换 Dataguard的切换分为两种,switchover和failover. switchover一般用于数据库或硬件升级,这时只需要较短时间中断 ...
- 测试redis+keepalived实现简单的主备切换【转载】
转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...
- Redis安装,主从,主备切换
网络环境: 主:10.187.120.5 从:10.187.69.58 从:10.187.69.59 一.安装 mv redis-2.8.19.tar.gz /export/servers/ cd / ...
- MySQL 复制 - 性能与扩展性的基石 4:主备切换
一旦使用 MySQL 的复制功能,就很大可能会碰到主备切换的情况.也许是为了迭代升级服务器,或者是主库出现问题时,将一台备库转换成主库,或者只是希望重新分配容量.不过出于什么原因,都需要将新主库的信息 ...
随机推荐
- 为什么Java匿名内部类访问的外部局部变量或参数需要被final修饰
大部分时候,类被定义成一个独立的程序单元.在某些情况下,也会把一个类放在另一个类的内部定义,这个定义在其他类内部的类就被称为内部类,包含内部类的类也被称为外部类. class Outer { priv ...
- JavaScript prototype背后的工作原理
首先从一个函数说起 function fn1(name, age) { this.name = name; this.age = age; this.say = function() { alert( ...
- 关于Unity中物理检测的准备
1.要确定每个物体的碰撞类型,是有碰撞效果的碰撞还是没有碰撞效果的碰撞(is trigger),带不带刚体. 2.给每个物体分层,再设置哪些层会发生碰撞,哪些完全不产生碰撞. 3.给每个物体设置标记, ...
- hash模块 hashlib不可逆加密 和 base64算法加密解密
hashlib模块 用于加密相关的操作,代替md5模块和sha模块,主要提供SHA1,SHA224,SSHA256,SHA384,SHA512,MD5算法 直接看代码案例: ---------md5- ...
- pandas汇总和计算描述统计
pandas 对象拥有一组常用的数学和统计方法. 他们大部分都属于简约和汇总统计, 用于从Series中提取单个值(如sum或mean) 或从DataFrame的行或列中提取一个Series.跟对应的 ...
- WEB打印大全
1.控制"纵打". 横打”和“页面的边距. (1)<script defer> function SetPrintSettings() { // -- advance ...
- Json---Linux下使用Jsoncpp
一.安装 scons 下载地址:http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download ...
- e816. 创建工具栏
A toolbar can be either horizontal or vertical. When the orientation is horizontal, the objects in t ...
- Sublime text 3 中Package Control 的安装与使用方法
Package Control插件本身是一个为了方便管理插件的插件,在Sublime text 3中,Package Control 的安装方法一开始出来的方法是要先安装Git, 再输入代码来安装,原 ...
- 关于SpringMVC的文件上传
关于文件的上传,之前写过2篇文章,基于Struts2框架,下面给出文章链接: <关于Struts2的文件上传>:http://www.cnblogs.com/lichenwei/p/392 ...