• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在一般的生产环境中,数据库都需要开启归档模式,那么在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空间不足。

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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MySQL处理重复数据的学习笔记发布时间:2022-02-08
下一篇:
Oracle查看和修改连接数(进程/会话/并发等等)发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap