Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The whole process of MongoDB database file damage and data recovery

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

one。 Fault description

The customer device environment is a server with an operating system of Windows Server 2008 and deploys the MongoDB database. Due to business needs, the customer copied the database files without shutting down the MongoDB database service. After copying the database file to another partition, the customer formats the original database partition, then copies the database file back to the original partition and restarts the MongoDB service. At this time, the customer finds that the service cannot be started. The error is as follows:

Figure 1:

two。 Fault detection

Generally speaking, when the service is not turned off, copy the MongoDB database file directly. Since the service is still running, it will cause errors in the copy of mongod.lock file and WiredTiger.lock file. Delete these two files in the copied file at this time, start the service again, and let MongoDB regenerate itself.

However, through the on-site inspection of the files copied by the customer, it is found that the _ mdb_catalog.wt file is missing in the database file copied by the customer.

The metadata of all collections in MongoDB is stored in the mdb_catalog.wt file, and the relevant information needs to be read from this file when the database starts. Due to the loss of this file, the database cannot obtain metadata such as the name of the collection, the creation options of the collection, the index information of the collection, and so on, and the database cannot be started.

three。 Data recovery

First we try to restore the _ mdb_catalog.wt file from a file system point of view.

Use professional data recovery software to scan the database partition to view the scan results, and there is no information about the _ mdb_catalog.wt file. According to the eigenvalues of the data file in the MongoDB database, the database partition is scanned, and the data area related to _ mdb_catalog.wt is not found. Judging from this, the _ mdb_catalog.wt file has been completely overwritten and destroyed and cannot be recovered.

At this time, we can only find a way to extract the data from the database level.

The MongoDB database deployed by the customer is a database system based on the WT storage engine, so we can use the WT utility kit to extract the data from the database.

First download the WT utility kit, and then compile the executable wt tool under the windows environment.

Figure 2:

After the compilation is completed, the wt tool is used to clean the data in the collection file of the database. After completion, the data in the file is read directly and written into a dump file.

At this time, we have extracted all the available data from the various collection files of the database, and the next step is to restore the database environment.

Recreate a MongoDB database, create a corresponding number of empty collections according to the extracted collection files, and then use the wt tool to write the extracted dump files to the newly created empty collections. At this time, we can query the data in the collection, confirm the corresponding relationship between these sets and the collection in the meta-database, modify the name of the collection, and rebuild the index information. at this point, the data recovery is basically completed.

After querying the records in the collection to determine the record type, and then determining the location of the fs.files and fs.chunks collections, and changing the names of the two collections to xxx.files and xxx.chunks, the index of the collection is rebuilt, and the collection is restored. You can view the data in the collection normally:

Figure 3:

Figure 4:

four。 Data verification

After assisting the customer to rebuild the index of the whole collection, the customer queries and verifies the database as a whole, the data is correct, and the data recovery is a complete success.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report