4

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

4

4 回答 4

7

我更新了 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;
            }
        }
    );

};
于 2015-08-26T15:53:42.387 回答
2

最后,通过删除 orm2 并使用 node-mysql 和 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) {
                //
                // 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;
            }
        }
    );

};
于 2014-11-27T09:46:20.437 回答
0

您可以使用settings参数将对象node-orm2传递options给底层驱动程序。例如,如果您使用的是 a mysqlthen 您可以传递一个ssl选项。在这种情况下,请参阅https://github.com/felixge/node-mysql#ssl-options 。

于 2014-08-03T04:44:14.493 回答
0

感谢此线程中的现有答案。以下解决方案对我有用:

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);
            });
        });
    })
}
于 2018-01-04T00:55:04.193 回答