今天在将pg_dump压缩过的dump文件,通过pg_restore 导入到测试库时,中途异常中断,造成测试库宕机,而且之后数据无法启动。
数据库宕机的 Csvlog
1 |
2010-09-17 17:28:03.943 CST,"mydb","mydb",23936,"192.168.1.25:58855",4c9334a3.5d80,1,"/opt/pgsql/bin/postgres",2010-09-17 17:28:03 CST,,0,FATAL,57P03,"the database system is in recovery mode",,,,,,,, |
从数据库down机前的 csvlog日志来看,down机前数据库SERVER 处于 recovery mode , 说明pg_restore时 SERVER此时有异常,之后尝试重新起动Server,命令如下
1 |
[postgres@PG1 bin]$ pg_ctl -D $PGDATA start |
虽然显示’server starting’, 但数据库并没有真正起来,因为这时数据库根本无法连接,接着查看 csvlog
数据库启动异常时 Csvlog
1 |
2010-09-17 17:36:36.660 CST,"mydb","mydb",24245,"192.168.169.42:42566",4c9336a4.5eb5,1,"/opt/pgsql/bin/postgres",2010-09-17 17:36:36 CST,,0,FATAL,57P03,"the database system is starting up",,,,,,,, |
从上面日志来看,可以看出Server 数据库正在启动(the database system is starting up”), 但是到后面就异常中上了; 而且也没有多余的信息,由于事情紧迫,随即向师傅请教,师傅说数据库需要恢复,可以恢复到具体的时间点。以下是解决过程。
解决过程
pg_controldata 查看PG SERVER 详细信息
1 |
[postgres@PG1 bin]$ pg_controldata $PGDATA |
重要的信息:Latest checkpoint's NextXID: 0/64282, Latest checkpoint's NextXID
是指最近一次安全的checkpoints的下一个事务ID,我们可以将数据库恢复到这一时刻。
通过pg_resetxlog 将数据库恢复到事务 64282 时刻
1 |
[postgres@PG1 bin]$ pg_resetxlog --help |
再次启动 PG SERVER,正常,此时数据库已恢复
1 |
[postgres 1 bin]$ pg_ctl -D $PGDATA start |
到了这里,数据库PG SERVER 终于可以启来了,这里顺便说一下,之前的 pg_restore 中途异常是因为脚本中加了 -j 参数,同时跑多个 pg_restore 线程, 造成 pg_restore 子线程连接丢失,pg_restore 脚本中去掉 -j 参数时,数据库顺利导入。
Pg_controldata 官网文档介绍
Name
pg_controldata ― display control information of a PostgreSQL database cluster
Synopsis
pg_controldata [datadir]
Description
pg_controldata prints information initialized during initdb, such as the catalog version. It also shows information about write-ahead logging and checkpoint processing. This information is cluster-wide, and not specific to any one database.
This utility can only be run by the user who initialized the cluster because it requires read access to the data directory. You can specify the data directory on the command line, or use the environment variable PGDATA.
Environment
PGDATA
Default data directory location
Pg_resetxlog 官网文档介绍
pg_resetxlog ― reset the write-ahead log and other control information of a PostgreSQL database cluster
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/236329.html