■  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.0standby改进

 

PostgreSQL 9 对于standby 功能进行了多方面的增进:

快速同步允许“尽快的”将处理中的WAL 发布出去。

流式同步允许standby 节点联接到primary 节点上,以流式获取新的WAL 数据,达到真正的

hot standby”。

压缩传输允许使用压缩方式传输WAL,更为节省带宽。近期见到的几个高负载的数据库服务

器,普遍CPU 资源比较充足,对于此类应用,这个功能相当实用。

replication 联接虽然PostgreSQL 9 要求用于流式同步的登录帐户必需是superuser,但是在

pg hba 中,可以限定该用户只能在primary 节点执行同步复制,提高了安全性。

性能提升文档声称新的基于WAL 文件的复制可以得到一个数量级以上的性能提升,这一点我

还没有确认。

standby 节点可读当standby 节点打开hot standby 选项,可以实时进行只读的查询。这使得

standby 成为一个兼具增量备份/维护、实时同步备用节点和负载均衡能力的多功能组件。

postgre9.0流方式双机热备

 

Figure 2: PostgreSQL 9 standby

 安装路径:/opt/PostgreSQL/9.0/

Data目录:/opt/PostgreSQL/9.0/data

Server ip192.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-serverBase数据库同步

$ 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执行postgreDMLDDL后再查看standby-server上是否同步反映


转载于:https://my.oschina.net/gammatimes/blog/630723

相关文章: