Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB slow initial connection

I am facing a really weird scenario, the initial connection to mongoDb takes around 15 seconds. My current setup is the following:

  1. mongodb running inside an ubuntu vm on the same machine
  2. mongodb is version 2.6.1
  3. node.js installed using brew and it is version 0.10.28

Upon restarting nodemon the initial signin POST takes around 15 seconds

POST /api/v1/signin 200 14707ms - 56b

other POST to the same route without restarting the server is relatively fast:

POST /api/v1/signin 200 76ms - 56b

the reason why this bothers me is that because this project is still in development, nodemon tends to restart a lot and testing is being a pain.

I am using the following node modules which are related to db and authentication:

  1. "express": "~4.2.0",
  2. "mongoose": "3.8.8",
  3. "passport": "0.2.0",
  4. "passport-local": "0.1.6",
  5. "bcrypt": "*"

this is the way i am connecting to mongo:

var mongoUrl = "mongodb://devmachine.local:27017/project";
mongoose.connect(mongoUrl, {auto_reconnect: true});

any help would be highly appreciated.

Thanks

like image 760
Fouad Avatar asked Jun 14 '14 06:06

Fouad


People also ask

Why is my MongoDB 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.

Does MongoDB support connection pooling?

Sharded Cluster Connection Poolingmongos routers have connection pools for each node in the cluster.

What is useNewUrlParser in MongoDB?

useNewUrlParser : The underlying MongoDB driver has deprecated their current connection string parser. Because this is a major change, they added the useNewUrlParser flag to allow users to fall back to the old parser if they find a bug in the new parser.

What is connection pool size in MongoDB?

The connection pool is on a per-mongod/mongos basis, so when connecting to a 3-member replica there will be three connection pools (one per mongod), each with a maxPoolSize of 1.


1 Answers

Replace the hostname with IP

From: var mongoUrl = "mongodb://devmachine.local:27017/project";

To: var mongoUrl = "mongodb://127.0.0.1:27017/project";

like image 103
Shimon Balzera Avatar answered Sep 19 '22 12:09

Shimon Balzera