使用Java通过SSH连接远程MySQL数据库
如何从 java 应用程序通过 SSH 连接到远程 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的理解是,您想要访问在远程计算机上运行的 mysql 服务器,并通过 SSH 隧道监听端口 3306。
要使用命令行 ssh 客户端创建从本地计算机上的端口 1234 到远程计算机上的端口 3306 的隧道,您可以从本地计算机键入以下命令:
要从 Java 执行相同的操作,您可以使用 JSch,SSH2 的 Java 实现。从其网站:
例如,请查看
PortForwardingL.java
。连接会话后,使用jdbc:mysql://localhost:1234/[database]
作为连接 URL 创建到 MySQL 的 JDBC 连接。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.
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:
To do the same thing from Java, you could use JSch, a Java implementation of SSH2. From its website:
For an example, have a look at
PortForwardingL.java
. Once the session connected, create your JDBC connection to MySQL using something likejdbc:mysql://localhost:1234/[database]
as connection URL.我的详细代码如下:
My detail code is below:
虽然现有答案是正确的,但它们掩盖了其他代码膨胀中的重要代码。
这是通过 SSH 通道隧道 JDBC(或任何其他)数据库连接所需的基本代码:
>您还必须处理主机密钥验证。为此,请参阅:
如何在使用 JSch SFTP 库时解析 Java UnknownHostKey?
While the existing answers are correct, they obscure the significant code in bloats of other code.
This is the basic code you need to tunnel your JDBC (or any other) database connection through an SSH channel:
You will also have to deal with host key verification. For that see:
How to resolve Java UnknownHostKey, while using JSch SFTP library?
首先感谢你的作品非常棒!
不过,我想知道是否应该为每个(可能同时发生的)SQL 连接重用该会话,或者是否应该每次都创建一个新会话,并且仅在由于某种原因过期时才刷新它。
目前,每次建立连接时,我都会在此处创建该控制器的一个新实例,然后使用从中获得的连接执行 SQL 查询,然后手动关闭它。
如果我能让该类可与 try-with-resource 一起使用并自行关闭,那就太好了。会调查这一点。因为我不想错过关闭它的机会。
事情就是这样,我现在正在获取数据库连接。
如果您想知道我自己也制作了 DbSettingsController,只需将设置放入本地 SQLite DB 的文本列中,并为其分配一个键(该枚举的 int 值)。只是复制粘贴我从其他项目中重用的代码,因此这样做既简单又快速。
First of all, thank you works great!
Though, I wonder if I should reuse that Session for every (potentially simultaneous) SQL Connection, or if I should create a new Session every time and only refresh it if for some reason it has expired.
Currently, I would every time I make a connection make a new instance of that Controller here and then do the SQL queries with the connection I got from it, then close it manually.
Would also be nice if I could make the class useable with try-with-resource and it closing itself. Will look into that. Cause I don´t wanna miss closing it.
That´s how the thing looks like, I'm getting DB Connections from right now.
If you wonder DbSettingsController I´ve made myself too, just puts settings in a Text column in a local SQLite DB, with a key assigned to it (that enum´s int value). Was just copy paste code I reused from some other project, so it was simple and fast to just do that this way.