在一般的生產(chǎn)環(huán)境中,數(shù)據(jù)庫都需要開啟歸檔模式,那么在pg中如何開啟歸檔模式呢?
pg中的歸檔配置涉及幾個參數(shù)如下:
# - Archiving -
是否開啟歸檔
#archive_mode = off # enables archiving; off, on, or always
# (change requires restart)
歸檔命令,注意 %p %f %% 格式化的含義。
%p 是被歸檔的redo文件的路徑,
%f 是被歸檔的redo文檔的文件名
%% 是百分號
#archive_command = '' # command to use to archive a logfile segment
# placeholders: %p = path of file to archive
# %f = file name only
# e.g. 'test ! -f /mnt/server/archivedir/%f cp %p /mnt/server/archivedir/%f'
超時強(qiáng)制歸檔,例:如果10分鐘數(shù)據(jù)庫都沒有什么活動,一個redo文件沒有寫完,就不會歸檔,
但是我們希望數(shù)據(jù)庫至少10分鐘要切換一個日志,則可以使用archive_timeout
#archive_timeout = 0 # force a logfile segment switch after this
# number of seconds; 0 disables
–歸檔配置方法為:
1、創(chuàng)建歸檔目錄
pg12@oracle-> mkdir -p $PGDATA/archive/
2、編輯歸檔腳本
該腳本還可以刪除7天內(nèi)的歸檔日志。
pg12@oracle-> vi $PGDATA/arch.sh
test ! -f $PGDATA/arch/$1 cp --preserve=timestamps $2 $PGDATA/arch/$1 ; find /arch/ -type f -mtime +7 -exec rm -f {} \;
3、配置歸檔相關(guān)參數(shù)
wal_level = replica
archive_mode = on
archive_command = 'arch.sh %f %p'
配置完之后重啟數(shù)據(jù)庫服務(wù)即可。
pg12@oracle-> ps -ef|grep archiver
pg12 21338 21331 0 20:20 ? 00:00:00 postgres: archiver
補(bǔ)充:postgresql歸檔失敗并在日志文件中存在報錯
PG運(yùn)行過程中出現(xiàn)歸檔失敗的情況,并在日志文件中存在報錯。
報錯如下:
cp: writing `/arch/20171204/000000010000000000000002': No space left on device
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: DATE=`date +%Y%m%d`;DIR="/arch/$DATE";(test -d $DIR || mkdir -p $DIR) cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002
cp: writing `/arch/20171204/000000010000000000000002': No space left on device
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: DATE=`date +%Y%m%d`;DIR="/arch/$DATE";(test -d $DIR || mkdir -p $DIR) cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002
cp: writing `/arch/20171204/000000010000000000000002': No space left on device
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: DATE=`date +%Y%m%d`;DIR="/arch/$DATE";(test -d $DIR || mkdir -p $DIR) cp pg_xlog/000000010000000000000002 $DIR/000000010000000000000002
WARNING: archiving transaction log file "000000010000000000000002" failed too many times, will try again later
原因是歸檔日志所在文件系統(tǒng)/arch空間不足。
通過清理該文件系統(tǒng)下過期的臨時文件,或?qū)w檔日志存放至更大的系統(tǒng)空間中。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PostgreSQL 性能優(yōu)化之服務(wù)器參數(shù)配置操作
- postgresql 性能參數(shù)配置方式
- postgresql安裝及配置超詳細(xì)教程
- Postgresql的日志配置教程詳解
- PostgreSQL 邏輯復(fù)制 配置操作
- 基于PostgreSQL pg_hba.conf 配置參數(shù)的使用說明
- PostgreSQL 自動Vacuum配置方式