本测试模拟pg_multixact,pg_xlog全部信息丢失的情况下,数据库完全起不来的情况下拉起数据库。本人血淋淋的教训,建议还是做好备库和增量备份。
一、初始化一个新的实例、创建数据库等。(因为当前系统用户并非postgres,因此我加了-U 来默认数据库超管用户postgres)
[lyf@centos250 pg9.6]$ initdb -D pg_db_test --encoding=UTF-8 --locale=C -U postgres The files belonging to this database system will be owned by user "lyf". This user must also own the server process. ….信息忽略 Success. You can now start the database server using: pg_ctl -D pg_db_test -l logfile start
修改配置端口即可,防止和其他已有实例冲突。如果本机只有一个pg实例,此步骤忽略。
listen_addresses = '*' # (change requires restart) port = 5678 # (change requires restart)
启动服务器
[lyf@centos250 pg9.6]$ pg_ctl start -D pg_db_test/ -l pg_db_test/db.log server starting
Psql进入数据库,创建测试库和测试数据
[lyf@centos250 pg9.6]$ psql -U postgres -p 5678 psql (9.6.0) Type "help" for help. postgres=# create database dbtest; CREATE DATABASE postgres=# \c dbtest; You are now connected to database "dbtest" as user "postgres". dbtest=# create table t_test(id int , name text); CREATE TABLE dbtest=# insert into t_test select generate_series(1,10000) , md5(random()::text); INSERT 0 10000 dbtest=# insert into t_test select generate_series(1,10000) , md5(random()::text); INSERT 0 10000 dbtest=# insert into t_test select generate_series(1,10000) , md5(random()::text); INSERT 0 10000 dbtest=# insert into t_test select generate_series(1,10000) , md5(random()::text); INSERT 0 10000 -- 此处没有checkpoint的话,删除了xlog之后很可能数据已经丢了,感兴趣的可以试试 dbtest=# checkpoint ; CHECKPOINT dbtest=# \q
二、停库,删除pg_multixact/members, pg_multixact/offsets 下的文件。
先停库,做好clog,xlog,multixact的备份
[lyf@centos250 pg9.6]$ pg_ctl stop -m fast -D pg_db_test/ waiting for server to shut down.... done server stopped [lyf@centos250 pg9.6]$ cd pg_db_test/ [lyf@centos250 pg_db_test]$ zip -r xlog.zip pg_xlog adding: pg_xlog/ (stored 0%) adding: pg_xlog/000000010000000000000001 (deflated 83%) adding: pg_xlog/archive_status/ (stored 0%) [lyf@centos250 pg_db_test]$ zip -r multixact.zip pg_multixact adding: pg_multixact/ (stored 0%) adding: pg_multixact/members/ (stored 0%) adding: pg_multixact/members/0000 (deflated 100%) adding: pg_multixact/offsets/ (stored 0%) adding: pg_multixact/offsets/0000 (deflated 100%)
删除文件
[lyf@centos250 pg_db_test]$ rm pg_xlog/* -r [lyf@centos250 pg_db_test]$ rm pg_multixact/members/* [lyf@centos250 pg_db_test]$ rm pg_multixact/offsets/* [lyf@centos250 pg_db_test]$
三、 尝试启动数据库
[lyf@centos250 pg9.6]$ pg_ctl start -D pg_db_test/ -l pg_db_test/db.log server starting [lyf@centos250 pg9.6]$ tail -f pg_db_test/db.log LOG: creating missing WAL directory "pg_xlog/archive_status" LOG: invalid primary checkpoint record LOG: invalid secondary checkpoint record PANIC: could not locate a valid checkpoint record LOG: startup process (PID 16658) was terminated by signal 6: Aborted LOG: aborting startup due to startup process failure LOG: database system is shut down
此时,数据库已经起不来了
四、尝试通过resetxlog恢复,参考手册的配置:https://www.postgresql.org/docs/9.6/static/app-pgresetxlog.html 。这里我们重点需要-O、-x、-m三个参数
-O mxoff
此时pg_multixact/members 文件夹下文件为空,因此值为 to_hex((0 + 1) * 52352) = 0xcc80, 注意此参数9.2以前为65536 , 9.3以后改为52352。
-x xid
[lyf@centos250 pg9.6]$ ls pg_db_test/pg_clog/ 0000
此时pg_clog下的文件夹文件最大0000,因此值为: 0x100000。
-m mxid,mxid
此时pg_multixact/ offsets文件夹下文件为空, 因此给值: 0x10000,0x10000
[lyf@centos250 pg9.6]$ pg_resetxlog -m 0x10000,0x10000 -x 0x100000 -O 0xcc80 -f pg_db_test/ Transaction log reset
观察此时的文件状态中xlog已经重置了wal。
五、再次重启数据库测试
[lyf@centos250 pg9.6]$ pg_ctl start -D pg_db_test/ -l pg_db_test/db.log server starting [lyf@centos250 pg9.6]$ tail -f pg_db_test/db.log LOG: database system was shut down at 2017-09-13 15:00:49 CST LOG: MultiXact member wraparound protections are now enabled LOG: database system is ready to accept connections LOG: autovacuum launcher started
此时数据库已经启动,psql进入查看数据。
[lyf@centos250 pg9.6]$ psql -U postgres -p 5678 psql (9.6.0) Type "help" for help. postgres=# \l+ List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description -----------+----------+----------+---------+-------+-----------------------+---------+------------+-------------------------------------------- dbtest | postgres | UTF8 | C | C | | 9767 kB | pg_default | postgres | postgres | UTF8 | C | C | | 7063 kB | pg_default | default administrative connection database template0 | postgres | UTF8 | C | C | =c/postgres +| 6953 kB | pg_default | unmodifiable empty database | | | | | postgres=CTc/postgres | | | template1 | postgres | UTF8 | C | C | =c/postgres +| 6953 kB | pg_default | default template for new databases | | | | | postgres=CTc/postgres | | | (4 rows) postgres=# \c dbtest You are now connected to database "dbtest" as user "postgres". dbtest=# \d List of relations Schema | Name | Type | Owner --------+--------+-------+---------- public | t_test | table | postgres (1 row) dbtest=# select count(1) from t_test ; count ------- 40000 (1 row) dbtest=# create table t_test2(id int , name text); CREATE TABLE dbtest=# insert into t_test2 values(1 , '1'); INSERT 0 1 dbtest=#
到此,数据库恢复正常。
相关推荐
PG导入导出命令详解
pg_dump -a -t tbl_test "host=127.0.0.1 hostaddr=127.0.0.1 port=5432 user=postgres password=123456 dbname=postgres" > /userdir/tbl_data a 参数是表示只导出数据,其他的额外信息不需要,该参数也可去掉 psql...
pg_timetable:PostgreSQL高级调度 pg_timetable是PostgreSQL高级作业调度程序,与cron等传统调度程序相比,具有许多优势。 它完全由数据库驱动,并提供了一些高级概念。 # ./pg_timetable Application Options: -...
可以直接粘贴,让服务器的数据库直接访问。
该存储库将保存您的postgres集群的统计信息“样本”。 通过调用take_sample()函数获取样本。 PostgreSQL没有任何类似于工作的引擎,因此您需要使用cron 。 定期抽样可以帮助您找到过去最耗费资源的活动。 假设您...
pg_probackup官方英文文档+中文翻译,pg_probackup管理PostgreSQL数据库集群的备份和恢复.pg_probackup是一个实用程序,用于管理PostgreSQL数据库集群的备份和恢复。它旨在执行PostgreSQL实例的定期备份,使您能够在...
在本地执行上下文中,要获得足够的权限来显示系统信息,运行pg_activity的系统用户必须是运行postgresql服务器的用户(默认情况下为postgres ),或者具有更多权限,例如root 。 否则, pg_activity可以回pg_...
postgres_vectorization_test, 矢量执行器,用于加速 PostgreSQL 向量执行器今年夏天我在Citus数据上暂存,并实现了sql的矢量执行器。 我们观察到了 3 -4x的性能改进,对于简单的选择查询,并决定打开我的项目。本文...
pg_auto_failover是PostgreSQL扩展和服务,用于监视和管理Postgres集群的自动故障转移。 它针对简单性和正确性进行了优化,并支持Postgres 10和更高版本。 pg_auto_failover支持几种Postgres架构,并为您的...
博客:PostgreSQL的学习心得和知识总结(六十五)|关于PostgreSQL数据库 实现MySQL数据库find_in_set()函数 的实现方案
postgres_exporter-0.9.0.linux-amd64.tar.gz
Postgres_Plus_Advanced_Server_Migration_Guide_v1.0_CN[1]
Postgres_Plus_Advanced_Server_JDBC_Connector_Guide_CN_v1.1[1]
导入osm数据到postgres数据库工具
pg_bloat_check pg_bloat_check是一个脚本,用于为PostgreSQL表和/或索引提供膨胀报告。 它至少需要Python 2.6中和pgstattuple的contrib模块- 注意,在非常大的数据库或具有许多表的数据库上,使用pgstattuple检查...
PostgreSQL有3种日志,分别是 pg_log(数据库运行日志) pg_xlog(WAL 日志,即重做日志) pg_clog(事务提交日志,记录的是事务的元数据)
PostgreSQL服务器导出器 用于PostgreSQL服务器指标的Prometheus导出器。... -e DATA_SOURCE_NAME="postgresql://postgres:password@localhost:5432/postgres?sslmode=disable" \ quay.io/prometheuscommunity/post
复制以上文件 pg_hba.conf 和 postgresql.conf ,并替换 /home/data/pgsql/data 下对应的文件 并根据当前服务器内存大小更改 postgresql.conf 中的 shared_buffered 参数(如果是数据库服务器,建议设置为总内存的...
再postgrsql-timescaledb-postgis的基础上增加pg_cron Dockerfile中的FROM reg.ji....postgresql.conf中cron.database_name指定了可以使用pg_cron扩展插件的数据库名称
pg_similarity是对支持相似性查询的扩展。 就实现而言,该实现紧密地集成在RDBMS中,因为它定义了运算符,因此可以使用~~~代替传统的运算符(=和<>)。 ! (这些运算符中的任何一个都代表相似性函数)。 pg_...