运行环境:

Primary: 192.168.0.11

Standby: 192.168.0.21, 192.168.0.22

OS: CentOS 6.2

PostgreSQL: 9.1.2 版本以上才支持Streaming Replication。

1. 环境规划

   Primary和Standby节点最好有相同的环境。

2. 安装PostgreSQL

1)在Primary和Standy节点上安装PostgreSQL软件,安装路径为/opt/pgsql

2)设置postgres用户的环境变量

     PGHOME=/opt/pgsql

     PGDATA=/home/postgres/main

     PATH=$PG_HOME/bin:$PATH:$HOME/bin

3. Primary节点

1)切换到postgres用户

    $su - postgres

2)初始化数据库

    $initdb

3)配置pg_hba.conf

     在# IPv4 local connections下面添加一行,设置PostgreSQL的访问及其权限

     host    all             all             192.168.0.1/24        trust

     在# replication privilege.下面添加一行,设置replication用户及权限

     host     replication     postgres        192.168.0.1/24       trust

4)配置postgresql.conf

     配置监听,修改listen_addresses = 'localhost'

     listen_addresses = '*'     # what IP address(es) to listen on;

     配置Primary Replication参数

     wal_level = hot_standby

     max_wal_senders = 8

     wal_keep_segments = 32

     archive_mode = on

     archive_command = 'cp %p /home/postgres/archive/%f < /dev/null'

     "/home/postgres/archive"是Replication的archive的存储路径。PostgreSQL会将Replication的WAL保存在 "/home/postgres/archive"路径下。

5) 启动Primary上的PostgreSQL数据库

    $pg_ctl start

6) 在primary上执行以下命令

    $psql -c "SELECT pg_start_backup('label', true);"

    将Primary的PGDATA目录下的文件,除了postmaster.pid复制到Standby节点的“/home/postgres/main”目录下,该目录是 Standby节点上的PostgreSQL数据库的PGDATA目录。

    $rsync -a ${PGDATA}/ postgres@192.168.0.21:/home/postgres/main --exclude postmaster.pid

    $psql -c "SELECT pg_stop_backup();"

    192.168.0.21的/home/postgres/main目录下的内容为

4. Standby节点

    PGDATA=/home/postgres/main

    Standby节点的PGDATA路径就是Primary节点的PGDATA的副本

1)配置postgresql.conf

     设置hot_standby为

     hot_standby= on

2)编辑recovery.conf,文件路径为$(PGDATA)/recovery.conf,内容为

---------------------------------------------------------------------------------------------------------------------------

# Specifies whether to start the server as a standby. In streaming replication,

# this parameter must to be set to on.
standby_mode  = 'on'

# Specifies a connection string which is used for the standby server to connect
# with the primary.
primary_conninfo = 'host=192.168.0.11 port=5432 user=postgres'
# Specifies a trigger file whose presence should cause streaming replication to
# end (i.e., failover).
trigger_file = '/home/postgres/trigger'

# Specifies a command to load archive segments from the WAL archive. If
# wal_keep_segments is a high enough number to retain the WAL segments
# required for the standby server, this may not be necessary. But
# a large workload can cause segments to be recycled before the standby
# is fully synchronized, requiring you to start again from a new base backup.
restore_command = 'cp /home/postgres/archive/%f %p'

--------------------------------------------------------------------------------



3)复制pg_xlog下的所有文件到/home/postgres/archive目录下
4)启动Standby节点,完成Replication。
完成Streaming Replication配置。

5. 验证

使用psql连接primary,创建test数据库。刷新从节点,可以看到在其上也生成了test数据库。

6. 问题

1)Standby的data目录是空的,其所有配置均是在rsync之后;

2)recovery.conf文件其实只需要修改standby_mode 和 primary_info,其余并不是必须的;

3)注意Primary的防火墙,必要时关闭,免得干扰集群配置;

4)如果从节点PostgreSQL已有数据,需要全部删除。

参考:

PostgreSQL配置Streaming Replication集群(http://www.cnblogs.com/marsprj/archive/2013/03/04/2943373.html)

http://opensourcedbms.com/dbms/setup-replication-with-postgres-9-2-on-centos-6redhat-el6fedora/

配置PostgreSQL Streaming Replication集群的更多相关文章

  1. postgresql Streaming Replication监控与注意事项

    一监控Streaming Replication集群 1 pg_stat_replication视图(主库端执行) pid Wal sender process的进程ID usesysid 执行流复制 ...

  2. 【Quartz】配置最简单的集群

    在许多情况,我们希望我们的定时任务是可靠的,不会因系统故障.机器宕机而导致某一笔定时任务不能按时运行.这种情况下,我们就需要为Quartz做个集群. 最简单的情况,有两台机器或两个应用,同时维护一批定 ...

  3. 配置drbd高可用集群

    前期准备: 同步时间 (两个节点) 节点一(172.16.21.6) [root@stu21 heartbeat2]# ntpdate 172.16.0.1 31 Dec 20:59:25 ntpda ...

  4. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  5. 大数据项目之_15_帮助文档_NTP 配置时间服务器+Linux 集群服务群起脚本+CentOS6.8 升级到 python 到 2.7

    一.NTP 配置时间服务器1.1.检查当前系统时区1.2.同步时间1.3.检查软件包1.4.修改 ntp 配置文件1.5.重启 ntp 服务1.6.设置定时同步任务二.Linux 集群服务群起脚本2. ...

  6. 在 Linux 多节点安装配置 Apache Zookeeper 分布式集群

    规划: 三台物理服务器就形成了(法定人数).对于高可用性集群,您可以使用高于3的任何奇数.例如,如果设置5台服务器,则集群可以处理两个故障节点等. 物理服务器需要开启的端口 2888 , 3888 和 ...

  7. dubbo配置负载均衡、集群环境

    再用dubbo作为项目架构的时候,给consumer消费者用nginx提供了负载均衡策略和集群的实现, 但是想了下,consumer再多,但是提供者还是一个,最后还不都是落到了这一个provider上 ...

  8. db2 数据库配置HADR+TSA添加集群节点

    Db2配置HADR高可用+TSA添加集群节点 一.服务器资源 Master IP:10.78.10.1 数据库:dbclassSlave IP:10.78.10.2 数据库:dbclassVIP:10 ...

  9. kubernetes配置(kubeconfig)对多集群的访问

    配置对多集群的访问 本文展示如何使用配置文件来配置对多个集群的访问. 在将集群.用户和上下文定义在一个或多个配置文件中之后,用户可以使用 kubectl config use-context 命令快速 ...

随机推荐

  1. angularJs项目实战!02:前端的页面分解与组装

    自从上一篇文章到现在已经有将近一个月的时间,我将精力放在了前端页面分解与组装,和angularjs如何与jquery.bootstrap.D3等一系列其他类库结合使用的经验总结上.由于公司新招了一些员 ...

  2. JavaWeb学习总结

    http://www.cnblogs.com/xdp-gacl/tag/JavaWeb%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ http://www.cnblogs. ...

  3. MYSQL 5.7 主从复制 -----GTID说明与限制 原创

    http://www.cnblogs.com/cenalulu/p/4309009.html http://txcdb.org/2016/03/%E7%B3%BB%E7%BB%9F%E8%A1%A8- ...

  4. UNIX/Linux网络编程基础:应用层协议简介

    目录 1.HTTP协议 2.FTP协议 3.TELNET协议 4.NFS协议 1.HTTP协议 应用层协议HTTP协议是Web的核心.HTTP协议在Web的客户程序和服务器程序中得以实现,运行在不同系 ...

  5. [转]DllMain中不当操作导致死锁问题的分析——DllMain中要谨慎写代码(完结篇)

    在CSDN中发现这篇文章,讲解的比较详细,所以在这里备份一个.原文链接:http://blog.csdn.net/breaksoftware/article/details/8167641 DllMa ...

  6. 【AsyncTask整理 1】 AsyncTask几点要注意的地方

    问题1:AsyncTask是多线程吗? 答:是. 问题2:AsyncTask与Handler相比,谁更轻量级? 答:通过看源码,发现AsyncTask实际上就是一个线程池,而网上的说法是AsyncTa ...

  7. leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  8. PreparedStatement和Statement的区别

    转自:http://blog.sina.com.cn/s/blog_77eba18f01019csh.html 1. PreparedStatement接口继承Statement, PreparedS ...

  9. JavaScript中的window.close在FireFox和Chrome上不能正常动作的解决方法

    JS中关闭窗口的方法window.close()在IE上能够正常动作,而在FireFox和Chrome上无法动作. (当时,在Chrome35.0上的时候还是可以的,Chrome36.0上就无法动作了 ...

  10. NDK编译FFMpeg[Linux]

    最近在研究视频直播相关的技术,了解到了FFmpeg,就在网上查看如何将FFmpeg移植到Android中,查了几天,看的东西不少,就是没有一个可以完全移植成功的,最后通过产看各种资料,结合网上的资料, ...