mongodb 误删某个数据库文件夹, 导致数据库无法启动处理

安装必要的库

1
2
apt install libsnappy-dev
apt install zlib1g-dev

安装 wiredtiger 工具包

1
2
3
./configure --enable-snappy --enable-zlib --prefix=/usr/local/wiredtiger
make -j8
make install

查看库名

1
wt -v -h /data/mongodb/db/ -C "extensions=[/usr/local/wiredtiger/lib/libwiredtiger_snappy.so, /usr/local/wiredtiger/lib/libwiredtiger_zlib.so]" list

dump 出某个 collection 的数据

1
wt -v -h /data/mongodb/db/ -C "extensions=[/usr/local/wiredtiger/lib/libwiredtiger_snappy.so, /usr/local/wiredtiger/lib/libwiredtiger_zlib.so]" -R dump -f test.dump test/collection/304--4723424148269970486

导入另外一个实例

在另外一个可启动的实例 foo 中创建数据库和集合,这个集合为临时集合,作为导入导出数据使用

1
2
3
4
5
6
root@test:~# tree /data/mongodb-27017/
/data/mongodb-27017/
└── db
├── test
│   ├── collection
│   │   └── 0--7048601165654010388.wt

导出元数据,并导入到 foo 中,获取 物理文件和 collection 的对应关系

1
2
3
4
5
6
7
wt -v -h /data/mongodb/db/ \
-C "extensions=[/usr/local/wiredtiger/lib/libwiredtiger_snappy.so, /usr/local/wiredtiger/lib/libwiredtiger_zlib.so]" \
-R dump -f _mdb_catalog.dump table:_mdb_catalog

wt -v -h /data/mongodb-27017/db/ \
-C "extensions=[/usr/local/wiredtiger/lib/libwiredtiger_snappy.so, /usr/local/wiredtiger/lib/libwiredtiger_zlib.so]" \
-R load -f ./_mdb_catalog.dump -r test/collection/7--7048601165654010388

清理掉元数据后,将要恢复的数据数据导入

1
2
3
wt -v -h /data/mongodb-27017/db/ \
-C "extensions=[/usr/local/wiredtiger/lib/libwiredtiger_snappy.so, /usr/local/wiredtiger/lib/libwiredtiger_zlib.so]" \
-R load -f ./1.dump -r test/collection/0--7048601165654010388

需要将要恢复的数据导入再导出,集合信息才能显示正常

1
/usr/local/mongodb/3.6/bin/mongodump --archive --db test | /usr/local/mongodb/3.6/bin/mongorestore --archive --nsFrom "test.*" --nsTo "test_reimport.*"