Node.js使用tunnel-sshbuild立与远程mysql服务器的连接
我正在使用tunnel-ssh
模块使用node.jsbuild立到远程mysql
数据库的连接。 文档很差,我无法build立连接。 这里是代码片段:
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}
21098
文档build议dstPort
是21098
。
不过,我收到超时错误,每当我添加此片段:
server.on('error', function(err) {});
我得到的错误server.on is not a function
。 远程连接在putty
和SQLyog
上工作正常。 任何有关如何build立成功连接的程序都会有很大的帮助。 谢谢!
更新
通过使用指定的正确端口,并通过直接使用ssh2
模块与这里给出的代码示例获得数据库的工作
名称文档存在误解。 21098是ssh端口,不是数据库正在监听的端口。 为了使用非标准的ssh端口,你需要明确地指定port
值,如:
config.sshData = { host: 'serverxyz.web-hosting.com', port: 21098, username: 'xyz', password: 'xyz', dstPort: 3306 };
那么你应该能够连接到localhost:3306
来访问你的远程数据库。