系統(tǒng)環(huán)境:centos6.5
數(shù)據(jù)庫(kù)版本: postgres9.3.9
虛擬機(jī)2臺(tái):
Master:10.0.2.160
Slave:10.0.2.69
數(shù)據(jù)存儲(chǔ)位置:/usr/local/pgsql/data/創(chuàng)新互聯(lián)專注于大通企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,購(gòu)物商城網(wǎng)站建設(shè)。大通網(wǎng)站建設(shè)公司,為大通等地區(qū)提供建站服務(wù)。全流程按需求定制開(kāi)發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
安裝過(guò)程可參考我上一篇博客:https://blog.51cto.com/13632960/2117902
兩臺(tái)機(jī)器都需要安裝完成,我在做熱備的時(shí)候,Master數(shù)據(jù)庫(kù)開(kāi)啟,Slave關(guān)閉。
Master端進(jìn)入數(shù)據(jù)庫(kù)并執(zhí)行:
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庫(kù)的節(jié)點(diǎn)數(shù),有多少個(gè)slave庫(kù)就設(shè)多少,
? ? ? wal_level是write ahead log參數(shù)值,設(shè)置流復(fù)制務(wù)必將此值更新成hot_standby
? ? ?wal_keep_segments默認(rèn)值是16,是PG_XLOG下的日志文件數(shù)相關(guān)參數(shù)
?? ? archive也可以選擇關(guān)閉,歸檔是定時(shí)恢復(fù)用的,流復(fù)制不是必須的
/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端如果已經(jīng)安裝了postgres數(shù)據(jù)庫(kù),數(shù)據(jù)目錄名稱一樣的話,可以先停掉備機(jī)數(shù)據(jù)庫(kù),更改數(shù)據(jù)目錄名稱
mv data data.old
遠(yuǎn)程拷貝至slave端并在備機(jī)端解壓
scp data.tar.gz postgres@10.0.2.69:/usr/local/pgsql/
拷貝完成后,進(jìn)入數(shù)據(jù)庫(kù)關(guān)閉備份: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
復(fù)制完成后修改如下內(nèi)容:
?standby_mode = 'on'
primary_conninfo ='host=10.0.2.69??port=5432 user=repuser password=repuser keepalives_idle=60'
正常啟動(dòng)備庫(kù),有異??梢钥磍og
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data >>logfile 2>&1