MongoDB Assertion Error - mongodb

I started seeing this in my mongodb production database logs
ns:my_app_production.artists query:{ $query: {}, $orderby: { semester: 1, name: 1 } }
Wed Jul 25 19:20:59 [conn199] Assertion: 10334:Invalid BSONObj spec size: -286331154 (EEEEEEEE) first element:_id: "agelio-batle"
Wed Jul 25 19:20:59 [conn199] assertion 10334 Invalid BSONObj spec size: -286331154 (EEEEEEEE) first element:_id: "agelio-batle"
first i tried running a repair
mongo --repair
...
Wed Jul 25 22:20:39 [initandlisten] Assertion: 10334:Invalid BSONObj spec size: -286331154 (EEEEEEEE) first element:_id: "agelio-batle"
0x467eaa 0x4183ca 0x62dd82 0x643478 0x532b22 0x64d196 0x6578b7 0x65ac31 0x65cd75 0x65d6d9 0x51a419 0x6195f5 0x61b0c5 0x61bd3d 0x4914c8 0x47ad9a 0x5e2e7c 0x5e60b9 0x5e78ad 0x6346a2
[0x467eaa]
# several more what i assume memory addresses omitted
[0x6346a2]
Wed Jul 25 22:20:39 [initandlisten] assertion 10334 Invalid BSONObj spec size: -286331154 (EEEEEEEE) first element:_id: "agelio-batle" ns:my_app_production.artists query:{}
Wed Jul 25 22:20:39 [initandlisten] exception in initAndListen std::exception: nextSafe(): { $err: "Invalid BSONObj spec size: -286331154 (EEEEEEEE) first element:_id: "a...", code: 10334 }, terminating
Wed Jul 25 22:20:39 dbexit:
...
i also tried running db.repairDatabase(); from the mongo shell with the same results.
I have never seen this before. Typically with mongodb a repair fixes most problems so im not sure how to proceed or troubleshoot this. any ideas?

If an overall repair fails you can mongodump out the individual collections with a --repair option and attempt to isolate the issues. You can even pass queries in to filter out the corrupt data from a corrupted collection, essentially working around the bad data, but it is an incremental and often slow process. This is why it is always recommended to take backups and run in replica sets to avoid the scenarion where you are left with a potentially corrupt data set.
That said, if you have no way to restore from a backup or another replica set member, then you can try something like (with the database shut down):
mongodump --dbpath /path/to/source/data/files --repair --db <dbname> --out /path/to/repaired
If that does not work, then to skip the index read (which might be tripping you up):
mongodump --forceTableScan --dbpath /path/to/source/data/files --repair --db <dbname> --out /path/to/repaired
Thanks to Ren in the comments, the other thing you can try is dropping/rebuilding the index. The table scan option (mongodump walks _id index by default) will avoid using the indexes for the dump. So assuming you get the path and the database names right, the second option should work.

Related

how to remove mongodb collection if mongod doesn't start cause of broken collection?

I can't start mongod cause of broken collection, mongod.log says:
2018-01-06T09:32:19.089+0000 I INDEX [initandlisten] found 2
index(es) that wasn't finished before shutdown
2018-01-06T09:32:19.089+0000 I - [initandlisten] Fatal assertion
28579 UnsupportedFormat: Unable to find metadata for table:index-5155-
3080678491732636775 Index: {name: _id_, ns: gozazu.phones_10759} -
version too new for this mongod. See
http://dochub.mongodb.org/core/3.4-index-downgrade for detailed
instructions on how to handle this error. at
src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp 241
2018-01-06T09:32:19.089+0000 I - [initandlisten]
***aborting after fassert() failure
Command mongod --repair doesn't help. How can i remove or fix this collection if mongo shell can't connect to mongod?

mongodb - Dump restore throws error

I'm getting this below error while restoring dump into mongodb,
Mon Jul 27 14:08:52.936 going into namespace [test.BC_2022_tmp]
Mon Jul 27 14:08:52.936 warning: Restoring to viacrm.BC_2022_tmp without dropping. Restored data will be inserted without raising errors; check your server log
Mon Jul 27 14:08:52.937 file /home/dev/test/BC_2022_tmp.bson empty, skipping
Mon Jul 27 14:08:52.937 Creating index: { key: { _id: 1 }, ns: "viacrm.BC_2022_tmp", name: "_id_" }
Mon Jul 27 14:08:52.938 ERROR: Error creating index test.BC_2022_tmp: 13347 err: "local.oplog.rs missing. did you drop it? if so restart server"
Abandon (core dumped)
Dump Restore command:
mongorestore -d test /home/dev/crm
Please can anyone help me to resolve this issue

how to set mongod --dbpath

very new to mongodb and databases in general. whenever i run mongo i receive this error message: ​​​
MongoDB shell version: 2.4.9
connecting to: test
Thu Jan 30 13:03:33.170 Error: couldn't connect to server 127.0.0.1:27017
at src/mongo/shell/mongo.js:145
exception: connect failed
running mongod i see this:
Thu Jan 30 13:13:36.588 [initandlisten] MongoDB starting : pid=29408 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Kimis-MacBook-Air-2.local
Thu Jan 30 13:13:36.588 [initandlisten]
Thu Jan 30 13:13:36.588 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Thu Jan 30 13:13:36.588 [initandlisten] db version v2.4.9
Thu Jan 30 13:13:36.588 [initandlisten] git version: nogitversion
Thu Jan 30 13:13:36.588 [initandlisten] build info: Darwin minilionvm.local 11.4.2 Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Thu Jan 30 13:13:36.588 [initandlisten] allocator: tcmalloc
Thu Jan 30 13:13:36.588 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb" }
Thu Jan 30 13:13:36.591 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Thu Jan 30 13:13:36.591 [initandlisten] recover begin
Thu Jan 30 13:13:36.591 [initandlisten] recover lsn: 108155770
Thu Jan 30 13:13:36.591 [initandlisten] recover /usr/local/var/mongodb/journal/j._0
Thu Jan 30 13:13:36.591 [initandlisten] journal file version number mismatch got:4147 expected:4149. if you have just upgraded, recover with old version of mongod, terminate cleanly, then upgrade.
Thu Jan 30 13:13:36.592 [initandlisten] dbexception during recovery: 13536 journal version number mismatch 16711
Thu Jan 30 13:13:36.592 [initandlisten] exception in initAndListen: 13536 journal version number mismatch 16711, terminating
Thu Jan 30 13:13:36.592 dbexit:
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close listening sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to flush diaglog...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: going to close sockets...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: waiting for fs preallocator...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: lock for final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: final commit...
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: closing all files...
Thu Jan 30 13:13:36.592 [initandlisten] closeAllFiles() finished
Thu Jan 30 13:13:36.592 [initandlisten] shutdown: removing fs lock...
Thu Jan 30 13:13:36.592 dbexit: really exiting now
when i manually set my mongo dpath to mongod --dbpath /data/db (as should be the default on installation) and keep it running in terminal everything runs fine. but once i close it, everything breaks again. my question is:
why is my dbpath set to /usr/local/var/mongodb?
how do i fix this error so mongo works on my machine?
i'm assuming i either need to permanently set the dbpath to /data/db or reconfigure something so it works with the dbpath as /usr/local/var/mongodb
i fairly new to unix commands as well so not entirely sure how to fix this error.
thank you for any suggestions!
so, digging through the mongodb documentation i read this:
"Unless specified, mongod will look for data files in the default /data/db directory. (Windows systems use the \data\db directory.) If you installed using a package management system. Check the /etc/mongodb.conf file provided by your packages to see the configuration of the dbpath."
when i took a look at the files within /etc/ there is no mongodb.conf file... however, i do see /usr/local/etc/mongod.conf... so if i understand this correctly, i should create a file called /etc/mongodb.conf and set the dbpath to /data/db.
do i need to delete the stuff within /usr/local/ as well?
i think this has something to do with how my PATH are setup... could someone explain to me how to fix this in unix so i won't have this problem?
thanks again!
Have only tried this on Mac:
Create a data directory in the root folder of your app
cd into your wherever you placed your mongo directory when you installed it
run this command:
mongod --dbpath ~/path/to/your/app/data
You should be good to go!
You can set dbPath in the mongodb.conf file:
storage:
dbPath: "/path/to/your/database/data/db"
It's a YAML-based configuration file format (since Mongodb 2.6 version), so pay attention no tabs only spaces, and space after ": "
usually this file located in the *nix systems here: /etc/mongodb.conf
So then just run
$ mongod -f /etc/mongodb.conf
And mongod process will start...
(on the Windows something like)
> C:\MongoDB\bin\mongod.exe -f C:\MongoDB\mongod.conf
First you will have a config file in /etc/mongodb.conf, therefore this sounds like a homebrew install which will use some more standardized paths. The whole /data/db/ thing is referenced in a lot of manual install documentation.
So basically from your log the server is not running, it's shutting down, so there is nothing for the shell to connect to. Seems like you have had some unclean shutdowns/restarts which has led to the inconsistency.
Clear the files in the journal /usr/local/var/mongodb/journal/ on your config.
Also:
sudo rm /var/lib/mongodb/mongod.lock
Just in case, even though that part looks clean. And then restart.
For me it must have:
mongod --dbpath=/whatever/data/path
Create a directory db in home, inside db another directory data
cd
mkdir db
cd db
mkdir data
then type this command--
mongod --dbpath ~/db/data
mongod --port portnumber --dbpath /path_to_your_folder
By default portnumber is 27017 and path is /var/lib/mongodb
You can set your own port number and path where you want to keep all your database.
You could also configure mongod to run on start up so that it is automatically running on start up and the dbpath is set upon configuration. To do this try:
mongod --smallfiles --config /etc/mongod.conf
The --smallfiles tag is there in case you get an error with size. It is, of course, optional. Doing this should solve your problem while also automating your mongodb setup.
sudo mongod --dbpath ~/data
This made it work for me.
very simple:
sudo chown mongodb:mongodb /var/lib/mongodb/mongod.lock
Scenario: MongoDB(version v4.0.9).
Set custom folder(with name: myCustomDatabases), where to store databases.
In custom folder(with name: myCustomDatabases), have to create database (with name: newDb).
Resolve:
Create custom folder(with name: myCustomDatabases):
D:>md myCustomDatabases
Run 'mongod --dbpath' with path to custom folder(with name: myCustomDatabases):
mongod --dbpath "D:\myCustomDatabases"
From another 'cmd' run 'mongo':
D:>mongo
3.1. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
3.2. Use database with name newDb:
> use newDb
switched to db newDb
3.3. Show all databases, stored in custom folder(with name: myCustomDatabases):
>show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
!!! Noticed, that newDb is NOT in the list !!!
3.4. Have to create a collection with a document,
which will create the database newDb.
> db.Cats.insert({name: 'Leo'})
WriteResult({ "nInserted" : 1 })
The insert({name: 'Leo'}) operation creates:
the database newDB and the collection Cats, because they do not exist.
3.5. Now the new created database newDb will be displayed in the list.
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
newDb 0.000GB
3.6. Now in custom folder D:\myCustomDatabases, have database newDb.
Windows environment, local machine.
I had an error
[js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException:
Error connecting to 127.0.0.1:27017 :: caused by ::
No connection could be made because the target machine actively refused it. :
After some back and forth attempts I decided
to check Windows "Task Manager". I noticed that MongoDB process is stopped.
I made it run. Everything starts working as expected.
You can use --dbpath flag in order to provide it with the parameters i.e, the directory address. I found it useful for Windows 10 OS.
With the default settings it is usually not required.
Go to the directory you would like your data to be saved in.
Create a folder called "mongodb" and within it create another directory named "data"
Navigate back to the "mongodb" directory in the terminal window and place the following line of mongod --dbpath=data --bind_ip 127.0.0.1
Run this command each time you want to start up the mongodb database
With the terminal command mongod --dbpath /YOUR/PATH/TO/DATA/ I need to type it everytime I want to start MongoDB. Somehow setting it in the mongo.conf didn't load the path properly. I found a solution to avoid having to type the long command every time by adding an alias in ~/.zshrc
Shut down MongoDB (while mongo is running type in Terminal: use admin press enter and on a next line db.shutdownServer().
Close the Terminal window.
Open ~/.zshrc or when your macOS is using bash-shell open ~/.bash_profile
Add in the file: alias -g mongod="MongoDB --dbpath /YOUR/PATH/TO/DATA/
Save your .zshrc or .bash_profile file
Open Terminal
Type source ~/.zshrc or for bash-shell users: source ~/.bash_profile
Type mongod
MongoDB should now be running with the right path and connection. Terminal can be closed now. To check whether mongo is running open Terminal, type mongo which gives you the interface in Terminal or type top and check mongo in the list of applications.

MongoDB: How to remove an index on a replicaset?

I see that the MongoDB documentation says that removing index is by calling db.accounts.dropIndex( { "tax-id": 1 } ). But it does not say whether the node needs to be removed from the replicaset or not.
I tried to take a secondary node in a replicaset offline and restart as a standalone node (in a different port) and tried to drop the index.
But after bringing back the node in the replica set with regular process sudo service mongod start, the mongod process is dying saying the index got corrupted.
Thu Oct 31 19:52:38.098 [repl writer worker 1] Assertion: 15898:error in index possibly corruption consider repairing 382
0xdddd81 0xd9f55b 0xd9fa9c 0x7edb83 0x7fb332 0x7fdc08 0x9d3b50 0x9c796e 0x9deb64 0xac45dd 0xac58df 0xa903fa 0xa924c7 0xa71f6c 0xc273d3 0xc26b18 0xdab721 0xe26609 0x7ff4d05f0c6b 0x7ff4cf9965ed
/usr/bin/mongod(_ZN5mongo15printStackTraceERSo+0x21) [0xdddd81]
/usr/bin/mongod(_ZN5mongo11msgassertedEiPKc+0x9b) [0xd9f55b]
/usr/bin/mongod() [0xd9fa9c]
/usr/bin/mongod(_ZN5mongo11checkFailedEj+0x143) [0x7edb83]
/usr/bin/mongod(_ZNK5mongo12BucketBasicsINS_12BtreeData_V1EE11basicInsertENS_7DiskLocERiS3_RKNS_5KeyV1ERKNS_8OrderingE+0x222) [0x7fb332]
/usr/bin/mongod(_ZNK5mongo11BtreeBucketINS_12BtreeData_V1EE10insertHereENS_7DiskLocEiS3_RKNS_5KeyV1ERKNS_8OrderingES3_S3_RNS_12IndexDetailsE+0x68) [0x7fdc08]
/usr/bin/mongod(_ZNK5mongo30IndexInsertionContinuationImplINS_12BtreeData_V1EE22doIndexInsertionWritesEv+0xa0) [0x9d3b50]
/usr/bin/mongod(_ZN5mongo14IndexInterface13IndexInserter19finishAllInsertionsEv+0x1e) [0x9c796e]
/usr/bin/mongod(_ZN5mongo24indexRecordUsingTwoStepsEPKcPNS_16NamespaceDetailsENS_7BSONObjENS_7DiskLocEb+0x754) [0x9deb64]
/usr/bin/mongod(_ZN5mongo11DataFileMgr6insertEPKcPKvibbbPb+0x123d) [0xac45dd]
/usr/bin/mongod(_ZN5mongo11DataFileMgr16insertWithObjModEPKcRNS_7BSONObjEbb+0x4f) [0xac58df]
/usr/bin/mongod(_ZN5mongo14_updateObjectsEbPKcRKNS_7BSONObjES4_bbbRNS_7OpDebugEPNS_11RemoveSaverEbRKNS_24QueryPlanSelectionPolicyEb+0x2eda) [0xa903fa]
/usr/bin/mongod(_ZN5mongo27updateObjectsForReplicationEPKcRKNS_7BSONObjES4_bbbRNS_7OpDebugEbRKNS_24QueryPlanSelectionPolicyE+0xb7) [0xa924c7]
/usr/bin/mongod(_ZN5mongo21applyOperation_inlockERKNS_7BSONObjEbb+0x65c) [0xa71f6c]
/usr/bin/mongod(_ZN5mongo7replset8SyncTail9syncApplyERKNS_7BSONObjEb+0x713) [0xc273d3]
/usr/bin/mongod(_ZN5mongo7replset14multiSyncApplyERKSt6vectorINS_7BSONObjESaIS2_EEPNS0_8SyncTailE+0x48) [0xc26b18]
/usr/bin/mongod(_ZN5mongo10threadpool6Worker4loopEv+0x281) [0xdab721]
/usr/bin/mongod() [0xe26609]
/lib64/libpthread.so.0(+0x7c6b) [0x7ff4d05f0c6b]
/lib64/libc.so.6(clone+0x6d) [0x7ff4cf9965ed]
Thu Oct 31 19:52:38.106 [repl writer worker 1] ERROR: writer worker caught exception: error in index possibly corruption consider repairing 382 on:
xxxxxxxx--deleted content related to the data...xxxxxxxxxxxxx
Thu Oct 31 19:52:38.106 [repl writer worker 1] Fatal Assertion 16360
0xdddd81 0xd9dc13 0xc26bfc 0xdab721 0xe26609 0x7ff4d05f0c6b 0x7ff4cf9965ed
/usr/bin/mongod(_ZN5mongo15printStackTraceERSo+0x21) [0xdddd81]
/usr/bin/mongod(_ZN5mongo13fassertFailedEi+0xa3) [0xd9dc13]
/usr/bin/mongod(_ZN5mongo7replset14multiSyncApplyERKSt6vectorINS_7BSONObjESaIS2_EEPNS0_8SyncTailE+0x12c) [0xc26bfc]
/usr/bin/mongod(_ZN5mongo10threadpool6Worker4loopEv+0x281) [0xdab721]
/usr/bin/mongod() [0xe26609]
/lib64/libpthread.so.0(+0x7c6b) [0x7ff4d05f0c6b]
/lib64/libc.so.6(clone+0x6d) [0x7ff4cf9965ed]
Thu Oct 31 19:52:38.108 [repl writer worker 1]
***aborting after fassert() failure
Thu Oct 31 19:52:38.108 Got signal: 6 (Aborted).
Is this due to dropping the index in the offline mode on the secondary? Any suggestions on the proper way to drop the index is highly appreciated.
The proper way to remove index from replica set is to drop it on primary. The idea of replica is having the same copy of data (with small time lags). So whenever you do something on primary is copied to the secondaries. So if you start doing anything on the primary, right after it finishes this process, the process propagates to secondaries.
If you are removing index from primary - the index will be removed on the secondary as well.

Cannot restart mongodb on Ubuntu 11.04

I have installed mongodb and when i typing the following command i am getting these messages
root#amila:~# service mongodb status
mongodb stop/waiting
root#amila:~# service mongodb restart
restart: Unknown instance:
root#amila:~# service mongodb stop
stop: Unknown instance:
I want to restart mongodb.but unable to restart.
EDIT:
here is the /etc/mongodb.conf
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 127.0.0.1
auth = true
Given the output of the start command, this is starting correctly. When you are running status, it seems to have been stopped. You need to look into the log file:
/var/log/mongodb/mongodb.log
That will tell you why the mongod is stopping. There are a couple of likely causes, but without seeing that output it will be hard to answer definitively. My general recommendations:
Do not use bind_ip - it is a bad idea in general Update (2016): Removing this because the issues with bind_ip that caused me to write this back in 2012 have been fixed, and it is even on by default in official packages now. Worth trying without the setting as a troubleshooting step, but not a bad idea to use it in general.
Check for something else running on port 27017
Specify the port explicitly in the mongodb.conf file, even if you want to use the default
Finally, take a look at this answer for how to fix file permissions, just in case that is a problem (usually caused by running as root at some point):
mongodb crashes after unexpected shutdown
Sorry if you consider this "SO archeology".
I don't know if this is the same issue, but I'm facing something similiar right now.
End of my log:
Mon Nov 19 10:25:37 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Mon Nov 19 10:25:37 dbexit:
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to close listening sockets...
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to flush diaglog...
Mon Nov 19 10:25:37 [initandlisten] shutdown: going to close sockets...
Mon Nov 19 10:25:37 [initandlisten] shutdown: waiting for fs preallocator...
Mon Nov 19 10:25:37 [initandlisten] shutdown: lock for final commit...
Mon Nov 19 10:25:37 [initandlisten] shutdown: final commit...
Mon Nov 19 10:25:37 [initandlisten] shutdown: closing all files...
Mon Nov 19 10:25:37 [initandlisten] closeAllFiles() finished
Mon Nov 19 10:25:37 dbexit: really exiting now
So I think the lock file is not closed properly (for example when shutting down). You should use
mongod --repair
But it didn't work for me, so I just removed /var/lib/mongodb/mongod.lock and then started the server. This is not the secure way, and there is possibility, that data in your database is corrupt if you do so.
Sorry to rehash this thread, but I'm putting this here because I ran into the exact same issue and this worked.
taken from here:
http://pastebin.com/6hP42r69
sudo -u mongodb mongod --repair -dbpath /var/lib/mongodb
I'm running linux mint 15 and current mongodb build.
I had the same problem and I solved running just:
$ service mongod restart
I solved this manually deleting .lock file like that
sudo rm -i /var/lib/mongodb/mongod.lock
then restart
Looks like Mongo is unable to start for some reason. Apart from the suggestions already provided by Adam.
Check to see if you have write permissions to the data directory.
Check to see if you have write permissions to the /var/log directory.
Is mongod running as root or as another user? Check the init script for mongodb
to see what user is mongod running as.
One of the possible cases might be mongod.lock(which was causing my problem).
Open your log file at
/var/log/mongodb/mongod.log
See if you have any exception because of mongod.lock file. Copy it's location and remove it
sudo rm /var/lib/mongodb/mongod.lock
Then run
sudo service mongod restart
To check if everything is fine now.
sudo service mongod status
Expected output will be mongod start/running
Sometimes the problems can be caused by not having enough free storage for journaling to continue smoothly.
Did a tail -100 /var/log/mongodb/mongod.log and found this error:
exception in initAndListen: 15926 Insufficient free space for journals, terminating
Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles`
Resolved it by putting smallfiles=true in my MongoDB config file /etc/mongod.conf.
Then sudo rm /var/lib/mongodb/mongod.lock
And then restarting the MongoDB instance using service mongod start
Hope this help.