運行環境:html
Primary: 192.168.0.11 sql
Standby: 192.168.0.21, 192.168.0.22數據庫
OS: CentOS 6.2centos
PostgreSQL: 9.1.2 版本以上才支持Streaming Replication。post
Primary和Standby節點最好有相同的環境。ui
1)在Primary和Standy節點上安裝PostgreSQL軟件,安裝路徑爲/opt/pgsqlthis
2)設置postgres用戶的環境變量rest
PGHOME=/opt/pgsqlpostgresql
PGDATA=/home/postgres/mainserver
PATH=$PG_HOME/bin:$PATH:$HOME/bin
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目錄下的內容爲
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配置。
使用psql鏈接primary,建立test數據庫。刷新從節點,能夠看到在其上也生成了test數據庫。
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/