简体   繁体   中英

initiating sequelizer mysql connection via tunnel-ssh module

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

localPort is the port listening on your local system. Currently you have it defined as 27000 but your Sequelize config is set to 3306.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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