PostgreSQL的streaming replication
磨砺技术珠矶,践行数据之道,追求卓越价值
回到上一级页面: 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 -- :: 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 /5012F78
LOG: redo starts at /5012EE0
LOG: record with zero length at /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=# \l
List of databases
Name | 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/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows) postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | test | table | postgres
(1 row) postgres=# select * from test;
id
----
1
2
3
(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
----
1
2
3
4
(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 removed LOG: 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索引页磨砺技术珠矶,践行数据之道,追求卓越价值
PostgreSQL的streaming replication的更多相关文章
- 配置PostgreSQL Streaming Replication集群
运行环境: Primary: 192.168.0.11 Standby: 192.168.0.21, 192.168.0.22 OS: CentOS 6.2 PostgreSQL: 9.1.2 版本以 ...
- Streaming replication slots in PostgreSQL 9.4
Streaming replication slots are a pending feature in PostgreSQL 9.4, as part of the logical changese ...
- PostgreSQL 9.3 Streaming Replication 状态监控
postgresql是使用Streaming Replication来实现热备份的,热备份的作用如下: 灾难恢复 高可用性 负载均衡,当你使用Streaming Replication来实现热备份(h ...
- PostgreSQL Streaming Replication的FATAL ERROR
磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页[作者 高健@博客园 luckyjackgao@gm ...
- postgresql Streaming Replication监控与注意事项
一监控Streaming Replication集群 1 pg_stat_replication视图(主库端执行) pid Wal sender process的进程ID usesysid 执行流复制 ...
- [笔记] postgresql 流复制(streaming replication)
基本环境说明: os:FreeBSD 9.3 postgresql version: master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 ...
- postgresql集群方案参考答案
PostgreSQL配置Streaming Replication集群 http://www.cnblogs.com/marsprj/archive/2013/03/04/2943373.html p ...
- 《A Tour of PostgreSQL Internals》学习笔记——进程间通信
中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...
- [转]PostgreSQL源码结构
PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信. 另外,还有一种‘Standalone Backend’使用 ...
随机推荐
- SVG中的元素属性
SVG attributes by category Animation event attributes onbegin, onend, onload, onrepeat Animation att ...
- 二值形态学——腐蚀与膨胀 及 C语言代码实现
参考文献:数字图像处理(第三版) 何东健 西安电子科技大学出版社 二值形态学中的运算对象是集合, 但实际运算中, 当涉及两个集合时并不把它们看作是互相对等的. 一般设A为图像集合, S为结构元素, 数 ...
- js 调用 oc 的解释
JavaScriptCore NSInvocation js解释器在解释函数调用时,会在执行环境进行函数搜索,主调者类型判定: 如果是js调用,直接解释执行: 如果是oc调用,则将调用打包成NSInv ...
- SoapUI这么好,舍得不用吗?
之前尝试去学习哈SoapUI, 安装都报错,直接拖黑不用,对java开发的产品本身不感冒 后来工作上,和老外沟通,发现他们不会用xmlspy,只会SoapUI,心里都想,不学习看来不方便,然后都安装了 ...
- 【[AHOI2008]逆序对】
被锤爆了 被这个题搞得自闭了一上午,觉得自己没什么前途了 我又没有看出来这个题的一个非常重要的性质 我们填进去的数一定是单调不降的 首先如果填进去的数并不是单调不降的,那么填进去本身就会产生一些逆序对 ...
- 【php】获取ip
addBoard.php中获取到ip $ip=$_SERVER['REMOTE_ADDR']; 通过ajax采取POST方式发送到服务器 $("#submit").on(" ...
- Kali-linux Gerix Wifi Cracker破解无线网络
Gerix Wifi Cracker是另一个aircrack图形用户界面的无线网络破解工具.本节将介绍使用该工具破解无线网络及创建假的接入点. 9.3.1 Gerix破解WEP加密的无线网络 在前面介 ...
- virtualbox+vagrant学习-4-Vagrantfile-8-WinSSH
WinSSH WinSSH通信器是专门为OpenSSH的Windows本机端口构建的.它不依赖于类posix的环境,这种环境消除了额外的软件安装(如cygwin)以获得适当功能的需求. 想获得更多的信 ...
- Gradle Goodness: Renaming Files while Copying
With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...
- JAVA并发(一)
java并发的一系列框架和技术主要是由java.util.concurrent 包所提供.包下的所有类可以分为如下几大类: locks部分:显式锁(互斥锁和速写锁)相关: atomic部分:原子变量类 ...