PostgreSQL 归档日志

在本教程中,我将解释如何在 postgresql 服务器中启用 archivelog
  • WAL 存档日志在 PostgreSQL 数据库系统中,实际数据库将数据“写入”磁盘上名为预写日志 (WAL) 的附加文件。
  • 它记录了数据库系统中的写入操作。当数据库崩溃时,可以通过这些记录来修复/恢复数据库。
  • 通常,预写日志会定期(称为检查点)记录与数据库匹配的内容,然后删除,因为不再需要它。您还可以使用 WAL 作为备份,因为它记录了对数据库进行的所有写入操作。
WAL 归档概念:
 
  • pg_xlog 中存储了预写日志。它是日志文件,其中存储了已提交和未提交事务的所有日志。它最多包含 6 个日志,最后一个日志将被覆盖。如果存档器处于打开状态,它会移动到那里。
  • 预写日志由每个 16 MB 大小的部分组成,这些部分称为段。 
  • WAL 位于 pg_xlog 目录下,它是“数据目录”的子目录。文件名将包含数字(0-9)和字符(az),按 PostgreSQL 实例的升序命名。要根据 WAL 执行备份,需要进行基本备份,即对数据目录以及基本备份和当前日期之间的 WAL 段进行完整备份。
--首先使用 psql 终端检查 postgresql 服务器是否已启用存档日志。
复制代码
postgres=# show archive_mode;
 archive_mode 
--------------
 off
(1 row)

postgres=# show archive_command;;
 archive_command 
-----------------
 (disabled)
(1 row) 
复制代码

--让我们创建备份和存档目录(以 root 用户身份登录):

 
mkdir -p /Archive/Location
mkdir /backups
chown postgres:postgres -R Archive        
chown postgres:postgres /backups

--check the folder permission

[root@p1 /]# ls -lrt
drwxr-xr-x   2 postgres postgres  4096 Apr 10 07:54 backups
drwxr-xr-x   3 postgres postgres  4096 Apr 10 07:54 Archive

--切换到postgres用户并修改postgresql.conf。使集群进入Archive-log模式需要RESTART。

[root@p1 /]# su postgres 
cd $PGDATA 
echo 'max_wal_senders=1' >> postgresql.conf 
echo 'wal_level=hot_standby' >> postgresql.conf 
echo 'archive_mode=on' >> postgresql.conf 
echo "archive_command='cp %p /Archive/Location/%f'" >> postgresql.conf

--重启服务器并检查服务器是否正在运行

复制代码
bash-3.2$ pg_ctl restart -D $PGDATA 
waiting for server to shut down.... done
server stopped
server starting
bash-3.2$ 2017-04-10 08:12:07 EDT LOG:  redirecting log output to logging collector process
2017-04-10 08:12:07 EDT HINT:  Future log output will appear in directory "pg_log".

bash-3.2$ pg_ctl status -D $PGDATA
pg_ctl: server is running (PID: 9825)
/opt/PostgreSQL/9.3/bin/postgres "-D" "/opt/PostgreSQL/9.3/data"
复制代码

--服务器启动后,使用 psql 终端检查档案是否正确启用。

复制代码
postgres=# show archive_mode;
 archive_mode 
--------------
 on
(1 row)


postgres=# show archive_command;
      archive_command       
----------------------------
 cp %p /Archive/Location/%f
(1 row)


postgres=# show wal_level; 
  wal_level  
-------------
 hot_standby
(1 row)
复制代码

 

posted @   wongchaofan  阅读(170)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-07-20 ubuntu安装mysql5.7的时候提示mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
2020-07-20 ubuntu版本scp连接Permission denied, please try again.问题
点击右上角即可分享
微信分享提示