Connect multiple mongodbs, running on different aws servers - node.js

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)

Related

Issue with tunnel-ssh npm for ssh connection to mongo through mongoose

I am trying to establish a connection to remote mongo server through ssh tunnel using mongoose
The implementation code is:
import tunnel from 'tunnel-ssh';
const config = {
username: 'username',
Password: 'password',
host: process.env.SSH_SERVER, //192.168.9.104
port: 22,
dstHost: process.env.DESTINATION_SERVER, //192.168.9.104
dstPort: process.env.DESTINATION_PORT, //27017
localHost: '127.0.0.1',
localPort: 27017
};
this is the config that i have created while the connection is as follows:
class DB {
initDB() {
tunnel(config, (error, server) => {
if (error) {
console.log('SSH connection error: ' + error);
}
const url = 'mongodb://' + process.env.MONGO_URL; //localhost:27017/DBname
mongoose.connect(url, { useNewUrlParser: true });
mongoose.plugin(toJson);
mongoose.plugin(setProperties);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log('DB connection successful');
});
});
}
}
When the function initDB() is invoked the following error pops up
SSH connection error: ConfigError: host not set
events.js:183
throw er; // Unhandled 'error' event
^
ConfigError: host not set
The host is already set but this error seems to be somewhere in the config part but I doesnt seem to single out to the exact reason
your "host" property at "config" var isn't defined. try using hard coded value instead of env var, if it works it means process can't read env vars which might be caused since you R not importing dotenv module

Connecting to mongodb through ssh username/password through nodejs

I am trying to connect to Mongodb through ssh username/password through nodejs as below :
var mongoose = require('mongoose');
var tunnel = require('tunnel-ssh');
var config = {
username : 'xyz',
host: 'xx-xxx-xx.com',
port:22,
password:'xxx',
dstPort:27017,
localPort:27017
};
console.log(tunnel);
var server = tunnel(config, function (error, server) {
if(error){
console.log("SSH connection error: " + error);
}
console.log(server);
mongoose.connect('mongodb://localhost:27017/myDB');
var db = mongoose.connection;
console.log(db);
db.on('error', console.error.bind(console, 'DB connection error:'));
db.once('open', function() {
console.log("DB connection successful");
});
});
But I am getting error as below :
DB connection error: { MongoError: connection 0 to localhost:27017 timed out
What is the issue here?
Probably is because you are missing the 'dstHost'in the config options.
The 'dstHost' must be the destination server url.
from official tunnel-ssh doc
var config = {
username:'root',
Password:'secret',
host:sshServer,
port:22,
dstHost:destinationServer,
dstPort:27017,
localHost:'127.0.0.1',
localPort: 27000
};
var tunnel = require('tunnel-ssh');
tunnel(config, function (error, server) {
//....
});

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");
});

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.

Resources