heroku can't connect to my mongoose database - node.js

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

Related

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

Connection to MongoDB with SSH in my Node App

My DB is with DigitalOcean and I'm trying to connect to it in my node app.
I've found a npm called tunnel-ssh however am having trouble connecting to it. My code is below.
It says DB connection successful, however when i do a console.log(mongoose) it shows the host and host as null.
If I do console.log(mongoose), after the console.log("DB connection successful"); then it shows me the host.
var tunnel = require('tunnel-ssh');
var config = {
agent : 'myuser',
host: 'xxx:xxx:xxx:xxx'
agent : process.env.SSH_AUTH_SOCK,
privateKey:require('fs').readFileSync('id_rsa'),
port:22,
dstPort:27010,
keepAlive: true
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://127.0.0.1:27017/mysuperdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
Here's the working code:
var tunnel = require('tunnel-ssh');
var config = {
username : 'myuser',
host: 'xxx:xxx:xxx:xxx',
privateKey:require('fs').readFileSync('id_rsa'),
port:22,
dstPort:27010,
localPort: 2000
};
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
mongoose.connect('mongodb://127.0.0.1:2000/mysuperdb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
Your ports aren't matching up. You have dstPort: 27010 but your connection string is 'mongodb://127.0.0.1:27017/mysuperdb'. One or the other needs to be corrected.

Connecting to MongoLab database from localhost

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.

mongoose output the error "Error: connection closed"

I stumble upon a curious problem about mongoose connect the mongodb, it generate the detail errors as the following
e:\Mentor_Resources\node\node_twitter_bootstrap>node app
Express server listening on port 3000
Trace: error occure when start to connect dbError: connection closed
at e:\Mentor_Resources\node\node_twitter_bootstrap\server\module\word.js:14:
17
at Connection.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_mod
ules\mongoose\lib\connection.js:201:5)
at Db.open (e:\Mentor_Resources\node\node_twitter_bootstrap\node_modules\mon
goose\node_modules\mongodb\lib\mongodb\db.js:247:16)
at Server.connect.connectionPool.on.server._serverState (e:\Mentor_Resources
\node\node_twitter_bootstrap\node_modules\mongoose\node_modules\mongodb\lib\mong
odb\connection\server.js:413:7)
at EventEmitter.emit (events.js:115:20)
at connection.on.connectionStatus (e:\Mentor_Resources\node\node_twitter_boo
tstrap\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connect
ion_pool.js:108:15)
at EventEmitter.emit (events.js:91:17)
at Socket.closeHandler (e:\Mentor_Resources\node\node_twitter_bootstrap\node
_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\connection.js:401:
12)
at Socket.EventEmitter.emit (events.js:88:17)
at Socket._destroy.destroyed (net.js:364:10)
the code snippet of mongoose is:
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/word-sentence",function(err) {
if(err)
console.trace('error occure when start to connect db' + err);
});
i am sure the mongodb is open, and i restart mongodb for several times,but the error is still exist, so I reboot my Windows XP , and try again the problem disappear,everything is ok, so I want to know why?
This is a common problem when pooled connections in longer running applications return connection closed.
The mongoose documentation recommends adding keepAlive to the options object you pass into the connect function.
Here's an example (you can remove replset if you're not using this),
// include keep alive for closing connections,
// http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html
var mongoOptions =
{
db: {safe: true},
server: {
socketOptions: {
keepAlive: 1
}
},
replset: {
rs_name: 'myReplSet',
socketOptions: {
keepAlive: 1
}
}
};
mongoose.connect( YOUR_URI, mongoOptions );
mongoose.connection.on('error', function(err) {
console.log('Mongo Error:\n');
console.log(err);
}).on('open', function() {
console.log('Connection opened');
});
mongoose.connect() Is not accepting any callback functions as you have use in your code i.e.your code snippet of mongoose:
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/word-sentence",function(err) {
if(err)
console.trace('error occurred, when attempted to connect db. Error: ' + err);
});
So, I recommend you to start with this:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/word-sentence');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// yay connected!
});
Again you cannot expect any arguments in the callback from db.open('open', function cb(){})
I suggest to go through these quick start, mongoose docs - enlightens how you can jump into source code if some conflict is found in the docs & mongodb/mongoose close connection

Resources