Can't connect to mongodb locally even though mongod is running - mongodb

I am unable to connect to my mongo database via external APIs or the mongo client locally, but can connect without issues from a remote host.
I have started mongod and can confirm that the server is running by visiting 127.0.0.1:27017
I have followed this answer with no luck
I have tried creating an entirely new database directory for mongo to generate a new set of database files, still no luck - so it's not a lock file or permissions issue.
$ mongo --verbose
MongoDB shell version: 2.4.4
Mon Jun 24 20:11:08.764 versionArrayTest passed
connecting to: test
Mon Jun 24 20:11:08.847 creating new connection to:127.0.0.1:27017
Mon Jun 24 20:11:08.847 BackgroundJob starting: ConnectBG
Mon Jun 24 20:12:11.848 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
Mon Jun 24 20:12:11.848 User Assertion: 12513:connect failed
exception: connect failed
I am running Ubuntu 12.04, I have installed mongo from the 10gen repo. Everything was running flawlessly originally, but after restarting the server, mongo fails to connect.
My netstat returns:
$ netstat -nap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN -

I gotten the same problem on CentOs 6.4 (Mongo 2.4.5, but actually it happens in other versions too). There is weird threading issue. I debugged mongo and found workaround. It is required mongo recompile. It looks like timed_wait method from boost library returns timeout when spurious wake up happened. I am not sure where bug is: mongo, boost, glibc, linux kernel.
Mongo 2.4.5
Replace in src/mongo/util/background.cpp method BackgroundJob::wait
bool BackgroundJob::wait( unsigned msTimeOut ) {
verify( !_status->deleteSelf ); // you cannot call wait on a self-deleting job
scoped_lock l( _status->m );
boost::system_time const endTime = boost::get_system_time()+ boost::posix_time::milliseconds(msTimeOut);
if ( msTimeOut ) {
while ( _status->state != Done ) {
if ( ! _status->finished.timed_wait( l.boost() , endTime ) ){
boost::system_time const curTime=boost::get_system_time();
if((curTime - endTime).total_milliseconds()>0){
return false;
}
else {
LOG( LL_WARNING ) << "backgroundjob " << name() << "warning: spurious wakeup return TIMEOUT code but we want wait "<<(endTime-curTime).total_milliseconds()<<" ms yet! Try again." << endl;
}
}
}
}
else {
while ( _status->state != Done ) {
_status->finished.wait( l.boost() );
}
}
return true;
}
After some time I found other problem with Perl:
perl -MCPAN -e 'install "DateTime"'
Simple Perl test failed:
use strict;
use warnings;
use Test::More;
use DateTime;
{
my $epochtest = DateTime->from_epoch( epoch => '997121000' );
is(
$epochtest->epoch, 997121000,
"epoch method returns correct value"
);
}
I discovered that the problems with Perl and Mongo disappear if I remove /etc/localtime file!

I had the same issue running mongod 3.0.2 on Ubuntu 14.04:
MongoDB shell version: 2.4.9
Sun Apr 26 00:57:31.604 versionArrayTest passed
connecting to: xxx.xxx.xxx.xxx:27000/test
Sun Apr 26 00:57:31.667 creating new connection to:xxx.xxx.xxx.xxx:27000
Sun Apr 26 00:57:31.667 BackgroundJob starting: ConnectBG
Sun Apr 26 00:57:31.668 connected connection!
Sun Apr 26 00:57:31.671 User Assertion: 18:{ ok: 0.0, errmsg: "auth failed", code: 18 }
Sun Apr 26 00:57:31.673 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:228
Sun Apr 26 00:57:31.673 User Assertion: 12514:login failed
Sun Apr 26 00:57:31.673 freeing 1 uncollected N5mongo20DBClientWithCommandsE objects
exception: login failed
The distribution came from MMS Mongo:
https://mms.mongodb.com/
I then noticed on my MMS page, under mongod processes -> elipse menu;
a menu called:
"Connect to this Mongod Instance"
which returned a string:
"/var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.0.2/bin/mongo RBX1:27000"
(RBX1 being my instance and 27000 being the port I set)
Once I applied this string in my shell (I added extra user / password and authenticationDatabase parameters, and it worked.
I can now connect to my Mongod instance both locally and remotely.
Did you install MongoDB through MMS?

Related

Error during mongo startup

When I type mongo on command prompt than output like
~$ mongo
MongoDB shell version: 2.2.3
connecting to: test
But when I type mongo 127.0.0.1:28017/stu1
output :-
MongoDB shell version: 2.2.3
connecting to: 127.0.0.1:28017/stu1
Mon Mar 10 16:56:01 DBClientCursor::init call() failed
Mon Mar 10 16:56:01 Error: Error during mongo startup. :: caused by :: 10276
DBClientBase::findN: transport error: 127.0.0.1:28017 ns: admin.$cmd query: { whatsmyuri: 1 } src/mongo/shell/mongo.js:93
exception: connect failed
OS :- ubuntu 12.04
So please help me to solve this error.
28017 is the default port for the HTTP admin interface, so if your config is allowing HTTP, you should use a web browser to access it http://127.0.0.1:28017/
You're trying to use mongo shell to access HTTP service.
To have access to the database using HTTP protocol then use ~$ mongod --rest to start the db. Now call http://127.0.0.1:28017/stu1/coll_name in the web browser which list all the documents in your collection.

practical limit for the number of databases in mongodb

Can anyone say are if there is any practical limit for the number of databases in mongodb? I've started to have serious problems when I passed 120 databases. Simple things like :
> show dbs
Mon Feb 10 16:35:32 DBClientCursor::init call() failed
Mon Feb 10 16:35:32 query failed : admin.$cmd { listDatabases: 1.0 } to: 127.0.0.1:27017
Mon Feb 10 16:35:32 Error: error doing query: failed src/mongo/shell/collection.js:155
Mon Feb 10 16:35:32 trying reconnect to 127.0.0.1:27017
Mon Feb 10 16:35:32 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
>
Mon Feb 10 16:36:01 trying reconnect to 127.0.0.1:27017
Mon Feb 10 16:36:01 reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
>
Mon Feb 10 16:37:01 trying reconnect to 127.0.0.1:27017
Mon Feb 10 16:37:01 reconnect 127.0.0.1:27017 ok
and
> getMemInfo()
{ "virtual" : 32, "resident" : 7 }
Mon Feb 10 16:39:00 DBClientCursor::init call() failed
Mon Feb 10 16:39:00 query failed : admin.$cmd { replSetGetStatus: 1.0, forShell: 1.0 } to: 127.0.0.1:27017
> shell
Mon Feb 10 16:39:38 ReferenceError: shell is not defined (shell):1
Mon Feb 10 16:39:38 trying reconnect to 127.0.0.1:27017
Mon Feb 10 16:39:38 reconnect 127.0.0.1:27017 ok
Yet the log file stayed enigmatic
What version of mongodb are you running on what host?
Here is a test on CenOS 6.5, mongodb 2.2 x86_64 direct from EPEL
Here is a sample python script that creates 1000 databases
from pymongo import MongoClient
mc = MongoClient()
for i in range(5000):
print i
mc['db%s'%(i)].test.insert({"test":True})
output:
...snip...
506
Traceback (most recent call last):
File "overload_mongo.py", line 6, in <module>
mc['db%s'%(i)].test.insert({"test":True})
File "/usr/lib64/python2.6/site-packages/pymongo/collection.py", line 357, in insert
continue_on_error, self.__uuid_subtype), safe)
File "/usr/lib64/python2.6/site-packages/pymongo/mongo_client.py", line 929, in _send_message
raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: [Errno 104] Connection reset by peer
There it is, looking at the log
ERROR: Uncaught std::exception: boost::filesystem::basic_directory_iterator constructor: Too many open files: "/index/bauman/db/_tmp/esort.1392056635.506/", terminating
The good ole too many open files problem
If you are on a enterprise linux platform, you can drop this file into /etc/security/limits.d/mongodb.conf and start a new session
mongodb hard nofile 99999
mongodb soft nofile 99999
mongodb hard nproc 99999
mongodb soft nproc 99999
I dont know how to achieve a similar result on windows.
The 'problem' lies in that MongoDB wants to memory map every single database file, so you need your hostOS to allow it to do so.
Same code as above
python overload_mongo.py
Output
...snip...
995
996
997
998
999
All better

Executing a JavaScript in MongoDB

I am trying to execute a Java script file in MongoDB at port 5930. Mongo can listen to the Database on that port:
./mongo localhost:5930
connecting to: localhost:5930/test
Server has startup warnings:
Wed Dec 18 13:15:58.906 [initandlisten]
Wed Dec 18 13:15:58.906 [initandlisten] ** WARNING: /proc/sys/vm/overcommit_memory is 2
Wed Dec 18 13:15:58.906 [initandlisten] ** Journaling works best with it set to 0 or 1
Now when I execute test.js at port 5930 but I get the following error:
./mongo test.js
connecting to: test
Wed Dec 18 13:22:56.011 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed
The test.js has the following line:
conn = new Mongo("localhost:5930);
How can I run the script at port 5930? and Why did the script always try to execute from the default port (27017)?
Thanks
Mongo tries to connect itself to the local database automatically, before it looks at your script.
Connect to a local Mongo database running on an alternate port with this mongo command line:
mongo --nodb test.js
And have your code open the connection (which you already do).

error during mongo db.shutdownServer()

I got the following error when I try to shutdown mongodb in my VM Ubuntu.
I am running 12.10 Ubuntu headless server.
The current Mongodb Shell Version is 2.0.6
use admin
switched to db admin
> db.shutdownServer()
Tue Dec 10 14:17:03 DBClientCursor::init call() failed
Tue Dec 10 14:17:03 query failed : admin.$cmd { shutdown: 1.0 } to: 127.0.0.1
server should be down...
Tue Dec 10 14:17:03 trying reconnect to 127.0.0.1
Tue Dec 10 14:17:03 reconnect 127.0.0.1 ok
Tue Dec 10 14:17:03 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Tue Dec 10 14:17:03 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [1] server [127.0.0.1:27017]
Tue Dec 10 14:17:03 DBClientCursor::init call() failed
Tue Dec 10 14:17:03 query failed : admin.$cmd { getlasterror: 1.0, w: 1.0 } to: 127.0.0.1
Tue Dec 10 14:17:03 Error: error doing query: failed shell/collection.js:151
What should I do?
My reason for trying to shut it down is because I want to update to mongo 2.2.
Please advise.
Although the messaging is confusing, this is actually expected behaviour if you shutdown via the mongo shell. Since you ran the db.shutdownServer() command through the mongo shell it can no longer connect to the server and this is essentially indicating the shell has been disconnected.
The mongo shell tries to automatically reconnect when you hit enter, which results in the messages like "trying to reconnect ...".
There is an open issue to improve this behaviour/messaging if you'd like to upvote/watch it: SERVER-5467.

Mongodb client intermittent connection error

I have a replicaset where there is one primary and two secondary nodes.
Once in a while I am not able to connect to primary and i get this error which goes away after a minute or so and I am able to connect. Any thoughts why this could be happening ?
When this error happens I tried connecting to secondary and did rs.status which said
primary is still same server and running ok.
Below is the command text from the console that shows the issue.
mongo --host <hostname> --port 27017
MongoDB shell version: 2.2.2
connecting to: <hostname>:27017/test
Sat Mar 2 14:54:02 DBClientCursor::init call() failed
Sat Mar 2 14:54:02 Error: Error during mongo startup. :: caused by :: 10276 DBClientBase::findN: transport error: <hostname>:27017 ns: admin.$cmd query: { whatsmyuri: 1 } src/mongo/shell/mongo.js:93
exception: connect failed
mongo --host <hostname> --port 27017
MongoDB shell version: 2.2.2
connecting to: <hostname>:27017/test
rs0:PRIMARY>