Node.js ORM mysql通过SSH隧道连接
我正在尝试设置一个node.js应用程序,它使用node-orm2。但是,我们的云托管数据库只能通过SSH隧道连接。检查ORM文档时,我看不到任何通过SSH隧道连接到DB的配置选项。是否有任何方法可以设置此功能,或者我需要找到一些不使用SSH连接的方法?您可以使用Node.js ORM mysql通过SSH隧道连接,node.js,node-orm2,Node.js,Node Orm2,我正在尝试设置一个node.js应用程序,它使用node-orm2。但是,我们的云托管数据库只能通过SSH隧道连接。检查ORM文档时,我看不到任何通过SSH隧道连接到DB的配置选项。是否有任何方法可以设置此功能,或者我需要找到一些不使用SSH连接的方法?您可以使用设置参数来节点-orm2,以便将选项对象传递给底层驱动程序。例如,如果您使用的是mysql,则可以传递ssl选项。看看这个例子 最后,通过删除orm2并使用节点mysql和隧道ssh模块解决了这个问题,如下面的代码所示 var mysq
设置参数来节点-orm2
,以便将选项
对象传递给底层驱动程序。例如,如果您使用的是mysql
,则可以传递ssl
选项。看看这个例子 最后,通过删除orm2并使用节点mysql和隧道ssh模块解决了这个问题,如下面的代码所示
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;
}
}
);
};
var mysql=require('mysql');
var Tunnel=require('Tunnel-ssh');
module.exports=函数(服务器){
返回新对象({
tunnelPort:33333,//实际上可以是用于隧道的任何自由端口
/**
*数据库服务器配置。请注意,由于隧道连接,服务器主机
*是localhost,服务器端口是隧道端口。这是因为隧道
*在本地主机上创建本地端口
*/
dbServer:server | |{
主持人:“127.0.0.1”,
港口:33333,
用户:“用户名”,
密码:“yourpwd”,
数据库:“yourdb”
},
/**
*SSH隧道的默认配置
*/
隧道配置:{
remoteHost:'127.0.0.1',//mysql服务器主机
remotePort:3306,//mysql服务器端口
localPort:33333,//可用的本地端口
verbose:true,//将信息转储到标准输出
disabled:false,//将其设置为true以禁用隧道(用于保持本地连接的体系结构)
sshConfig:{//ssh2配置(https://github.com/mscdex/ssh2)
主持人:“你的主持人”,
港口:22,
用户名:'user_on_tunneling',
密码:“pwd”
//privateKey:require('fs')。readFileSync(“”),
//密码短语:“verySecretString”//选项请参见ssh2配置
}
},
/**
*通过隧道初始化mysql连接。一旦它被创建,请回调调用者
*
*@param回调
*/
init:函数(回调){
//
//SSH隧道创建
//
var me=这个;
me.tunnel=新隧道(此.tunnelConfig);
me.tunnel.connect(函数(错误){
console.log('Tunnel connected',错误);
//
//连接到数据库
//
me.connection=me.connect(回调);
});
},
/**
*Mysql连接错误处理
*
*@param err
*/
errorHandler:函数(err){
var me=这个;
//
//检查丢失的连接并尝试重新连接
//
如果(err.code==='协议\连接\丢失'){
log('MySQL连接丢失。正在重新连接');
me.connection=me.connect();
}else if(err.code=='econnreference'){
//
//如果连接被拒绝,则继续尝试每3秒重新连接一次
//
log('MySQL连接被拒绝。请稍后重试。'+err);
setTimeout(函数(){
me.connection=me.connect();
}, 3000);
}
},
/**
*如果由于任何原因连接失败,请每3秒重试一次连接mysql服务器
*
*@param回调
*@returns{*}创建的mysql连接
*/
连接:函数(回调){
var me=这个;
//
//创建mysql连接对象
//
var connection=mysql.createConnection(me.dbServer);
connection.on('error',me.errorHandler);
//
//尝试连接
//
connection.connect(函数(err){
如果(错误)抛出错误;
log('Mysql作为id连接'+connection.threadId);
if(callback)callback();
});
回路连接;
}
}
);
};
我更新了tunnel ssh 1.1.0的代码示例,因为它实际上是internet上唯一可用的示例(到目前为止我搜索了..)。
配置这个新的隧道ssh非常麻烦
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;
}
}
);
};
var mysql=require('mysql');
var Tunnel=require('Tunnel-ssh');
module.exports=函数(服务器){
返回新对象({
tunnelPort:33333,//实际上可以是用于隧道的任何自由端口
/**
*数据库服务器配置。请注意,由于隧道连接,服务器主机
*是localhost,服务器端口是隧道端口。这是因为隧道
*在本地主机上创建本地端口
*/
dbServer:server | |{
主持人:“127.0.0.1”,
港口:33333,
用户:“用户名”,
密码:“yourpwd”,
数据库:“yourdb”
},
/**
*SSH隧道的默认配置
*/
隧道配置:{
remoteHost:'127.0.0.1',//mysql服务器主机
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);
});
});
})
}