使用java通过ssh连接到远程mysql数据库 [英] connect to remote mysql database through ssh using java

查看:1034
本文介绍了使用java通过ssh连接到远程mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过SSH从Java应用程序连接到远程MySQL数据库。小代码示例对我很有帮助,我很感激。

How can I connect to remote MySQL database through SSH from java application. Small code example is helpful for me and I'd appreciate this.

推荐答案

我的理解是你要访问一个mysql服务器在远程机器上运行并通过SSH隧道监听端口3306。

My understanding is that you want to access a mysql server running on a remote machine and listening on let's say port 3306 through a SSH tunnel.

要使用命令行ssh client从本地计算机上的端口1234到远程计算机上的端口3306创建此类隧道,您可以从本地键入以下命令machine:

To create such a tunnel from port 1234 on your local machine to port 3306 on a remote machine using the command line ssh client, you would type the following command from your local machine:

ssh -L 1234:localhost:3306 mysql.server.remote

要从Java做同样的事情,你可以使用 JSch ,SSH2的Java实现。从其网站:

To do the same thing from Java, you could use JSch, a Java implementation of SSH2. From its website:


JSch允许您连接到sshd服务器并使用端口转发,X11转发,文件传输等,以及您可以将其功能集成到您自己的Java程序中。 JSch根据BSD风格许可证授权。

JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under BSD style license.

举个例子,看看 PortForwardingL.java 。连接会话后,使用 jdbc:mysql:// localhost:1234 / [database] 等连接URL创建与MySQL的JDBC连接。

For an example, have a look at PortForwardingL.java. Once the session connected, create your JDBC connection to MySQL using something like jdbc:mysql://localhost:1234/[database] as connection URL.

这篇关于使用java通过ssh连接到远程mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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