通过SSH连接的Node.js [英] Node.js connecting through ssh

查看:208
本文介绍了通过SSH连接的Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以运行的node.js服务器,但需要为ssh连接进行设置:

I have a node.js server that works but needs to be set up for ssh connections:

var mysql = require('mysql')
var io = require('socket.io').listen(3000)
var db = mysql.createConnection({

    host: 'hostname',
    user: 'username',
    password: '12345',
    database: '12345',
    port: 3306,
    socket: '/var/run/mysqld/mysqld.sock' 
})

db.connect(function(err){
    if (err) console.log(err)
})

我知道有用于此目的的ssh npm库,但是可用的选项(ssh2,node-sshclient等)似乎处理了可能使事情复杂化的复杂功能.我正在寻找通过ssh连接到我的mysql数据库的最简单方法.最好的方法是什么?

I'm aware that there are ssh npm libraries for this purpose, however the options available (ssh2, node-sshclient, etc) appear to deal with pretty intricate features that may overcomplicate things. I'm looking for the simplest way to connect to my mysql db through ssh. What would be the best way to accomplish this?

推荐答案

如果您正在运行linux/unix系统,请执行以下操作:

If you are running a linux/unix system do the following:

通过ssh连接到您的mysql服务器,并通过此ssh隧道代理mysql端口(默认为3306).

Connect to your mysql server via ssh and proxy the mysql port (default is 3306) via this ssh tunnel.

其工作原理如下:

1 (键入screen)(以启动一个屏幕会话,即使关闭外壳,该会话也是永久的).

1 Type in screen (to start a screen session which is permanent even if the shell gets closed).

2 键入屏幕外壳:

ssh -L 3306:127.0.0.1:3306 your_servers_domain_or_ip -lyour_login_name

3 输入您的ssh密码/或使用PKI身份验证以避免手动操作

3 Enter your ssh password / or use a PKI auth to avoid manual steps

4 完成...现在,可以像在将MySQL与应用程序安装在同一台计算机上一样连接MySQL.

4 Done... now it’s possible to connect MySQL like you would do when it’s installed on the same machine as your application.

从node.js连接到MySQL,如下所示:

Connect to MySQL from node.js like below:

var db = mysql.createConnection({
    host: '127.0.0.1', // Important to connect to localhost after connecting via ssh in screen
    user: 'username',
    password: '12345',
    database: '12345',
    port: 3306
});

这篇关于通过SSH连接的Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆