使用java通过ssh连接到远程mysql数据库

使用java通过ssh连接到远程mysql数据库

本文介绍了使用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做同样的事情,你可以使用,SSH2的Java实现。从其网站:

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

举个例子,看看。连接会话后,使用 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数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:02