When I am saving documents in monogdb, getting the following error
"{WriteConcern detected an error 'bad offset:0 accessing file:xxxx - consider repairing database'. (Response was { \"err\" : \"bad offset:0 accessing file: xxxxx - consider repairing database\", \"code\" : 13440, \"n\" : 0, \"connectionId\" : 13, \"ok\" : 1.0 }).}" any help please
11 Answer
As the error states, the database requires repairing because it is in an inconsistent state due to whatever operation that caused unexpected shutdown.
In MongoDB WriteConcern provides feedback/acknowledgement in response to a write operation. There are multiple levels of WriteConcerns or guarantees that on whether the write operation was successful or not. The WriteConcerns levels are as follow:
- Unacknowledged (the client does not wait for acknowledgement of write op)
- Acknowledged (client waits for success or exception ack. of write op)
- Journaled (mongodb sends acknowledgment only after committing the write to journal)
- Replica Acknowledged (ack. of write op on primary and/or other members of replica set)
You can set the levels of WriteConcerns to any level depending on importance of write operation.
The WriteConcern in your case returns an exception stating that database is in an inconsistent state may/may not data might have been corrupted. In order to get back to operational mode, you need to repair the database in one of many ways.
- Via Mongodb shell (if you can connect to MongoDb)
- Via command line with
mongodsee below
If you can get into MongoDB shell, then you need to find the database and repair it as follow
use dbName db.repairDatabase() //repairs the above database If you cannot get into MongoDB shell or wish to use other method then you can use the following methods
Repair Data files and preserve original files
mongod --dbpath /data/db --repair --repairpath /data/db0 mongod --dbpath /data/db0 Once the first command above complete, the newly repaired data will be under /data/db0 directory and you can use the second command to start mongodb using repaired data. The original data files are preserved in the default /data/db or a custom location (if not default)
Repair Data files WITHOUT preserve original files
This method you only supply --repair and no --repathpath and it will attempt to repair the original data files. First you have to delete mongod.lock file, steps are as follow:
rm /data/db/mongod.lock Assuming your data is in /data/db directory, if not specify the location.
mongod --dbpath /data/db --repair
Start mongod with only --repair option, you don't have to pass --repairpath option. This will attempt to repair the data. Then finally start the database
mongod --dbpath /data/db
If you follow the first method of repair then you can delete the mongod.lock after the repair is successful. If you follow the second method then you can delete he mongod.lock before performing the repair operation.
Read the documentation how to safely shutdown the database, what is in mongod.lock? How you should do the repair (for example running it with same user in order to preserve the file ownership). It is also possible that the permissions to your data files has changed, you can chown them back, see this question for more details.