首页 > 数据库 >使用wt工具恢复mongo数据库单个集合文件

使用wt工具恢复mongo数据库单个集合文件

时间:2023-07-20 16:25:30浏览次数:42  
标签:rw 20 mongo -- 数据库 Jul wt root

单节点 MongoDB,宕机后 --repair 起不来,或只有 collection 物理文件想做数据恢复,使用 wt 工具恢复方法。

参考文档:
https://mongoing.com/archives/81301
https://blog.csdn.net/qq_28018283/article/details/106658647
https://nintha.github.io/2018/05/06/MongoDB/Wiredtiger_restore_MongoDB_data/
https://developer.aliyun.com/article/73203
https://www.alexbevi.com/blog/2016/02/10/recovering-a-wiredtiger-collection-from-a-corrupt-mongodb-installation/?spm=a2c6h.12873639.article-detail.4.36963e4akPZdRG
https://source.wiredtiger.com/11.1.0/build-posix.html


本地环境:
arm ubuntu 20

1、安装依赖包

apt-get install libsnappy-dev build-essential

2、安装 wt 工具

参考:https://source.wiredtiger.com/11.1.0/build-posix.html

其中【git clone git://github.com/wiredtiger/wiredtiger.git】替换为【git clone https://github.com/wiredtiger/wiredtiger.git】
cmake 时会报一些 not found 的错误,是缺少依赖包,反馈 chatpgt 安装好依赖就能正常安装了。
按照文档操作到 make install 即可。

3、执行 wt 工具

第一次执行 wt 命令报错【/usr/local/bin/wt: error while loading shared libraries: libwiredtiger.so.11.2.0: cannot open shared object file: No such file or directory】

解决方法:【export LD_LIBRARY_PATH=/usr/local/lib/libwiredtiger.so.11.2.0:$LD_LIBRARY_PATH】

4、恢复单个物理文件

整个恢复流程基本参考张耀星的这篇文章【https://mongoing.com/archives/81301】
基本思路就是创建一个没用的集合,找到没用集合对应的物理文件,将要恢复的物理文件替换掉没用集合的物理文件。
比如目前有一个集合的物理文件要恢复【文件名称:collection-11-1234567890.wt】



恢复流程:

4.1 启动一个mongo实例
mongod --dbpath "/root/tmpdata1"  --port 27018
4.2 进入数据库创建空集合,记录 uri 信息中的文件名称 collection-8--7050449506244367834 ,它是 t1 集合对应的物理文件名称。
mongo --port 27018

> show dbs
admin   0.000GB
ceshi   0.004GB
config  0.000GB
local   0.000GB
> use ceshi
switched to db ceshi
> db.t1.insert({id:1})
WriteResult({ "nInserted" : 1 })
> show tables
t1

> db.t1.stats()
{
        "ns" : "ceshi.t1",
        "size" : 6301010,
        "count" : 10589,
        "avgObjSize" : 595,
        "storageSize" : 3411968,
        "capped" : false,
        "wiredTiger" : {
                "metadata" : {
                        "formatVersion" : 1
                },
                "creationString" : "",
                "type" : "file",
                "uri" : "statistics:table:collection-8--7050449506244367834",

4.3 关闭数据库替换物理文件

关闭数据库后,开始替换物理文件
root@db-0:~/tmpdata1# ll
total 4212
drwxr-xr-x  4 root root    4096 Jul 20 15:59 ./
drwx------ 19 root root    4096 Jul 20 14:27 ../
-rw-------  1 root root      46 Jul 20 14:27 WiredTiger
-rw-------  1 root root      21 Jul 20 14:27 WiredTiger.lock
-rw-------  1 root root    1254 Jul 20 15:59 WiredTiger.turtle
-rw-------  1 root root   49152 Jul 20 15:59 WiredTiger.wt
-rw-------  1 root root   49152 Jul 20 15:56 WiredTiger.wt.orig
-rw-------  1 root root    4096 Jul 20 15:59 WiredTigerLAS.wt
-rw-------  1 root root   36864 Jul 20 15:59 _mdb_catalog.wt
-rw-------  1 root root   20480 Jul 20 15:59 collection-0--7050449506244367834.wt
-rw-------  1 root root   36864 Jul 20 15:59 collection-2--7050449506244367834.wt
-rw-------  1 root root   36864 Jul 20 15:59 collection-4--7050449506244367834.wt
-rw-r--r--  1 root root 3411968 Jul 20 15:59 collection-8--7050449506244367834.wt  --这个文件
drwx------  2 root root    4096 Jul 20 15:59 diagnostic.data/
-rw-------  1 root root   36864 Jul 20 15:59 index-0-8916583402311889064.wt
-rw-------  1 root root  487424 Jul 20 15:59 index-1-8916583402311889064.wt
-rw-------  1 root root   24576 Jul 20 15:59 index-2-8916583402311889064.wt
-rw-------  1 root root   24576 Jul 20 15:59 index-3-8916583402311889064.wt
-rw-------  1 root root   20480 Jul 20 15:59 index-4-8916583402311889064.wt
drwx------  2 root root    4096 Jul 20 15:56 journal/
-rw-------  1 root root       0 Jul 20 15:59 mongod.lock
-rw-------  1 root root   36864 Jul 20 15:59 sizeStorer.wt
-rw-------  1 root root     114 Jul 20 14:27 storage.bson
root@db-0:~/tmpdata1# rm -rf collection-8--7050449506244367834.wt
root@db-0:~/tmpdata1# cp /tmp/collection-11-1234567890.wt collection-8--7050449506244367834.wt

4.4 修复文件,并启动数据库

#注意:wt 命令一定要在mongo数据目录中执行
root@db-0:~/tmpdata1# /usr/local/bin/wt -v -C "extensions=[/usr/local/lib/libwiredtiger_snappy.so]"  salvage file:collection-8--7050449506244367834.wt
        WT_SESSION.salvage 100                 
root@db-0:~/tmpdata1# 

#正常启动数据库
root@db-0:~/tmpdata1# mongod --dbpath "/root/tmpdata1"  --port 27018
报错:
2023-07-20T16:04:56.754+0800 F  STORAGE  [initandlisten] Please read the documentation for starting MongoDB with --repair here: http://dochub.mongodb.org/core/repair
2023-07-20T16:04:56.754+0800 F  -        [initandlisten] Fatal Assertion 50944 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 922
2023-07-20T16:04:56.754+0800 F  -        [initandlisten] \n\n***aborting after fassert() failure\n\n

#使用 --repair 修复一下数据库,修复完成后会自动关闭数据库
root@db-0:~/tmpdata1# mongod --dbpath "/root/tmpdata1"  --port 27018  --repair

#再次正常启动数据库,成功
root@db-0:~/tmpdata1# mongod --dbpath "/root/tmpdata1"  --port 27018
2023-07-20T16:07:00.336+0800 I  NETWORK  [listener] Listening on /tmp/mongodb-27018.sock
2023-07-20T16:07:00.336+0800 I  NETWORK  [listener] Listening on 127.0.0.1
2023-07-20T16:07:00.336+0800 I  NETWORK  [listener] waiting for connections on port 27018

4.5 查询数据

> show dbs
admin   0.000GB
ceshi   0.004GB
config  0.000GB
local   0.000GB
> use ceshi
switched to db ceshi
> db.t1.count()
589
>  db.t1.find().limit(1)
{ "_id" : "xxx", "userId" : 123}
> 

我本地是 MongoDB 4.2环境,发现恢复 MongoDB 6.0 环境的物理文件也是可以的。

标签:rw,20,mongo,--,数据库,Jul,wt,root
From: https://www.cnblogs.com/nanxiang/p/17568709.html

相关文章

  • 使用navicat连接非默认端口的数据库
    使用navicat连接非默认端口的数据库有时候数据库服务器端口不是默认的1521、1433、3306等端口时,我们要如何通过Navicat连接它们呢?很简单,只需要在ip地址后面加“,端口号”即可......
  • mysql主从数据库
    今日配置主从数据库,在配置完成时,对主数据库进行创建库操作,从数据库正常同步。但直接对从数据库进行删库操作后。从数据库的sql能力无法正常进行,但主从数据库的IO通信,没有问题。在重新配置主从数据库后。问题解决。 ......
  • spring boot使用mongodb时,xxxRepository不能Autowired的问题
    默认情况下,当继承MongoRepository的CRUD在@SpringBootApplication的子包下时,xxxRepository是能够自动被扫描和创建代理的。但是如果不在默认路径下,就无法注入了,即使是扫描路径加到了@ComponentScan也一样。解决方法:在springboot启动类中添加@EnableMongoRepositories注解,标注mon......
  • flak创建数据库报 NameError: name 'MySQLdb' is not defined
     因为pycharm中无法安装MySQLdb,安装会报错 所以安装pymysql然后替换MySQLdb  进入到mysqldb.py文件中 替换mysqldb方法,导入pymysql取别名为MySQLdb 再次执行便能成功  ......
  • mysql查询数据库重复数据
    查询重复领取的优惠券//查询8888888用户优惠券状态为未使用且数量大于2的用户领取过的优惠券SELECTdiscount_coupon_id,count(discount_coupon_id)ascFROM`faith_user_discount_coupon`whereuser_id='88888888'and`status`='1'GROUPBYdiscount_coupon_idhavingc>......
  • RuntimeError: Working outside of application context. 创建flask数据库报错
    在执行flask创建数据库时报错 解决办法借鉴如此 修改执行代码执行成功 ......
  • kettle连接mongodb
    Kettle连接MongoDB的实现步骤对于一个刚入行的开发者来说,实现Kettle连接MongoDB可能会有些困惑。下面我将为你详细介绍整个连接过程的步骤,并提供相应的代码示例。步骤概览下面是连接Kettle和MongoDB的整个流程的概览,我们将使用Kettle中的MongoDB输入(MongoDBInput)和输出(MongoDB......
  • mongodb 集群迁移方案
    MongoDB集群迁移方案简介在实际开发中,可能会遇到需要将MongoDB集群迁移到新的环境的情况,本文将介绍一种常见的MongoDB集群迁移方案。迁移流程以下是迁移MongoDB集群的一般步骤:步骤描述1创建新的目标环境2备份源集群数据3在目标环境中配置MongoDB集群4将......
  • mongodb 获取所有数据
    MongoDB获取所有数据MongoDB是一种非关系型数据库,被广泛应用于大数据处理和实时数据分析场景中。在使用MongoDB时,我们经常需要获取数据库中的所有数据。本文将介绍如何使用MongoDB来获取所有数据,并提供相应的代码示例。连接到MongoDB在开始之前,我们需要先连接到MongoDB数据库。......
  • mongodb 的分片
    MongoDB的分片什么是分片?在MongoDB中,分片是指将数据按照某种规则分散存储在多个服务器上的过程。这个过程使得MongoDB可以处理超过单个服务器容量限制的大型数据集。当数据集变得庞大,无法在单个服务器上存储和处理时,我们可以通过分片技术将数据分散存储在多个服务器上,这样可以提......