■ Postgre9.0流方式双机热备简介
1. Postgresql standby
对于大规模数据库应用,如何提供读写分离和热同步集群,是非常重要的内容。PostgreSQL
有多种不同的实施方案,这里我们讨论standby 技术的特色和实现。
2. Standby简介
standby 技术基于PostgreSQL 的预写入日志(wal)同步,建立服务器级的数据同步节点。
它的节点间直接传递写操作的二进制预写入事务,传输效率高、性能好,一致性强,自动同步
DDL 操作。
standby 组件是PostgreSQL 的官方发行版内置功能。
standby 节点的核心机制并不复杂,即通过操作系统命令(或pg standby 命令),将服务器
节点间的二进制日志同步到同一个状态。设定备份节点处于恢复状态。当主节点可访问时,不
断有新的预写入日志产生并同步到备份节点。备份节点就一直处于恢复状态。一旦这个同步过
程因某些原因终止1,备份节点即认为恢复过程结束,自动切换到可访问的状态。
通常我们会使用PostgreSQL 配置项archive command 将wal 存档于一个特定的路径,
standby 使用recovery 配置项restore command 指定一个系统命令,自行获取需要恢复的wal
,如果文件不存在,会等待直到timout 。
archive 和restore 的原本是为了通过wal 实现增量备份和恢复。
因此我们需要注意这样几点:
过去的standby 组件性能很好,但是也有其不足,其中一个很重要的问题是standby 节点不
能读。这使它只能限于作为份节点存在。
3. Postgresql9.0的standby改进
PostgreSQL 9 对于standby 功能进行了多方面的增进:
快速同步允许“尽快的”将处理中的WAL 发布出去。
流式同步允许standby 节点联接到primary 节点上,以流式获取新的WAL 数据,达到真正的
“hot standby”。
压缩传输允许使用压缩方式传输WAL,更为节省带宽。近期见到的几个高负载的数据库服务
器,普遍CPU 资源比较充足,对于此类应用,这个功能相当实用。
replication 联接虽然PostgreSQL 9 要求用于流式同步的登录帐户必需是superuser,但是在
pg hba 中,可以限定该用户只能在primary 节点执行同步复制,提高了安全性。
性能提升文档声称新的基于WAL 文件的复制可以得到一个数量级以上的性能提升,这一点我
还没有确认。
standby 节点可读当standby 节点打开hot standby 选项,可以实时进行只读的查询。这使得
standby 成为一个兼具增量备份/维护、实时同步备用节点和负载均衡能力的多功能组件。
Figure 2: PostgreSQL 9 的standby
安装路径:/opt/PostgreSQL/9.0/
Data目录:/opt/PostgreSQL/9.0/data
Server ip:192.168.1.143
日志归档目录:/opt/PostgreSQL/9.0/archive
standby-server:
安装路径:/opt/PostgreSQL/9.0/
Data目录:/opt/PostgreSQL/9.0/data
Server ip:192.168.1.160
日志归档目录:/opt/PostgreSQL/9.0/archive
其他默认安装,server之间可互相被访问。
■ 实施环境设定
Ø Primary-server设定
1. 在primary-server上设定一个可允许standby-server访问的权限
| $ $EDITOR pg_hba.conf |
| # The standby server must have superuser access privileges. |
| host replication postgres 192.168.0.20/22 trust |
2. 在primary-server上设置流方式同步相关的参数
| $ $EDITOR postgresql.conf |
| # To enable read-only queries on a standby server, wal_level must be set to # "hot_standby". But you can choose "archive" if you never connect to the # server in standby mode. wal_level = hot_standby
# Set the maximum number of concurrent connections from the standby servers. max_wal_senders = 5
# To prevent the primary server from removing the WAL segments required for # the standby server before shipping them, set the minimum number of segments # retained in the pg_xlog directory. At least wal_keep_segments should be # larger than the number of segments generated between the beginning of # online-backup and the startup of streaming replication. If you enable WAL # archiving to an archive directory accessible from the standby, this may # not be necessary. wal_keep_segments = 32
# Enable WAL archiving on the primary to an archive directory accessible from # the standby. If wal_keep_segments is a high enough number to retain the WAL # segments required for the standby server, this may not be necessary. archive_mode = on archive_command = 'cp –f %p /opt/PostgreSQL/9.0/archive/%f' |
3. 启动primary-server数据库
略
Ø Primary-server & Standby-server的Base数据库同步
| $ psql -c "SELECT pg_start_backup('label', true)" $ rsync -a --progress /opt/PostgreSQL/9.0/data/* [email protected]:/opt/PostgreSQL/9.0/data/ --exclude postmaster.pid $ psql -c "SELECT pg_stop_backup()" |
注意:在primary-server上操作
Ø Standby-server设定
1. 设置standby-server可执行只读查询参数
| $ $EDITOR postgresql.conf hot_standby = on |
2. 在standby-server上创建recorvery.conf的配置文件并进行设置
| $ $EDITOR recovery.conf # Note that recovery.conf must be in $PGDATA directory.
# 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.1.143 port=5432 user=postgres password=postgres'
# Specifies a trigger file whose presence should cause streaming replication to # end (i.e., failover). trigger_file = '/tmp/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 /path_to/archive/%f "%p"' |
3. 在standby-server上启动postgresql,开始流方式同步
略
■ 热备功能验证
1. 根据进程判断
| # The displayed LSNs indicate the byte position that the standby server has # written up to in the xlogs. [primary] $ ps -ef | grep sender postgres 6879 6831 0 10:31 ? 00:00:00 postgres: wal sender process postgres 127.0.0.1(44663) streaming 0/2000000
[standby] $ ps -ef | grep receiver postgres 6878 6872 1 10:31 ? 00:00:01 postgres: wal receiver process streaming 0/2000000 |
2. Standby-server pg_log确认
| LOG:database system is ready to accept read only connections LOG:streaming replication successfully connected to primary |
3. Primary-server执行postgre的DML,DDL后再查看standby-server上是否同步反映
转载于:https://my.oschina.net/gammatimes/blog/630723