系統環境:centos6.5
數據庫版本: postgres9.3.9
虛擬機2臺:
Master:10.0.2.160
Slave:10.0.2.69
數據存儲位置:/usr/local/pgsql/data/sql
安裝過程可參考我上一篇博客:https://blog.51cto.com/13632960/2117902 兩臺機器都須要安裝完成,我在作熱備的時候,Master數據庫開啓,Slave關閉。
Master端進入數據庫並執行: CREATE USER repuser replication PASSWORD 'repuser';
vim /usr/local/pgsql/data/pg_hba.conf 在最後添加:host replication repuser 10.0.2.69/16 md5 #md5爲鏈接方式 大家也能夠選擇其餘的
max_wal_senders = 1 wal_level = hot_standby archive_mode = on archive_command = 'cp %p /usr/local/pgsql/data/archive/%f'' hot_standby = on wal_keep_segments = 64
註釋: max_wal_senders是Slave庫的節點數,有多少個slave庫就設多少, wal_level是write ahead log參數值,設置流複製務必將此值更新成hot_standby wal_keep_segments默認值是16,是PG_XLOG下的日誌文件數相關參數 archive也能夠選擇關閉,歸檔是定時恢復用的,流複製不是必須的
/usr/local/pgsql/bin/psql psql (9.3.9) Type "help" for help. postgres=# select pg_start_backup('backup_1');
cd /usr/local/pgsql/ tar -czvf data.tar.gz data--exclude=data/pg_xlog Slave端若是已經安裝了postgres數據庫,數據目錄名稱同樣的話,能夠先停掉備機數據庫,更改數據目錄名稱 mv data data.old 遠程拷貝至slave端並在備機端解壓 scp data.tar.gz postgres@10.0.2.69:/usr/local/pgsql/ 拷貝完成後,進入數據庫關閉備份:select pg_stop_backup(),current_timestamp;
cd /usr/local/pgsql tar -zxvf data.tar.gz cd /usr/local/pgsql/data mkdir pg_xlog vim /usr/local/pgsql/data/postgresql.conf 修改:hot_standby = on
cp /usr/local/pgsql/share/recovery.conf.sample /usr/local/pgsql/data/recovery.conf 複製完成後修改以下內容: standby_mode = 'on' primary_conninfo ='host=10.0.2.69 port=5432 user=repuser password=repuser keepalives_idle=60'
正常啓動備庫,有異常能夠看log /usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data >>logfile 2>&1