繁体   English   中英

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

[英]Node.js ORM mysql connect via SSH tunnel

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

我更新了tunnel-ssh 1.1.0的代码示例,因为它实际上是互联网上唯一的工作示例(到目前为止我搜索过了...)。 让这个新的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;
            }
        }
    );

};

最后通过删除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;
            }
        }
    );

};

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

感谢此主题中的现有答案。 以下解决方案适合我:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM