PostgreSQL归档配置及自动清理归档日志的操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PostgreSQL归档配置及自动清理归档日志的操作

foucus、   2021-01-14 我要评论
这篇文章主要介绍了PostgreSQL归档配置及自动清理归档日志的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在一般的生产环境中,数据库都需要开启归档模式,那么在pg中如何开启归档模式呢?

pg中的归档配置涉及几个参数如下:

# - 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' 
 
超时强制归档,例:如果10分钟数据库都没有什么活动,一个redo文件没有写完,就不会归档, 
但是我们希望数据库至少10分钟要切换一个日志,则可以使用archive_timeout 
#archive_timeout = 0      # force a logfile segment switch after this 
                # number of seconds; 0 disables 

–归档配置方法为:

1、创建归档目录

pg12@oracle-> mkdir -p $PGDATA/archive/

2、编辑归档脚本

该脚本还可以删除7天内的归档日志。

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、配置归档相关参数

wal_level = replica 
archive_mode = on 
archive_command = 'arch.sh %f %p'

配置完之后重启数据库服务即可。

pg12@oracle-> ps -ef|grep archiver
pg12 21338 21331 0 20:20 ? 00:00:00 postgres: archiver

补充:postgresql归档失败并在日志文件中存在报错

PG运行过程中出现归档失败的情况,并在日志文件中存在报错。

报错如下:

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

原因是归档日志所在文件系统/arch空间不足。

通过清理该文件系统下过期的临时文件,或将归档日志存放至更大的系统空间中。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们