Node.js ORM mysql connect via SSH tunnel - node.js

I'm trying to set up a node.js application which uses node-orm2. However our cloud hosted DB can only be connected via SSH tunnel. Checking the ORM doc I can not see any config option to connect to the DB via SSH tunnel. Is there any way to set up this or I need to find some way to connect without SSH?

I updated the code example for tunnel-ssh 1.1.0, because it's actually the only working example on the internet (for so far i searched..).
It was quite a hassle to get this new tunnel-ssh configured...
var mysql = require('mysql');
var Tunnel = require('tunnel-ssh');
module.exports = function (server) {
return new Object({
tunnelPort: 33333, // can really be any free port used for tunneling
/**
* DB server configuration. Please note that due to the tunneling the server host
* is localhost and the server port is the tunneling port. It is because the tunneling
* creates a local port on localhost
*/
dbServer: server || {
host: '127.0.0.1',
port: 33333,
user: 'username',
password: 'yourpwd',
database: 'yourdb'
},
/**
* Default configuration for the SSH tunnel
*/
tunnelConfig: {
remoteHost: '127.0.0.1', // mysql server host
remotePort: 3306, // mysql server port
localPort: 33333, // a available local port
verbose: true, // dump information to stdout
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections)
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2)
host: 'your_tunneling_host',
port: 22,
username: 'user_on_tunneling',
password: 'pwd'
//privateKey: require('fs').readFileSync('<pathToKeyFile>'),
//passphrase: 'verySecretString' // option see ssh2 config
}
},
/**
* Initialise the mysql connection via the tunnel. Once it is created call back the caller
*
* #param callback
*/
init: function (callback) {
/* tunnel-ssh < 1.0.0
//
// SSH tunnel creation
// tunnel-ssh < 1.0.0
var me = this;
me.tunnel = new Tunnel(this.tunnelConfig);
me.tunnel.connect(function (error) {
console.log('Tunnel connected', error);
//
// Connect to the db
//
me.connection = me.connect(callback);
});
*/
/* tunnel-ssh 1.1.0 */
//
// SSH tunnel creation
//
var me = this;
// Convert original Config to new style config:
var config = this.tunnelConfig;
var newStyleConfig = {
username: config.sshConfig.username,
port: config.sshConfig.port,
host: config.sshConfig.host,
// SSH2 Forwarding...
dstPort: config.remotePort,
dstHost: config.remoteHost,
srcPort: config.localPort,
srcHost: config.localHost,
// Local server or something...
localPort: config.localPort,
localHost: config.localHost,
privateKey: config.privateKey
}
me.tunnel = tunnel(newStyleConfig, function (err) {
console.log('Tunnel connected', err);
if (err) {
return callback(err);
}
me.connection = me.connect(callback);
});
},
/**
* Mysql connection error handling
*
* #param err
*/
errorHandler: function (err) {
var me = this;
//
// Check for lost connection and try to reconnect
//
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('MySQL connection lost. Reconnecting.');
me.connection = me.connect();
} else if (err.code === 'ECONNREFUSED') {
//
// If connection refused then keep trying to reconnect every 3 seconds
//
console.log('MySQL connection refused. Trying soon again. ' + err);
setTimeout(function () {
me.connection = me.connect();
}, 3000);
}
},
/**
* Connect to the mysql server with retry in every 3 seconds if connection fails by any reason
*
* #param callback
* #returns {*} created mysql connection
*/
connect: function (callback) {
var me = this;
//
// Create the mysql connection object
//
var connection = mysql.createConnection(me.dbServer);
connection.on('error', me.errorHandler);
//
// Try connecting
//
connection.connect(function (err) {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
if (callback) callback();
});
return connection;
}
}
);
};

Finally it was resolved by dropping orm2 and using node-mysql and tunnel-ssh modules as in the code below.
var mysql = require('mysql');
var Tunnel = require('tunnel-ssh');
module.exports = function (server) {
return new Object({
tunnelPort: 33333, // can really be any free port used for tunneling
/**
* DB server configuration. Please note that due to the tunneling the server host
* is localhost and the server port is the tunneling port. It is because the tunneling
* creates a local port on localhost
*/
dbServer: server || {
host: '127.0.0.1',
port: 33333,
user: 'username',
password: 'yourpwd',
database: 'yourdb'
},
/**
* Default configuration for the SSH tunnel
*/
tunnelConfig: {
remoteHost: '127.0.0.1', // mysql server host
remotePort: 3306, // mysql server port
localPort: 33333, // a available local port
verbose: true, // dump information to stdout
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections)
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2)
host: 'your_tunneling_host',
port: 22,
username: 'user_on_tunneling',
password: 'pwd'
//privateKey: require('fs').readFileSync('<pathToKeyFile>'),
//passphrase: 'verySecretString' // option see ssh2 config
}
},
/**
* Initialise the mysql connection via the tunnel. Once it is created call back the caller
*
* #param callback
*/
init: function (callback) {
//
// SSH tunnel creation
//
var me = this;
me.tunnel = new Tunnel(this.tunnelConfig);
me.tunnel.connect(function (error) {
console.log('Tunnel connected', error);
//
// Connect to the db
//
me.connection = me.connect(callback);
});
},
/**
* Mysql connection error handling
*
* #param err
*/
errorHandler: function (err) {
var me = this;
//
// Check for lost connection and try to reconnect
//
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('MySQL connection lost. Reconnecting.');
me.connection = me.connect();
} else if (err.code === 'ECONNREFUSED') {
//
// If connection refused then keep trying to reconnect every 3 seconds
//
console.log('MySQL connection refused. Trying soon again. ' + err);
setTimeout(function () {
me.connection = me.connect();
}, 3000);
}
},
/**
* Connect to the mysql server with retry in every 3 seconds if connection fails by any reason
*
* #param callback
* #returns {*} created mysql connection
*/
connect: function (callback) {
var me = this;
//
// Create the mysql connection object
//
var connection = mysql.createConnection(me.dbServer);
connection.on('error', me.errorHandler);
//
// Try connecting
//
connection.connect(function (err) {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
if (callback) callback();
});
return connection;
}
}
);
};

You can use the settings parameters to node-orm2 in order pass an options object to the underlying driver. For example, if you are using a mysql then you can pass an ssl option. See https://github.com/felixge/node-mysql#ssl-options in this case.

Thanks for the existing answers in this thread. The following solution worked for me:
function connect() {
return new Promise(async resolve => {
let tunnelPort = 33000 + Math.floor(Math.random() * 1000);
Tunnel({
//First connect to this server over ssh
host: '6.6.6.6',
username: 'vagrant',
privateKey: await fs.readFile('path/to/private_key'),
//And forward the inner dstPort (on which mysql is running) to the host (where your app is running) with a random port
dstPort: 3306,
localPort: tunnelPort
}, (err) => {
if (err) throw err;
console.log('Tunnel connected');
let connection = mysql.createConnection({
//Now that the tunnel is running, it is forwarding our above "dstPort" to localhost/tunnelPort and we connect to our mysql instance.
host: '127.0.0.1',
port: tunnelPort,
user: 'root',
password: 'password',
database: 'dbName'
});
connection.on('error', err => { throw err; });
connection.connect((err) => {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
resolve(connection);
});
});
})
}

Related

Failed to connect SQL Server from Node.js using tedious

I am trying to connect to SQL Server in our domain network. I am able to connect using python but not able to connect in Node.js using Tedious.
Node.js code snippet:
var config = {
server: 'serverName.domain.com',
authentication: {
type: 'default',
options: {
userName: 'DOMAINID\\username',
password: 'password'
}
},
options: {
database: 'dbName',
port: 1234,
}
};
var connection = new Connection(config);
connection.on('connect', function (err) {
if (err) {
console.log('err', err);
} else {
console.log("Connected");
executeStatement();
}
});
connection.connect();
Receiving error:
Login Failed for the user DOMAINID/username. The login is from an untrusted domain and cannot be used with Windows authentication.
But when trying to connect from Python, I am able to connect successfully.
Python snippet:
import sqlalchemy
conn = sqlalchemy.create_engine('mssql+pymssql://DOMAINID\\username:password#serverName.domain.com:1234/dbName')
print(conn.execute('SELECT * FROM table_name').fetchall())
Data received successfully in python.
And also I tried with mssql and msnodesqlv8 with Microsoft ODBC 11 for Microsoft SQL Server drivers.
I am able to connect. Following is the code snippet.
const sql = require("mssql/msnodesqlv8");
const main = async () => {
const pool = new sql.ConnectionPool({
server: "server.domain.com",
database: "dbName",
port: 1234,
user:'DomainId\\username', // Working without username and password
password:'password',
options: {
trustedConnection: true // working only with true
}
});
await pool.connect();
const request = new sql.Request(pool);
const query = 'select * from table';
const result = await request.query(query);
console.dir(result);
};
main();
In the above snippet, I am able to connect without username and password but with trustedConnection true only. I am using windows authentication not SQL authentication. How can I connect using tedious js

Can I connect to ssh2 without the privateKey?

I can connect to ssh2 without the privateKey
I am trying to enter a server with SFTP but when I get the following error ...
Timed out while waiting for handshake
I'm looking for an example and almost everyone uses the privateKey, is it mandatory? and how is one generated?
My code is the following ...
var Client = require ('ssh2'). Client;
var conn = new Client ();
conn.on ('error', function (err) {
console.log ('SSH - Connection Error:' + err);
});
conn.on ('end', function () {
console.log ('SSH - Connection Closed');
});
conn.on ('ready', function () {
console.log ("------ enter ------");
// code to work with SSH
});
conn.connect ({
host: 'host',
username: 'user',
port: 22
password: 'password',
});

How to make a SSH to postgres server with help of ssh2 library and query using pg libary?

I am trying to make a connection to Postgres remote host using the SSH tunnel from node application(library:ssh2) and query the data(lib:pg).
Before I used mysql2(node library) make a tunnel using ssh2(node library) and connected to Mysql remote host as well as able to query the data. But I want to do the same in Postgres remote host I am not able to connect to it.
pg Client obj config does not support the stream from ssh2 forwardOut..!
Or, Is there any library available to make this happen instead of that ssh2, pg
PS: In mysql2 when I try to make a connection with a mysql config with stream: stream key value.
var Clientssh = require('ssh2').Client;
var conn = new Clientssh();
conn.on('ready', function() {
console.log('Client :: ready');
conn.forwardOut(
'127.0.0.1',
5434,
'localhost',
5432,
function(err, stream) {
if (err) throw err;
let conf = {
host: 'localhost',
port: 5434,
user: 'test',
database: 'test',
password: 'test'
}
let remoteConnection = new Client(conf);
remoteConnection.connect(function(err) {
if (err) {
console.log(err);
console.log("Unable to connect to postgre");
res.send(err);
} else {
remoteConnection.query('SELECT * FROM test', function(err, testResult) {
remoteConnection.end();
if (err) {
console.log("Unable to fetch data");
res.send(err);
} else {
console.log("Succcess");
res.send(testResult);
}
});
}
});
});
}).connect({
host: 'hostaddress',
port: 'hostport',
username: 'hostusername',
privateKey: require('fs').readFileSync('path/for/key')
});
It shows the Connection terminated unexpectedly from this line
Client :: ready
Error: Connection terminated unexpectedly
at Connection.con.once (/node-postgres/node_modules/pg/lib/client.js:235:9)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Channel.<anonymous> (/node-postgres/node_modules/pg/lib/connection.js:131:10)
at Channel.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Unable to connect to postgre

Error: Can't add new command when connection is in closed state

I have recently deployed my node.js API application on live server. I am getting these issue on live server.
I have googled it, but could not get any exact solution. Can anyone suggest how can i solve this problem?
{ Error: read ETIMEDOUT at TCP.onread (net.js:622:25) errno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'read', fatal: true }
{ Error: Can't add new command when connection is in closed state at PoolConnection._addCommandClosedState }
I amd using the mysql connection pool like this
var mysql = require('mysql2');
var mysqlPool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'xyz',
database: 'xyz',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
module.exports = mysqlPool;
I had a similar problem and ended up having to put the connection request in it's own .js file and import it into the controller-
connectionRequest.js
module.exports = function () {
let mysql = require('mysql2')
let connCreds = require('./connectionsConfig.json');
//Establish Connection to the DB
let connection = mysql.createConnection({
host: connCreds["host"],
user: connCreds['username'],
password: connCreds['password'],
database: connCreds['database'],
port: 3306
});
//Instantiate the connection
connection.connect(function (err) {
if (err) {
console.log(`connectionRequest Failed ${err.stack}`)
} else {
console.log(`DB connectionRequest Successful ${connection.threadId}`)
}
});
//return connection object
return connection
}
Once I did that I was able to import it into my query on the controller file like so
ControllerFile.js
let connectionRequest = require('../config/connectionRequest')
controllerMethod: (req, res, next) => {
//Establish the connection on this request
connection = connectionRequest()
//Run the query
connection.query("SELECT * FROM table", function (err, result, fields) {
if (err) {
// If an error occurred, send a generic server failure
console.log(`not successful! ${err}`)
connection.destroy();
} else {
//If successful, inform as such
console.log(`Query was successful, ${result}`)
//send json file to end user if using an API
res.json(result)
//destroy the connection thread
connection.destroy();
}
});
},
After a lot of messing around I was able to solve the problem by destroying the connection, waiting (this is the important page) and getting the connection again.
conn = await connPool.getConnection();
// We have error: Can't add new command when connection is in closed state
// I'm attempting to solve it by grabbing a new connection
if (!conn || !conn.connection || conn.connection._closing) {
winston.info('Connection is in a closed state, getting a new connection');
await conn.destroy(); // Toast that guy right now
sleep.sleep(1); // Wait for the connection to be destroyed and try to get a new one, you must wait! otherwise u get the same connection
conn = await connPool.connection.getConnection(); // get a new one
}

When mongodb server is down how to catch the error while running mongoose query

I am using mongoose for connecting node.js with mongoDB, now i wrote below query
var trans = new transmodel({method: method, trans_id: r});
trans.save(function(err) {
if (err) {
console.error("Razor_pay_webhook Error 4 err: " + err);
res.write('statusCode: 200');
res.end();
} else {
res.write('statusCode: 400');
res.end();
}
});
I thought when my mongodb cluster will be down then i will get 'err' while executing above mongoose query, but when i ran above query while my mongo cluster was down nothing happened(No err was called). Can anyone please tell me how can i catch the error if my mongodb server is down inside my query. Also for reconnecting again with my cluster i have set below parameters but my node server is not trying to reconnect again with my mongodb server i don't know what's going wrong.
var mongoose = require('mongoose');
var config = require('./config/database.js');
var DB_URL = config.db.url;
mongoose.connection.on("connected", function(ref) {
console.log("Connected to " + " DB!");
});
mongoose.connection.on("error", function(err) {
console.error('Failed to connect to DB ' + ' on startup ', err);
if (err) {
return next(err);
}
});
mongoose.connection.on('disconnected', function(err) {
console.log('Mongoose default connection to DB :' + ' disconnected');
if (err) {
return next(err);
}
});
var gracefulExit = function() {
mongoose.connection.close(function () {
console.log('Mongoose default connection with DB :' + ' is disconnected through app termination');
process.exit(0);
});
}
process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
exports.con_close = function () {
console.log('Mongoose connection disconnected');
mongoose.connection.close();
}
var options = {
server: {
socketOptions: {
keepAlive: 1000,
connectTimeoutMS: 30000
}
},
replset: {
rs_name: 'replicaset',
auto_reconnect:true,
socketOptions: {
keepAlive: 1000, // doubt about it
connectTimeoutMS: 30000
}
},
user: 'root',
pass: 'G3saGT2Y',
auth: {
authdb: 'admin'
}
}
mongoose.connect(DB_URL, options, function(err) {
console.log('ho rha hai');
if (err) {
console.log('error connection to mongo server!');
console.log(err);
}
});
You are using mongoose, it emits events (the EventEmitter pattern) when the database is down and when the database is reconnecting and up again.
from mongoose code found here we can see that the library db connection - connection.js
has the following events that are emitted:
* #param {Mongoose} base a mongoose instance
* #inherits NodeJS EventEmitter
http://nodejs.org/api/events.html#events_class_events_eventemitter
* #event connecting: Emitted when connection.{open,openSet}() is executed on this connection.
#event connected: Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected scenarios.
#event open: Emitted after we connected and onOpen is executed on all of this connections models.
#event disconnecting: Emitted when connection.close() was executed.
#event disconnected: Emitted after getting disconnected from the db.
#event close: Emitted after we disconnected and onClose executed on all of this connections models.
#event reconnected: Emitted after we connected and subsequently disconnected, followed by successfully another successfull connection.
#event error: Emitted when an error occurs on this connection.
#event fullsetup: Emitted in a replica-set scenario, when primary and at
least one seconaries specified in the connection string are connected.
#event all: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
When the database is down you will receive two events:
1. disconnected
2. error (the error that driver encountered)
When the database is up again you will receive the reconnect event.
So you don't need to try catch the error rather you should listen to these events.
More helpful information about connection failures and reconnecting can be found here.
This article explain how to use and configure the autoReconnect and the bufferMaxEntries according to your settings.

Resources