自PostgreSQL 9.0開始,添加了流復制(Streaming Repulication)。
SR源於pg早起的“同步日志傳送復制”(Synchronous Log Shipping Repulication)--一個高可用的(HA)解決方案。
安裝與配置(Linux--PostgreSQL9.X)
說明:Primary_IP 表示主服務器IP地址
Standby_IP 表示備用服務器IP地址
1.在主服務器中安裝PostgreSQL(備用服務器同理)
cd ./root/Desktop (打開安裝文件所在目錄)
./configure
make
make install
2.在主服務器的PostgreSQL中配置../data/postgresql.conf
vi postgresql.conf
listen_address = '*'
wal_level = hot_standby
max_wal_zenders = 30 (根據實際情況自己設置即可)
archive_mode = on
archive_command = 'cd'
3.在主服務器的PostgreSQL中配置pg_hba_conf文件中的參數
vi pg_hba.conf
host replication all/postgres Standby_IP/32 trust (all與postgres僅選其一)
4.啟動主服務器中的PostgreSQL數據庫,執行基礎備份:
psql -U posgres
postgres#select pg_start_backup('hot_backup'); (可使用任意符號做備份標記)
postgres#\q
tar -zcvf pgsql.tar.gz pgsql
psql -U posgres
postgres#select pg_stop_backup();
5.將基礎備份拷貝到備用服務器:
scp pgsql.tar.gz Standby_IP:/usr/local/
6.在備用服務器的PostgreSQL中配置postgresql.conf文件中的參數
vi postgresql.conf
hot_standby = on
7.在備用服務器PostgreSQL的data目錄下建立recovery.conf文件
vi recovery.conf
standby_mode = ‘on’
primary_conninfo = 'host=Primay_IP user=posgres port=5432'
安裝配置完畢,重啟主服務器,然后重啟備用服務器。
一定要確保修改后的參數更新完畢,例如可以通過postgres#show wal_level來查看wal_level的參數。
如果參數沒有更新則服務器無法運行,所以一定要保證參數更新,方法是:關閉所有postgres服務后重新啟動。
運行PostgreSQL數據庫后,在priamy的任何修該都會同步到standby中,standby不能對數據庫做修改只能查看。
注意:1 一定要把防火牆關閉,否則主-備服務器無法獲得連接,會出現如下問題:
FATAL: could not connect to the primary server: could not connect to server: No route to host
Is the server running on host "192.168.100.112" and accepting
TCP/IP connections on port 5432?
具體解決方法(將firewall設置為disable):
[root@localhost bin]# setup
[root@localhost bin]# geten
getenforce getent
[root@localhost bin]# geten
getenforce getent
[root@localhost bin]# getenforce
Permissive
(更多問題可以查看pg英文文檔。)