数据库的归档, 是为了更好的保证数据库的数据安全不可或缺的功能, 无论是什么数据库都应开启归档模式!

检查当前数据库情况

检查数据库是否为归档模式

postgres=# \c
You are now connected to database "postgres" as user "postgres".


postgres=# show archive_mode;
archive_mode 
--------------
off
(1 row)


归档模式未启动.

开启数据库归档

修改数据库参数
PostgreSQL数据库参数文件:

[postgres@pg01 ~]$ cd /var/lib/pgsql/12/data
[postgres@pg01 data]$ ll postgresql.conf 
-rw-------. 1 postgres postgres 26612 Mar 1 16:03 postgresql.conf

 

vi postgres.conf修改如下:

wal_level = replica # minimal, replica, or logical
archive_mode = on # enables archiving; off, on, or always
archive_command = 'test ! -f /var/lib/pgsql/12/arch/%f && cp %p /var/lib/pgsql/12/arch/%f'

## 其中: %p表示wal日志文件的路径,%f表示wal日志文件名称。

%p 的目录为 $PGHOME/data/pg_wal

相关参数解释

wal_level参数控制WAL日志信息的输出级别

minimal 记录的WAL日志信息最少, 除了记录数据库异常关闭需要恢复时的WAL外, 其他操作信息都不记录;
replica 记录的WAL信息比minimal信息多, 会记录支持WAL归档、复制和备库中启用只读查询等操作所需的WAL信息;
logical 记录的最多, 包含了支持逻辑解析所需的WAL。 并包含了minimal和replica记录的信息。

 

archive_command

可以将WAL归档到本级目录, 也可以归档到远程其他主机上。

max_wal_senders

控制主库上的最大WAL发送进程数, 通过pg_basebackup命令在主库上做基准备份时也会消耗WAL进程。此参数不能大于max_connections, 默认值10.

创建归档目录
[postgres@pg01 12]$ mkdir /var/lib/pgsql/12/arch

 

重启PG数据库
使用root用户重启PG

[root@pg01 ~]# service postgresql-12 restart
Stopping postgresql-12 service: [ OK ]
Starting postgresql-12 service: [ OK ]

 

检查归档是否开启成功

查看模式
[postgres@pg01 12]$ psql
psql (12.0)
Type "help" for help.


postgres=# show archive_mode;
archive_mode 
--------------
on
(1 row)

 

手动切换日志

在PG10之前

postgres=# select pg_switch_xlog();

 

在PG10之后

postgres=# select pg_switch_wal();
pg_switch_wal 
---------------
0/130005E0
(1 row)

 


检查归档文件
[root@pg01 12]# cd /var/lib/pgsql/12/arch
[root@pg01 arch]# ll
total 4
drwx------. 2 postgres postgres 4096 Mar 3 02:33 20200303

 

转载自:https://www.cnblogs.com/chendian0/p/13474390.html

 posted on 2020-11-16 13:44  xibuhaohao  阅读(264)  评论(0编辑  收藏  举报