-1

As I have installed mongodb package using:

npm install mongodb --save

on terminal and made a file connection.js. Code inside it is:

var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://127.0.0.1:27017/my_database_name';

MongoClient.connect(url, function (err, db) {
    if (err) {
        console.log('Unable to connect to the mongoDB server. Error:', err);
    } else {
        console.log('Connection established to', url);
        db.close();
    }
});

And running command:

C:\Users\upasana\Desktop\node>node connection.js

on terminal. The error that I am facing is:

Unable to connect to the mongoDB server. Error: { [MongoError:connection 0 to 127.0.0.1:27017 timed out]
name: 'MongoError',
message: 'connection 0 to 127.0.0.1:27017 timed out' }

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

This suggests you're not running MongoDB on the server and are subsequently receiving a timeout as no connection can be established..

Check to make sure the MondoDB server is running by using a `netstat -ano | grep mongo' and check if its running.

Just incase, try restarting it using sudo service mongodb restart.

Attempt to run the connection.js file again. If its still getting the same error, let me know.

Dandy
  • 1,466
  • 16
  • 31
  • Sir,I have a confusion,i am newbie to this technology and watched a lot many videos,and got 2 ways to connect to mongodb first was installing mongodb and working on cmd and second was i am using webstrom and working on nodejs and want to connect database to it,i have tried to explain all the steps,hope u won't mind explaining in the direction i want to know,sry for the reply besides the topic but if u can help in step by step to the solution of the problem.. – upasana arora Oct 11 '16 at 15:35
  • @upasanaarora Oh sorry, you're using Windows? In which case, go to your Windows `services` and make sure that Mongodb is running in your services list. If you want to ensure its started successfully, try following suggestions here: http://stackoverflow.com/questions/20796714/what-is-the-way-to-start-mongo-db-from-windows-7-64-bit To summarise, your MongoDB instance isn't running. – Dandy Oct 11 '16 at 15:38
  • Sir,the link u have provided i have tried that way nd that is working but i was trying to do it using webstrom software installing package using npm install mongodb --save command and then adding connection that way it is not working...,i have explained that in my Question....If u can help in that direction. – upasana arora Oct 11 '16 at 15:46