Node.js使用tunnel-ssh建立与远程mysql服务器的连接

时间:2016-04-13 09:06:42

标签: mysql node.js ssh

我使用tunnel-ssh模块使用node.js建立与远程mysql数据库的连接。文档很差,我无法建立连接。以下是代码段:

var tunnel = require('tunnel-ssh')
var server = tunnel.tunnel(config.sshData, function(err, result) {console.log('connected'});

这是我的sshData对象。

config.sshData = {host : 'serverxyz.web-hosting.com', username : 'xyz', password : 'xyz',
srcPort: 3307, dstPort : 21098}

dstPort 21098namecheap documentation建议的server.on('error', function(err) {});

但是我收到超时错误,每当我添加这个片段时:

server.on is not a function

我收到错误putty。远程连接在SQLyogssh2上正常运行。任何关于如何建立成功连接的程序都会有很大帮助。谢谢!

更新

使用指定的正确端口并使用<html> <head> <title>Toastr</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script> <link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" /> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <div class="container-fluid"> <h1>Toastr bottom test</h1> </div> <script type="text/javascript"> $(function() { function Toast(type, css, msg) { this.type = type; this.css = css; this.msg = 'This is positioned in the ' + msg + '. You can also style the icon any way you like.'; } var toasts = [ new Toast('error', 'toast-bottom-full-width', 'This is positioned in the bottom full width. You can also style the icon any way you like.'), new Toast('info', 'toast-top-full-width', 'top full width'), new Toast('warning', 'toast-top-left', 'This is positioned in the top left. You can also style the icon any way you like.'), new Toast('success', 'toast-top-right', 'top right'), new Toast('warning', 'toast-bottom-right', 'bottom right'), new Toast('error', 'toast-bottom-left', 'bottom left') ]; toastr.options.positionClass = 'toast-top-full-width'; toastr.options.extendedTimeOut = 0; //1000; toastr.options.timeOut = 1000; toastr.options.fadeOut = 250; toastr.options.fadeIn = 250; var i = 0; delayToasts(); function delayToasts() { if (i === toasts.length) { return; } var delay = i === 0 ? 0 : 2100; window.setTimeout(function () { showToast(); }, delay); // re-enable the button if (i === toasts.length-1) { window.setTimeout(function () { $('#tryMe').prop('disabled', false); i = 0; }, delay + 1000); } } function showToast() { var t = toasts[i]; toastr.options.positionClass = t.css; toastr[t.type](t.msg); i++; delayToasts(); } }) </script> </body> </html>模块直接使用here给出的代码示例使数据库工作

1 个答案:

答案 0 :(得分:1)

对namecheap文档存在误解。 21098是 ssh 端口,而不是数据库正在侦听的端口。要使用非标准ssh端口,您需要明确指定port值,如:

config.sshData = {
  host: 'serverxyz.web-hosting.com',
  port: 21098,
  username: 'xyz',
  password: 'xyz',
  dstPort: 3306
};

然后您应该能够连接到localhost:3306以访问您的远程数据库。