Postgresql9.0的一个主要新特性是可以实施流复制,这有点像ORACLE 里的DataGuard(Physial Standby)
但是这种方式比Oracle的DataGuard更为安全,更为高效,因为从库同步主库是实时的,几乎没有时间差。
而Oracle的 DataGuard的从库接收并应用主库的日志的延迟,本人测试了下,大概有几分钟,具体延时决定于
主库的业务繁忙程度。

下面是流复制实验的详细步骤

1 环境信息
PG版本: PostgreSQL 9.0beta3
OS版本: Red Hat Enterprise Linux Server release 5.5
硬件环境: WINDOWS XP上安装两台虚拟机
Master信息 IP: 192.168.1.25
Standby信息 IP: 192.168.1.26

2 由于主库已经安装 Postgresql ,主库上安装PG的步骤这里就不介绍了

3 配置从库主机参数 (On standby)
3.1 设置 /etc/sysctl.conf,增加以下内容
kernel.shmmni = 4096
kernel.sem = 501000 6412800000 501000 12800
fs.file-max = 767246
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 1048576
net.core.wmem_default = 262144
net.core.wmem_max = 262144
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_max_syn_backlog=4096
net.core.netdev_max_backlog=10000
vm.overcommit_memory=0
net.ipv4.ip_conntrack_max=655360

sysctl -p 生效

3.2 设置/etc/security/limits.conf 增加以下内容
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000

3.3 设置 /etc/pam.d/login ,增加以下内容
session required pam_limits.so

4 在主库上创建创建超级用户( On Master )
4.1 创建用户
CREATE USER repuser
SUPERUSER
LOGIN
CONNECTION LIMIT 2
ENCRYPTED PASSWORD 'repuser';

4.2 设置 master 库 pg_hba.conf
host replication repuser 192.168.1.26/16 md5

说明:超级用户 repuser 是用来从库上读取库主库(Master)的 WAL stream,并且在4。2中设置
权限,只允许主机 192.168.1.26(Standby 节点)以 md5 加密方式访问。

5 设置日志参数,记录连接信息 ( Both Master and Standby 库)
log_connections = on

说明:"log_connections" 参数用来记录数据库连接信息,打开这个开关,从而在接下来的CSV日志中
能更好的观察Master库和 Standby 库情况。

6 设置库库 postgresql.conf ( On Master )
max_wal_senders = 1 --WAL STREAM 日志发送进程数
wal_level = hot_standby --主库设置成 hot_standby ,从库才能以READ-ONLY模式打开
archive_mode = on
archive_command = 'cd .'
wal_keep_segments = 64

说明,关键参数"max_wal_senders" 是指 wal 发送进程数, 我这里只有一台从库,所以设置为1,如果有多台
从库,则应该设置成从库个数,因为在Master库上,每台从库需要一个 WAL日志发送进程向从库发送WAL日志流。
一方面是这一参数官网的介绍。

max_wal_senders (integer)
Specifies the maximum number of concurrent connections from standby servers (i.e., the maximum number
of simultaneously running WAL sender processes). The default is zero. This parameter can only be set
at server start. wal_level must be set to archive or hot_standby to allow connections from standby servers.

7 主库全备
7.1 tart the backup (On Master )
select pg_start_backup('base backup for log streaming');

7.2 COPY 数据文件
tar czvf pg_root.tar.gz pg_root --exclude=pg_root/pg_xlog
由于 $PGDATA/pg_xlog 不是必须的,这里排除了这个目录,节省时间。

7.3 将数据文件COPY到standby 主机并解压
scp pgdata.tar.gz pgb:/database

7.4 数据COPY完后,结束备份 Stop the backup (On Master)
select pg_stop_backup(), current_timestamp;

说明:建议主库和从库配置信息一致,包括硬件信息,目录结构,主机配置等。

8 修改从库 postgresql.conf (On standby )
hot_standby = on --从库上可以执行只读操作

9 设置从库 recovery.conf (On standby)
standby_mode = 'on' --标记PG为STANDBY SERVER
primary_conninfo = 'host=192.168.1.25 port=1921 user=repuser password=repuser'
trigger_file = '/tmp/postgresql.trigger.1921'

说明:关键参数“primary_conninfo (string)” ,这里配置了hostname,port,username ,password,
关于这个参数的更多解释可以参考官网.其中更多关于连接的参数可以配置,这里不说明了
http://www.postgresql.org/docs/9.0/static/libpq-connect.html

10 删除从库文件,并创建 pg_xlog目录 (On standby )
$ rm -f $PGDATA/postmaster.pid
$ mkdir -p $PGDATA/pg_xlog

11 启从库,并观察 csvlog
11.1 观察CSVLOG
2011-01-08 17:22:49.757 CST,,,24243,,4d282ce9.5eb3,2,,2011-01-08 17:22:49 CST,,0,LOG,00000,"entering standby mode",,,,,,,,,""
2011-01-08 17:22:49.887 CST,,,24244,,4d282ce9.5eb4,1,,2011-01-08 17:22:49 CST,,0,LOG,00000,"streaming replication successfully connected to primary",,,,,,,,,""
2011-01-08 17:22:52.677 CST,,,24243,,4d282ce9.5eb3,3,,2011-01-08 17:22:49 CST,1/0,0,LOG,00000,"redo starts at 1/94000020",,,,,,,,,""
2011-01-08 17:22:52.696 CST,,,24243,,4d282ce9.5eb3,4,,2011-01-08 17:22:49 CST,1/0,0,LOG,00000,"consistent recovery state reached at 1/98000000",,,,,,,,,""
2011-01-08 17:22:52.805 CST,,,24241,,4d282ce8.5eb1,4,,2011-01-08 17:22:48 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
日志中 "streaming replication successfully connected to primary","database system is ready to accept read only connections"
这些信息说明流复制已经成功,从库正准备接收主库的WAL-STREAM。

11.2 主库观察WAL-Sender 进程
[postgres@pg1 pg_root]$ ps -ef | grep post
postgres 27225 27166 0 17:22 ? 00:00:05 postgres: wal sender process repuser 192.168.1.26(59836) streaming 1/9801E000

说明:将输出结果省略部分,可以看到 " wal sender process repuser"进程

11.3 在从库上观察 WAL-接收进程
[postgres@pgb pg_log]$ ps -ef | grep post
postgres 24244 24241 0 17:22 ? 00:00:04 postgres: wal receiver process streaming 1/9801DF00

说明:同样省略部分输出结果,可以看到“ wal receiver process ” 进程。

12 测试
12.1 主库上创建用户
postgres=# CREATE ROLE browser LOGIN ENCRYPTED PASSWORD 'browser'
postgres-# nosuperuser noinherit nocreatedb nocreaterole CONNECTION LIMIT 200;
CREATE ROLE

从库上验证
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+-----------------------------------+-----------
browser | No inheritance +| {}

说明:果然,在从库上就立刻创建了新用户 'browser'

12.2 主库上创建表空间(On Master)
mkdir -p /database/pgdata/pg_tbs/tbs_browser
从库上也执行 mkdir -p /database/pgdata/pg_tbs/tbs_browser (On Sandby)
postgres=# create tablespace tbs_browser owner skytf LOCATION '/database/pgdata/pg_tbs/tbs_browser';
CREATE TABLESPACE

--在从库上验证
postgres=# \db
List of tablespaces
Name | Owner | Location
-------------+----------+-------------------------------------
pg_default | postgres |
pg_global | postgres |
tbs_browser | skytf | /database/pgdata/pg_tbs/tbs_browser
tbs_mydb | skytf | /database/pgdata/pg_tbs/tbs_mydb

表空间"tbs_browser" 也立刻创建过来了

12.3 主库上创建数据库
postgres=# CREATE DATABASE browser
postgres-# WITH OWNER = skytf
postgres-# TEMPLATE = template0
postgres-# ENCODING = 'UTF8'
postgres-# TABLESPACE = tbs_browser;
CREATE DATABASE

--从库上验证
postgres=# \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-----------+-------+-----------------------
browser | skytf | UTF8 | C | C |
mydb | skytf | UTF8 | C | C |
postgres | postgres | UTF8 | C | C | =Tc/postgres +
| | | | | postgres=CTc/postgres
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres

从库上数据库 "browser" 也立刻有了,几乎没有延时。

12.4 同时观察CSV日志,从日志上看,基本没有延迟
2011-01-08 17:28:59.335 CST,"postgres","postgres",24274,"[local]",4d282e5b.5ed2,2,"authentication",2011-01-08 17:28:59 CST,2/3,0,LOG,00000,"connection authorized: user=postgres database=postgres",,,,,,,,,""

12.5 在从库上建表
mydb=> create table table3(id integer);
ERROR: cannot execute CREATE TABLE in a read-only transaction
说明:从库是以只读形式打开,只能执行读操作,不能写。

13 监控streaming
CREATE OR REPLACE VIEW pg_stat_replication AS
SELECT
S.procpid,
S.usesysid,
U.rolname AS usename,
S.application_name,
S.client_addr,
S.client_port,
S.backend_start
FROM pg_stat_get_activity(NULL) AS S, pg_authid U
WHERE S.usesysid = U.oid AND S.datid = 0;

postgres=# select * from pg_stat_replication ;
procpid | usesysid | usename | application_name | client_addr | client_port | backend_start
---------+----------+---------+------------------+--------------+-------------+-------------------------------
27225 | 64949 | repuser | | 192.168.1.26 | 59836 | 2011-01-08 17:22:05.480584+08
(1 row)

14 总结

以上就是搭建 streaming(又称Hot Standby)的详细过程,这是一个令人兴奋的学习过程,
因为PG的HOT STANDBY 提供的数据及时性和可靠性丝毫不比ORACLE的DataGuard逊色,相反,
本人还觉得比在这方面比Oracle更给力,谢谢开源的人们提供这么优秀的数据库。

--参考文档
http://www.postgresql.org/docs/9.0/static/high-availability.html
PDF电子书: PostgreSQL-Admin-Cookbook.PDF

转载于:https://blog.51cto.com/stoneking/782136

PG: Setting up streaming log replication (Hot Standby )相关推荐

  1. PostgreSQL 9.3 beta2 stream replication primary standby switchover bug?

    [更新] 已有patch. 请参见. PostgreSQL 9.1,9.2,9.3 clean switchover Primary and Standby Patch. http://blog.16 ...

  2. Postgresql逻辑复制报错could not start WAL streaming: ERROR: replication slot “x“is active for PID xxx

    先看日志错误: #发布端报错如下: 2022-04-01 10:18:23.812 CST,"postgres","hank",4666,"10.4. ...

  3. 【DataGuard】ORA-16014 and ORA-00312 Messages in Alert.log of Physical Standby

    安装完成dg后,发现alert 之中的有如下错误 ORA-16014: log 4 sequence# 44 not archived, no available destinations ORA-0 ...

  4. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(九)安装kafka_2.11-1.1.0

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  5. The Log: What every software engineer should know about real-time data's unifying abstraction

    分布式经典文章,先mark,后面慢慢总结一下读书心得. 原文链接:The Log: What every software engineer should know about real-time d ...

  6. MySQL—Replication

    Reference Setting Up Binary Log File Position Based Replication 配置步骤 NOTE Certain steps within the s ...

  7. oracle00313 ora27037,ORA-00313, ORA-00312, ORA-27037 in Standby Database

    ORA-00313, ORA-00312, ORA-27037 in Standby Database [ID 601835.1] 修改时间 20-APR-2009     类型 PROBLEM    ...

  8. Duplicate standby database from active database

    11g Release1 中引入了新的RMAN duplicate命令,即duplicate from active database命令.利用该命令可以更加便捷地创建Data Guard环境,你甚至 ...

  9. Duplicate复制数据库并创建物理StandBy(spfile+不同实例名+不同路径)

    过程和Duplicate复制数据库并创建物理StandBy类似,只是不需要重启数据库. 目的:创建standby,不重启源数据库 1设定环境如下: Primary数据库 IP 172.17.22.16 ...

最新文章

  1. angularjs解决方案之 递归模板
  2. oracle 12c 新特性之不可见字段
  3. VMWare网络设置的3中方式
  4. linux浏览器打开pdf文件,linux下的pdf浏览器(可加批注)
  5. AXD+H-JTAG环境搭建总结
  6. 面试官跟我扯了半小时 CountDownLatch 后,给我发 Offer?| 原力计划
  7. codefroces204A - Little Elephant and Interval 数位DP
  8. python中的thread_深入理解Python中的ThreadLocal变量(上)
  9. GNOME Screencaster 将支持 Miracast P2P 传输
  10. dell 恢复介质_如何使用Dell DataSafe Local Backup创建系统恢复介质
  11. chrony时间同步配置
  12. 网站如何统计访问人数?51LA如何安装使用?
  13. 38.JavaScript中异步与回调的基本概念,以及回调地狱现象
  14. 21岁日本女星惨遭猥亵,只因自拍瞳孔倒影暴露住址? 一张照片是怎么出卖你的?
  15. 循环语句介绍(笔记学习)
  16. Qt OpenGL(08)通过递归细分正二十面体逼近球面
  17. GXNNCTF 2018 We_ax WriteUp 第三届南宁市网络安全技术大赛
  18. 记我的第一次腾讯游戏策划面试
  19. php做网站需要html,杭州做网站:所有新的PHP, CSS和HTML帮助表
  20. 2个超实用Mac软件免费下载地址,从此自力更生

热门文章

  1. 职业高中计算机专业目标,职业高中计算机专业开设的必要性
  2. hadoop是什么_Hadoop精华问答 | hadoop能干什么?
  3. sqlserver中分组按逗号连接
  4. 指定tomcat运行时的JDK版本
  5. stringify用法
  6. spark pineline流水线+聚类评估函数 小结
  7. linux 任务计划 cron
  8. 披一张机器皮,智能就有了?!
  9. 《连线》杂志专访李彦宏:百度all in AI 欲引领智能革命
  10. 面对数据缺失,如何选择合适的机器学习模型?