MongoDB备份含索引、集合但无文档,新手求助排查原因
Hey there, let's figure out why your mongodump is generating empty .bson files (only .metadata.bson has content) and fix the data loss during restore. Here are the most common causes and fixes tailored to your Windows 7 environment:
1. MongoDB服务未运行(最常见原因)
mongodump needs to connect to a running MongoDB instance to read data. If the service isn't started, it can only grab database metadata, not actual documents.
- Fix steps:
- Press
Win + R, typeservices.mscand hit Enter to open the Services Manager - Look for the service named
MongoDB(might include the version, likeMongoDB Server 4.0) - If it shows "Stopped", right-click and select "Start"; it's better to set it to "Automatic" startup to avoid this issue later
- Press
2. Missing explicit connection parameters (for non-default instances)
If your MongoDB instance isn't using the default localhost:27017 port or has authentication enabled, mongodump might fail to connect properly, resulting in no data being read.
- Modified script example (specify host/port explicitly; add auth params if needed):
cd "C:\Program Files\MongoDB\Server\4.0\bin\" # For default local instance; add --username yourUser --password yourPass if authentication is on mongodump.exe --host localhost:27017 --db aya --out e:\mongodb
3. The aya database actually has no documents
First double-check if your aya database really contains data:
- Open the MongoDB shell (run
mongo.exein the same bin directory) - Type
use ayato switch to the target database - Run
db.getCollectionNames()to list all collections, then check each one withdb.yourCollectionName.find().count()(MongoDB 4.0 syntax) to confirm if documents exist
4. Permissions issue with the output directory
Even though files are generated, insufficient read/write permissions on e:\mongodb in Windows 7 might prevent mongodump from writing actual data:
- Right-click the
e:\mongodbfolder, select "Properties" → "Security" tab - Make sure your current user has "Write" and "Modify" permissions; add them if missing
5. Test backup for a single collection to narrow down issues
If all above checks pass, try backing up a single collection to see if it generates a non-empty .bson file:
cd "C:\Program Files\MongoDB\Server\4.0\bin\" mongodump.exe --db aya --collection yourCollectionName --out e:\mongodb
If this works, there might be an issue with a specific collection in your full database backup.
Note for restore
When restoring, make sure to use the correct mongorestore command and specify the target database:
cd "C:\Program Files\MongoDB\Server\4.0\bin\" mongorestore.exe --db aya e:\mongodb\aya
内容的提问来源于stack exchange,提问作者BenYsil

