PostgreSQL歸檔配置及自動清理歸檔日志的操作
在一般的生產(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),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL 數(shù)據(jù)庫跨版本升級常用方案解析
這篇文章主要介紹了PostgreSQL 數(shù)據(jù)庫跨版本升級常用方案解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Postgresql在mybatis中報錯:操作符不存在:character varying == unknown的問題
這篇文章主要介紹了Postgresql在mybatis中報錯: 操作符不存在 character varying == unknown的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
基于PostgreSQL pg_hba.conf 配置參數(shù)的使用說明
這篇文章主要介紹了基于PostgreSQL pg_hba.conf 配置參數(shù)的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL事務(wù)回卷實(shí)戰(zhàn)案例詳析
前段時間在公司小范圍做了一個關(guān)于PG事務(wù)實(shí)現(xiàn)的講座,最后總結(jié)了一個摘要性的東西,分享一下,這篇文章主要給大家介紹了關(guān)于PostgreSQL事務(wù)回卷實(shí)戰(zhàn)案例的相關(guān)資料,需要的朋友可以參考下2022-03-03

