8

I have a node js app in IP ex: 111.111.111.23:3000 I want to connect from my local express/node app script to my remote mongodb database. the database is running in the IP mention above but for some reason all i can do is connect to my local mongodb databse.

if (app.get('env') === 'development') { app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); mongoose.connect('mongodb://localhost/test'); } 

the code above is in my app.js and it works, but what I want to do is something like this.

if (app.get('env') === 'development') { app.use(function (err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); mongoose.connect('mongodb:111.111.111.23:27017/test'); } 

I tried adding the ip with and without the port but it fails.

My question is how do I connect to my remote database from my local machine ?

it case this help. The remote server is running ubuntu server 14.04 and I installed mongodb using this guide: https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

I get the following error:

Error: connect ECONNREFUSED 111.111.111.23:27017

8
  • You seem to be missing the // prefix before the IP address. Commented Nov 9, 2015 at 17:12
  • I added that and it still doesn't work. Commented Nov 9, 2015 at 17:16
  • Is your database password-protected? Does your server port forward external requests to :27017? Commented Nov 9, 2015 at 17:35
  • Does your server have a firewall? Commented Nov 9, 2015 at 17:41
  • my firewall is inactive. Commented Nov 9, 2015 at 17:53

3 Answers 3

12

The mongoose connection format is:

mongoose.connect('mongodb://username:password@host:port/database') 

if you don't have username and password, just execute:

mongoose.connect('mongodb://host:port/database'); 

Source: http://mongoosejs.com/docs/connections.html

You could also try to connect using the mongo client and see if it works..

$ mongo 111.111.111.23:27017 

Also, but not likely, make sure your mongod service is up.

$ sudo service mongod start 

Finnaly, make sure your firewall is down or open the port using:

$ iptables -A INPUT -p tcp --dport 27017 -j ACCEPT 
Sign up to request clarification or add additional context in comments.

Comments

0

Here is my code and still having the error: error:

E:\nodeJs>node mongoDBApi.js E:\nodeJs\node_modules\mongodb\lib\mongo_client.js:792 throw err; ^ [object Object] 

code:

var MongoClient = require('mongodb').MongoClient; var url = 'mongodb://xxxx:[email protected]:22/'; MongoClient.connect(url, function(err, db) { if (err) throw err; var dbo = db.db("cdb"); dbo.collection("ccollection").findOne({}, function(err, result) { if (err) throw err; console.info(result.name); db.close(); }); }); 

2 Comments

what if my password includes @ symbol?
const PASSWORD = encodeURIComponent('hello@123'); @DeepakTerse encode your password if it contains special characters
0

code :

const mongoose = require("mongoose"); const DbConnector = () => { const DbConnectStr = process.env.MONGODB_URL; mongoose.connect(DbConnectStr, { useNewUrlParser: true, useUnifiedTopology: true }, (err) => { if (err) console.log(err); else console.log("Database Connected"); }); }; module.exports = { DbConnector }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.