initiating sequelizer mysql connection via tunnel-ssh module

时间:2016-07-11 23:07:29

标签: mysql node.js ssh

I'd like to connect to a MySQL database using Sequelizer. Right now, I'm getting a Connection Refused Error.

To access the database, I have to SSH in. According to Mick Hansen here: https://github.com/sequelize/sequelize/issues/3753, one way to SSH in is to use tunnel-ssh to establish the tunnel, then initiate Sequelizer.

My (unsuccessful) approach so far has been to initiate the tunnel, then when the tunnel opens, test whether Sequelizer has authenticated.

Update

  • Host: DigitalOcean
  • CLI Success: I can 1) ssh into digitalocean server 2) login into mysql from the server and 3) access all database information as the root user.
  • Sequel Pro: I can also log into the database using Sequel Pro.
  • MySQL 127.0.0.1:3306: Based on the mysql/my.cnf file, the port is 3306 and the bind-address is 127.0.0.1. The config file also says instead of skip-networking, the default is to listen only on localhost, if that's relevant.
  • socketPath -> Error Connection Switching from TCP to socket seems to sometimes work for this type of problem, but when I tried it, I continued to get a connection refused error.
  • 2 Error Types - "All Configured Authentication Methods Failed" and "Error Connection Refused"

Thanks for the help!

Code:

// sequelize config
 var sequelize = new Sequelize('database', 'user', 'pass', {
  host: '127.0.0.1',
  dialect: 'mysql',
  port: 3306,

  pool: {
    max: 10,
    min: 0,
    idle: 20000
  }
});

// tunnel config
var config = {
      user:'user',
      host:'sshHost',
      port:22,
      dstHost:'127.0.0.1',
      dstPort:3306,
      srcHost:'127.0.0.1',
      srcPort:3306,
      localHost:'127.0.0.1',
      localPort: 3306,
      privateKey:require('fs').readFileSync('/path/to/key')
    };

var tunnel = require('tunnel-ssh');

// initiate tunnel
tunnel(config, function (error, server) {
  //....
  if(error) {
    console.error(error);
  } else {
    console.log('server:', server);

  // test sequelize connection
    sequelize
        .authenticate()
        .then(function(err) {
            console.log('Connection established');
        })
        .catch(function(err) {
            console.error('unable to establish connection', err);
        })
  }
})

When my config is set to the object above, I get an "All configuration methods failed error".

If I change my config to the below, I get a "Sequelize Error Connection Refused" error.

// tunnel config
    var config = {
          user:'user',
          host:'sshHost',
          port:22,
          dstHost:'127.0.0.1',
          dstPort:3306,
          //srcHost:'127.0.0.1',
          //srcPort:3306,
          //localHost:'127.0.0.1',
          //localPort: 3306,
          privateKey:require('fs').readFileSync('/path/to/key')
        };

1 个答案:

答案 0 :(得分:0)

localPort是侦听本地系统的端口。目前,您将其定义为27000,但您的Sequelize配置设置为3306。