7

When using the node-mysql npm package, is it possible to connect to the MySQL server using a SSH key instead of a password?

3 Answers 3

10

You can do the SSH tunnel component completely independently, and then point node-mysql (or any other sql client...) to your DB by using TCP tunneled over SSH.

Just set up your SSH tunnel like this

ssh -N -p 22 [email protected] -L 33306:localhost:3306

Leave that going in the background (see articles like this for more in depth info).

Then just send any MySQL client to port 33306 on localhost. It will actually connect as though you are on your remote server and using port 3306.

2
  • This actually worked! What's the best method of making sure the connection is always running?
    – Jack
    Dec 12, 2017 at 3:06
  • I'm not sure what the best method is, but this may be a start.
    – Steven Lu
    Dec 12, 2017 at 4:11
3

Thanks so much Steve your answer help me alot. just to make it clearer use

ssh -f [email protected] -L 2000:personal-server.com:25 -N

The -f tells ssh to go into the background just before it executes the command. This is followed by the username and server you are logging into. The -L 2000:personal-server.com:25 is in the form of -L local-port:host:remote-port. Finally the -N instructs OpenSSH to not execute a command on the remote system

To connect to mongo use whatever port you set as Local port (in this case the port is 2000)

For example let's say I want to connect on a remote server with IP 192.168.0.100 and mongo is running on port 27017.

Assume a user called elie with password eliepassword has access to ssh on port 22 I will have to do this First run on the terminal the following :

ssh -f [email protected] -L 2002:127.0.0.1:27017 -N

In my mongo connection I will do :

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:2002/mydatabase');

module.exports = mongoose.connection;

I hope this makes it clear.

0
-2
const mysql = require('mysql2');
const { Client } = require('ssh2');
const sshClient = new Client();
const dbServer = {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE
}
const tunnelConfig = {
    host: process.env.DB_SSH_HOST,
    port: 22,
    username: process.env.DB_SSH_USER,
    password: process.env.DB_SSH_PASSWORD
}
const forwardConfig = {
    srcHost: '127.0.0.1',
    srcPort: 3306,
    dstHost: dbServer.host,
    dstPort: dbServer.port
};
const SSHConnection = new Promise((resolve, reject) => {
    sshClient.on('ready', () => {
        sshClient.forwardOut(
        forwardConfig.srcHost,
        forwardConfig.srcPort,
        forwardConfig.dstHost,
        forwardConfig.dstPort,
        (err, stream) => {
             if (err) reject(err);
             const updatedDbServer = {
                 ...dbServer,
                 stream
            };
            const connection =  mysql.createConnection(updatedDbServer);
           connection.connect((error) => {
            if (error) {
                reject(error);
            }
            resolve(connection);
            });
});
}).connect(tunnelConfig);
});
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.