Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb takes 3 minutes to connect

Just reinstalled Mongodb on my mac (fresh install of mountain lion 10.8) and now my apps are taking ~3 mins to connect.

I put together a simple node script to test this:

var start = (new Date()).getTime();

var mongoose = require('mongoose');

 var db = mongoose.connect('mongodb://localhost/passport-mongox',function(err){
  var stop = (new Date()).getTime();

  console.log('Took this long: ',(stop-start) / 1000 );
 });

Both times were 175.273 and 175.316 seconds.

When I connect to an external, hosted mongodb it connects in less than a second,

Any idea why this would happen? Here is my mongo.log:

Fri Feb  1 12:43:25 [initandlisten] MongoDB starting : pid=2262 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=w
Fri Feb  1 12:43:25 [initandlisten] db version v2.2.2, pdfile version 4.5
Fri Feb  1 12:43:25 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Fri Feb  1 12:43:25 [initandlisten] build info: Darwin bs-osx-106-x86-64-1.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Feb  1 12:43:25 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/usr/local/etc/mongod.conf", dbpath: "/usr/local/var/mongodb", logappend: "true", logpath: "/usr/local/var/log/mongodb/mongo.log" }
Fri Feb  1 12:43:25 [initandlisten] journal dir=/usr/local/var/mongodb/journal
Fri Feb  1 12:43:25 [initandlisten] recover : no journal files present, no recovery needed
Fri Feb  1 12:43:26 [websvr] admin web console waiting for connections on port 28017
Fri Feb  1 12:43:26 [initandlisten] waiting for connections on port 27017
Fri Feb  1 12:44:05 [initandlisten] connection accepted from 127.0.0.1:52137 #1 (1 connection now open)
Fri Feb  1 12:44:40 [initandlisten] connection accepted from 127.0.0.1:52152 #2 (2 connections now open)
Fri Feb  1 12:45:15 [initandlisten] connection accepted from 127.0.0.1:52201 #3 (3 connections now open)
Fri Feb  1 12:45:50 [initandlisten] connection accepted from 127.0.0.1:52298 #4 (4 connections now open)
Fri Feb  1 12:46:25 [initandlisten] connection accepted from 127.0.0.1:52325 #5 (5 connections now open)
Fri Feb  1 12:51:26 [conn5] end connection 127.0.0.1:52325 (4 connections now open)
Fri Feb  1 12:51:26 [conn3] end connection 127.0.0.1:52201 (4 connections now open)
Fri Feb  1 12:51:26 [conn4] end connection 127.0.0.1:52298 (4 connections now open)
Fri Feb  1 12:51:26 [conn1] end connection 127.0.0.1:52137 (4 connections now open)
Fri Feb  1 12:51:26 [conn2] end connection 127.0.0.1:52152 (4 connections now open)
like image 922
wesbos Avatar asked Feb 01 '13 17:02

wesbos


People also ask

Why MongoDB is not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Why MongoDB compass is not connecting?

Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.

Is Mongoose good for MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.


2 Answers

So the answer came from @AdamMeghji on twitter.

My hosts file has always looked like this:

127.0.0.1 localhost
127.0.0.1 test.com
127.0.0.1 wes.dev

I switched that to:

127.0.0.1 localhost test.com wes.dev

and connections went back to 0.015 seconds.

like image 119
wesbos Avatar answered Nov 15 '22 07:11

wesbos


Answer from mongoose.js

Cause: The underlying MongoDB driver defaults to looking for IPv6 addresses, so the most likely cause is that your localhost DNS mapping isn't configured to handle IPv6.

Solution : Use 127.0.0.1 instead of localhost or use the family option as shown in the connection docs.

mongoose.connect(url, {family:4}, function(err, connection) { connection.db(your_db_name); });

like image 38
Arjun J Gowda Avatar answered Nov 15 '22 09:11

Arjun J Gowda