最近,一客户单实例mongodb数据库,没有备份的情况下遇到了断电导致的数据文件损坏,由于客户业务需要
及数据的不敏感性,要求尽快恢复业务,使用了Mongdb的自动修复repair命令进行修复。可喜的是,帮助用户尽
快恢复了服务,可悲的是在客户可接受情况下相关数据文件内的数据丢失。这里,对这一过程做个总结,同时说明
repair后为什么数据丢失。
-
正常的mongodb数据查询
> show dbs;
admin 0.000GB
config 0.000GB
dns_testdb 0.009GB
local 0.000GB
> use dns_testdb
switched to db dns_testdb
> db.test_collection.find();
{ "_id" : ObjectId("5fedd03d9d2569ee04ab62e1"), "name" : "elephant", "user_id" : 0, "boolean" : false, "added_at" : ISODate("2020-12-31T13:21:01.226Z"), "number" : 5129 }
{ "_id" : ObjectId("5fedd03d9d2569ee04ab62e2"), "name" : "dog", "user_id" : 1, "boolean" : false, "added_at" : ISODate("2020-12-31T13:21:01.237Z"), "number" : 9699 }
{ "_id" : ObjectId("5fedd03d9d2569ee04ab62e3"), "name" : "lion", "user_id" : 2, "boolean" : false, "added_at" : ISODate("2020-12-31T13:21:01.238Z"), "number" : 1783 }
Type "it" for more
>
2.模拟数据文件损坏
[mongo@centos7 dns_testdb]$ du -sh *
28M collection-8--6736947369024546614.wt
9.5M index-9--6736947369024546614.wt
[mongo@centos7 dns_testdb]$
[mongo@centos7 dns_testdb]$
[mongo@centos7 dns_testdb]$ pwd
/opt/mongo/data/single/dns_testdb
[mongo@centos7 dns_testdb]$ dd if=/dev/null of=/opt/mongo/data/single/dns_testdb/collection-8--6736947369024546614.wt bs=1024k count=5
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000132203 s, 0.0 kB/s
[mongo@centos7 dns_testdb]$
3.重新启动mongodb
> use admin
switched to db admin
> db.shutdownServer();
[mongo@centos7 data]$ mongod --dbpath /opt/mongo/data/single --port 50001 --oplogSize 5