Connecting to MongoLab database from localhost - node.js

I am trying to connecting to a database created under my mongolab account. I am running this subsequent code on localhost. It runs for 10-20 seconds before giving me either this error: [Error: failed to connect to [gmail.com>:27017]] or this error: connection error: { [MongoError: auth failed] name: 'MongoError', ok: 0, errmsg: 'auth failed', code: 18 }
var mongoose = require('mongoose');
mongoose.connect('mongodb://<myUserName>:<myPassword>#ds011863.mlab.com:11863/myDBName'); // connect to our database
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
console.log('mongo connected!');
});
Are there any inherent problems with attempting to connect to mongoLab's hosted DB's through localhost on port 8080? I have been through much of mongoLab's trouble-shooting and cannot see what the cause of this error can be.

Related

heroku can't connect to my mongoose database

I'm deploying a nodejs app on heroku but an issue happend when I try to log in my app with my mongodb users collection
MongooseError: Operation `users.findOne()` buffering timed out after 10000ms
I had the 0.0.0.0/0 ip in my mongodb whitelist and it works well when I run my app localy
Here is my code to connect the database
const mongoUrl = process.env.DB_URL;
mongoose.connect(mongoUrl, {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to Mongodb');
});
I have had the env var DB_URL in my heroku settings but it's still not working
I don't know what to do
Please help me

MongoParseError: URI does not have hostname, domain name and tld on deploy app on Heroku

My app works very well on my local machine but when I deploy it on Heroku I can not connect with my Database I got an error: MongoParseError: URI does not have hostname, domain name and tld. I have allowed access from any IP address in Mongo Atlas.
Here is my code to connect with Mongo Atlas:
mongoose.connect(`mongodb+srv://drdung1999:onichan123#cluster0.lbxxt.mongodb.net/pinet2?retryWrites=true&w=majority`,{useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false});
// try connect mongodb
const db = mongoose.connection;
// log result
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connect mongDB success');
});

Why am I seeing "Application Error" after deploying my nodejs application to heroku and connected with mongob atlas?

MongoDB connection file
module.exports={
database:process.env.MONGODB_URI || "mongodb://localhost:27017/blogdb",
secret:"secretkey"
};
MongoDB connection code
mongoose.connect(config.database, {useNewUrlParser: true,useUnifiedTopology: true});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Connected to mongodb');
});
whitelisted Ip's-
0.0.0.0/0 (includes your current IP address)
Heroku logs are attached as screenshots

MongoError: failed to connect to server [localhost:27017] on first connect (ECONNREFUSED)

I just added MongoDB to the dependencies of my Node.js project created with the npm init command. My index.js code is:
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/mydatabase';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
db.close();
});
But when I execute the code it throws the following error:
AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
I really don't know how to fix it. I followed some guides that reported the same error but I couldn't fix it.
Should I poen a port on my modem? Should I replace "localhost" with my IP? Should I do anything else?
Please help me!
UPDATE:
I installed a MongoDB server on my Android device and replacing the url variable with mongodb://ANDROID-DEVICE-LOCAL-IP:27017/mydatabase it now works.
How can I accomplish it placing the database on my computer? Is the firewall blocking incoming connections? Is this why it works on Android but not on Windows?
I fixed the issue!
I ran mongod -dbpath "MY-PATH" in the cmd and now it works.
The error occurred because nothing was listening on 27017 port. Now the mongod program is listening for connections on that port and so the connection from my project is no more refused.
Make a configuration file like config.js
module.exports = {
'secretKey': '12345-67890-09876-54321',
'mongoUrl' : 'mongodb://localhost:27017/cubs'
}
And require that file in your server code
var config = require('./config');
var mongoose = require('mongoose');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("Connected correctly to server");
});

Connect multiple mongodbs, running on different aws servers

I am new to nodejs and I am facing an issue when I try to connect multiple mongodbs, running on different aws servers.
I have used tunnel-ssh but it shows failed to connect.
Can anyone provide me some information on how to connect multiple mongodbs, running on different aws servers within a single nodejs application?
const fs=require('fs')
var mongoose = require('mongoose');
var tunnel = require('tunnel-ssh');
var config = {
username:'ubuntu',
host:'my.ip.address',
agent : process.env.SSH_AUTH_SOCK,
privateKey:require('fs').readFileSync('C:\Users\\Ronit\\Downloads\\my.ppk'),
port:19735,
dstHost:'Database server Ip address',
dstPort:19735,
localHost:'127.0.0.1',
//password:'mypassword',
localPort: 19735,
keepAlive:true
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://localhost:19735/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
// we're connected!
console.log("DB connection successful");
});
});
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED my_ip:19735
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)

Resources