磨砺技术珠矶,践行数据之道,追求卓越价值
回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页
[作者 高健@博客园  luckyjackgao@gmail.com]

主要参考的是如下url:

http://www.rassoc.com/gregr/weblog/2013/02/16/zero-to-postgresql-streaming-replication-in-10-mins/

准备两台机器,

master: 10.10.10.2

slave:    10.10.10.1

首先在 master机器上,建立一个名为replicator的用户:

psql -c "CREATE USER replicator REPLICATION LOGIN ENCRYPTED PASSWORD 'thepassword';"

master机器上的 postgresql.conf,配置成这样:

listen_address = # make sure we're listening as appropriate
wal_level = hot_standby
max_wal_senders = 3
checkpoint_segments = 8
wal_keep_segments = 8 

然后在master的 pg_hba.conf文件,中,进行如下配置,添加一行,允许replicator用户从远端访问:

host  replication     replicator      10.10.10.1/32              md5

然后,启动master端的postgresql

然后,在slave端:

在slave端的postgresql停止的前提下,以postgres用户身份,删除data目录:

 rm -rf /usr/local/pgsql/data

然后,在slave端,执行pg_basebackup程序:

pg_basebackup -h 10.10.10.2 -D /usr/local/pgsql/data -U replicator -v -P

在执行完毕 pg_basebackup后,会得到一个从 master端拷贝到的/usr/local/pgsql/data目录,

编辑其中的 postgresql.conf,把其standby_mode设置为on。

在slave端,编辑一个/usr/local/pgsql/data/recovery.conf文件,

内容如下:

  standby_mode = 'on'primary_conninfo = 'host=10.10.10.2 port=5432 user=replicator password=thepassword sslmode=require'trigger_file = '/tmp/postgresql.trigger'

然后,在slave端,启动postgresql:

[postgres@pg200 pgsql]$ ./bin/pg_ctl -D ./data start
pg_ctl: another server might be running; trying to start server anyway
server starting
[postgres@pg200 pgsql]$ LOG:  database system was interrupted while in recovery at log time 2013-09-27 17:28:27 CST
HINT:  If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target.
LOG:  entering standby mode
LOG:  consistent recovery state reached at 0/5012F78
LOG:  redo starts at 0/5012EE0
LOG:  record with zero length at 0/5012F78
LOG:  database system is ready to accept read only connections
LOG:  streaming replication successfully connected to primary

从log中,可以看到,postgresql的 streaming repliation开始工作了。

下面进行简单的验证:

master端,新增数据:

[postgres@pg200 ~]$ cd /usr/local/pgsql/
[postgres@pg200 pgsql]$ ./bin/psql
psql (9.2.4)
Type "help" for help.postgres=# \lList of databasesName    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgrestemplate1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +|          |          |             |             | postgres=CTc/postgres
(3 rows)postgres=# \dList of relationsSchema | Name | Type  |  Owner
--------+------+-------+----------public | test | table | postgres
(1 row)postgres=# select * from test;id
----123
(3 rows)postgres=# insert into test values(4);
INSERT 0 1
postgres=# 

slave端,可以看到数据:

[postgres@pg200 ~]$ cd /usr/local/pgsql/bin
[postgres@pg200 bin]$ ./psql
psql (9.2.4)
Type "help" for help.postgres=# select * from test;id
----1234
(4 rows)postgres=# 

关于pg_basebackup,其官方文档说明如下:

http://www.postgresql.org/docs/current/static/app-pgbasebackup.html

pg_basebackup is used to take base backups of a running PostgreSQL database cluster. These are taken without affecting other clients to the database, and can be used both for point-in-time recovery (see Section 24.3) and as the starting point for a log shipping or streaming replication standby servers (see Section 25.2).pg_basebackup makes a binary copy of the database cluster files, while making sure the system is automatically put in and out of backup mode automatically. Backups are always taken of the entire database cluster, it is not possible to back up individual databases or database objects. For individual database backups, a tool such as pg_dump must be used.The backup is made over a regular PostgreSQL connection, and uses the replication protocol. The connection must be made with a superuser or a user having REPLICATION permissions (see Section 20.2), and pg_hba.conf must explicitly permit the replication connection. The server must also be configured with max_wal_senders set high enough to leave at least one session available for the backup.

但是,实际上有一个问题是需要引起注意的,上述的streaming replication,并没有使用到archive_log模式,这个也不是必须的。

可是如果master很繁忙,比如像这样:

create table test01(id integer, val char(1024));
insert into test01 values(generate_series(1,1228800),repeat( chr(int4(random()*26)+65),1024));

此时,master端的online wal log,不断地快速产生,有的会随着新的wal log的生成而被删除掉。

此时,就会出现如下错误:

[postgres@pg200 ~]$ cd /usr/local/pgsql
[postgres@pg200 pgsql]$ ./bin/pg_ctl -D ./data start
server starting
[postgres@pg200 pgsql]$ LOG:  database system was shut down in recovery at 2013-09-30 14:51:27 CST
LOG:  entering standby mode
LOG:  consistent recovery state reached at 0/5013A48
LOG:  redo starts at 0/50139B0
LOG:  record with zero length at 0/5013A48
LOG:  database system is ready to accept read only connections
LOG:  streaming replication successfully connected to primary
FATAL:  could not receive data from WAL stream: FATAL:  requested WAL segment 000000010000000000000011 has already been removedLOG:  invalid magic number 0000 in log file 0, segment 17, offset 14467072
LOG:  streaming replication successfully connected to primary
FATAL:  could not receive data from WAL stream: FATAL:  requested WAL segment 000000010000000000000011 has already been removed

从这个意义上说,使用 archive log是必须的。

[作者 高健@博客园  luckyjackgao@gmail.com]
回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页
磨砺技术珠矶,践行数据之道,追求卓越价值

转载于:https://www.cnblogs.com/gaojian/p/3347203.html

PostgreSQL的streaming replication相关推荐

  1. pgpool 之四 2 pgpool + 2 postgresql 的 stream replication 模式

    os: ubuntu 16.04 db: postgresql 9.6.8 pgpool: pgpool-II-3.7.7 pgpool 简介 准确的名字是Pgpool-II,这里简称为pgpool. ...

  2. PostgreSQL源码分析

    PostgreSQL源码结构 PostgreSQL的使用形态 PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信 ...

  3. postgresql 客户端_Postgresql体系结构

    Potgres(常驻进程) 管理后端的常驻进程,也称为'postmaster'.其默认监听UNIX Domain Socket和TCP/IP(Windows等,一部分的平台只监听tcp/ip)的543 ...

  4. PostgreSQL体系架构

    PostgreSQL 使用客户机/服务器(C/S)的模式提供服务,一个PostgreSQL会话由下列相关的进程(程序)组成: (1)一个服务器端进程.该进程管理数据库文件,接受客户端与数据库的连接,且 ...

  5. 《A Tour of PostgreSQL Internals》学习笔记——进程间通信

    中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...

  6. java ipc pgsql_[转]PostgreSQL源码结构

    PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信. 另外,还有一种'Standalone Backend'使用 ...

  7. PostgreSQL源码结构

    PostgreSQL的使用形态 PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信. 另外,还有一种'Stand ...

  8. Citus高可用方案演进介绍

    点击上方"数据和云" 关注我们,享更多干货! 1. Citus的复制功能 Citus支持两种数据复制方案: Citus的Shard Replication:通过将DML语句复制到多 ...

  9. 【postgres】源码结构

    文章目录 PostgreSQL的使用形态 PostgreSQL的结构 Potgres(常驻进程) Postgres(子进程) 其他的进程 Writer process WAL writer proce ...

最新文章

  1. vim查找/替换字符串
  2. 模块化开发seajs 配置和简单调用
  3. [css] 用css实现倒影的效果
  4. JavaScript高级之构造函数和原型
  5. 2019年程序员薪酬报告:平均年薪超70万!40岁后,这类人不“保值”了
  6. php display block,CSS display (block inline none )常见属性和用法教程
  7. 多出20倍?Android 收集用户数据量远超 iPhone
  8. 安利10个让你爽到爆的IDEA必备插件,终获offer
  9. Requests上传文件
  10. 2021-06-27面向对象继承
  11. 读EntityFramework.DynamicFilters源码_心得_示例演示02
  12. c51语言自定义头文件,C51语言头文件包括的内容有
  13. android sqlite英文文献,SQLite数据库外文文献翻译.doc
  14. VON矿链资本的技术含量内幕是什么?尊皇社区为你揭秘!
  15. 在现有Fabric 2.2.0 网络上设置和运行Caliper性能测试 实战
  16. 微信朋友圈得内容可以爬取吗?看完这篇你心里就有底了
  17. 用电脑远程给海信智能电视安装软件的办法
  18. 最好用的coreldraw2020中文版,安装好后不显示评估版,显示已注册
  19. HIT计算机系统大作业-程序人生-Hello’s P2P
  20. kafka教程之linux安装教程(一)

热门文章

  1. 企业短信平台 php,梅州PHP短信平台企业,PHP短信平台
  2. 明星热图|朱一龙环保主题大片出炉;李现为您开启新一年“红运”时刻;杨采钰成林清轩产品代言人...
  3. 华为数通笔记-PPPoE
  4. yolo 算法中的IOU算法程序与原理解读
  5. 查询电脑关机/重启记录
  6. vue中父传子,父传孙说明
  7. 全志A40i开发板(4核ARM CortexA7)测评合集——存储介质读写测试
  8. js数组的多条件筛选
  9. 棋牌类游戏的开发心得
  10. 阿里技术风险与效能部负责人张瓅玶:从底层资源到核心竞争力 阿里巴巴的深度用云实践