Node.js ORM mysql通过SSH隧道连接

佐尔坦·玛雅(Zoltan Magyar)

我正在尝试建立一个使用node-orm2的node.js应用程序。但是,我们的云托管数据库只能通过SSH隧道连接。检查ORM文档我看不到任何通过SSH隧道连接到数据库的配置选项。有什么方法可以设置此设置,或者我需要找到某种无需SSH即可连接的方法?

约书亚(Joshua Angnoe)

我更新了tunnel-ssh 1.1.0的代码示例,因为它实际上是Internet上唯一有效的示例(到目前为止,我一直在搜索..)。配置此新的tunnel-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;
            }
        }
    );

};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Node.js ORM mysql通过SSH隧道连接

来自分类Dev

通过SSH连接的Node.js

来自分类Dev

NET实体框架之类的Node JS的ORM?

来自分类Dev

如何使用Node.js建立与MongoDB数据库的SSH隧道连接

来自分类Dev

使用sequelize ORM连接到node.js上的Amazon RDS Postgres数据库的问题

来自分类Dev

无法在Node js中创建书架orm表

来自分类Dev

在Sails.js中使用其他ORM(例如node-orm2)的“干净”方式?

来自分类Dev

通过单例的node.js mySQL连接

来自分类Dev

通过SSH无法使用node.js

来自分类Dev

Node JS TCP代理:使用Node JS设置HTTP隧道

来自分类Dev

Node JS TCP代理:使用Node JS设置HTTP隧道

来自分类Dev

Node.js无法与MySQL,PHPMyAdmin连接

来自分类Dev

Node.js mySQL 无法创建连接

来自分类Dev

node.js 不会连接到 mysql

来自分类Dev

Node.js orm2-在OneToMany关系中检索新关联时遇到麻烦

来自分类Dev

遵循数据映射器模式的Javascript node.js ORM

来自分类Dev

通过Node JS App在OpenShift中连接到MySQL数据库时出错

来自分类Dev

Vue.js Web应用程序使用带有mysql-ssh的Node.js连接

来自分类Dev

连接到远程SSH服务器(通过Node.js / html5控制台)

来自分类Dev

保持SSH连接在Node.js中的活动

来自分类Dev

使用node.js / mysql连接时中止连接

来自分类Dev

无法在Node js中连接

来自分类Dev

Node.js tcp 连接

来自分类Dev

Node.js 与 MongoDB 的连接

来自分类Dev

如何在Node JS中使用Node-ssh断开SSH连接

来自分类Dev

如何终止通过SSH启动的Node.js的NOHUP进程

来自分类Dev

使用Node.js通过SSL连接到MongoDB

来自分类Dev

如何通过Node.js与SQLite3建立连接

来自分类Dev

通过Node.js启动MongoDB,然后进行连接

Related 相关文章

热门标签

归档