WriteConcern detected an error in mognodb - mongodb

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

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 mongod see 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.

Related

Recovering mongodb `--dbpath`

I used MongoDB a month ago to run a simple web app to collect data for my research. After some inactivity (during which I did not run MongoDB), I forgot the parameter dbpath. Now, whenever I launch mongod on my Mac, I get the error, which I understand:
Exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
I have tried:
Reading mongodb.conf (apparently that's not the correct path; I think it got reset.)
Looking into all my terminal history.
I do know the name of the collection and the database. And that my database was password protected.
I have already read most of the answers including this:
Forgot mongodb data path

Mongorestore writing to MongoDB datafiles

MongoDB documentation has the following line. What do they mean when they say, mongorestore can directly write to MongoDB data files without an active mongod? Does it mean it can write even if a mongod instance is not ready and reachable, or, is there something called inactive mongod instance. I am finding it difficult to understand. Can someone explain me this?
mongorestore can write data to either mongod or mongos instances, in addition to writing directly to MongoDB data files without an active mongod.
Prior to v3.x mongorestore could be configured to write directly to data files rather than writing into data files via mongod.
The term "data files" here refers to the contents of a dbpath directory so, by specifying --dbpath you could tell mongorestore to attach to the data files in that directory and insert data into those files without having to go through a mongod instance.

Starting mongod with --nojournal option deletes existing journal files?

My MongoDB had crashed due to out of memory error that occurred when it tried appending to a journal file.
At that instance, my mongod.lock file was empty. I restarted mongod without any options. It was accepting connections normally. Then I ran mongo.exe, but was unable to connect to db. It got stuck to "connecting to test" but never connected successfully.
I ended that process and I restarted mongod with --nojournal option. But that didnt help either.
But now I see mongod.lock file non empty. Also,all my journal entries are deleted.
The question is, does --noJournal option deletes existing journal entries? Also, is there a way to recover the journal entries?
Recovering after a crash
First, please read this article:
Recover Data after an Unexpected Shutdown
After a crash, you have two options:
if it is a standalone instance, run mongod with the --repair option;
if the instance is a part of a replica set, wipe all data and either restore from a backup or perform an initial sync from another replica set member.
The --nojournal option
Running mongod --nojournal will not remove journal files. In fact, mongod will not even start if there are journal files present. It will give you the following message and shut down.
Error: journal files are present in journal directory, yet starting without journaling enabled.
It is recommended that you start with journaling enabled so that recovery may occur.
exception in initAndListen: 13597 can't start without --journal enabled when journal/ files are present, terminating
If you then run mongod without the --nojournal option, it will apply all changes saved in journal files and remove the files. Only then can you restart it with --nojournal.
I believe this is what happened in your case. You don't need to attempt to restore your journal files, as they are already applied to your data.

Connection to MongoDB fails

I tried to connect to a local MongoDB instance, but it failed with error message Failed to connect.
I have run the commands shown in the below screenshots. The error message is shown there, too.
My folder structure is:
D:/mongodb/bin
D:/mongodb/data/db
D:/mongodb/log
I am using 32-bit Windows7.
Your data directory doesn't exist where MongoDB is expecting it to be.
For simplicity you can create D:/data/db, which is the default location and where mongod is looking at the moment, and use this as your data directory.
Alternatively if you want to use D:/mongodb/data/db as the data directory you'll need to specify the dbPath when starting mongod
mongod --dbpath D:/mongodb/data/db
This can also be done in a configuration file.

Data storage in mongodb

first of all please forgive me for asking a silly question but I am new to mongodb and just installed it on my windows platform by following this installation guide :http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
It says "MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is C:\data\db.You can specify an alternate path for data files using the --dbpath option to mongod.exe."
So I created a folder d://data/db in my computer and issued a command
C:\mongodb\bin\mongod.exe --dbpath d:\mongodb\data
Then it says
"At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
db.test.save( { a: 1 } )
db.test.find()"
I issued this to commands to save and retrieve the objects and its working fine but what is this default test database? where is it? Moreover where this object is stored? Where I can find this file?
what is this default test database?
When you connect to a mongod server without specifying a database, a default database "test" is selected. Since databases are created lazily, it may not even exist until you write to it.
db.test.save( { a: 1 } )
After this line is executed, database with current name ("test" by default) is created (if didn't exist already) and in it, collection "test" is created (if didn't exist already).
where is it? Moreover where this object is stored? Where I can find this file?
All databases are ultimately stored as files in your data dir. Look for "test.*" files there.
mongod.lock, is the file which provides the PID of your running mongod instance. When you start a mongod instance, MongoDB check if the lock is empty to start cleanly mongod. Then MongoDB registered the PID number of the running mongod instance in this lock file.
MongoDB delete the contains of this lock file when you shutdown cleanly your server,
mongod --shutdown -- dbpath <path name> --port <port number>