通过 SSH 和使用 Spring Boot 连接到 MySQL [英] Connect to MySQL through SSH and by using Spring Boot
问题描述
在我的 Spring Boot
应用程序 WAR 中,我有一个 application.properties
文件,其中我定义了以下 datasource
属性:
In my Spring Boot
app WAR, I have an application.properties
file wherein I have defined following datasource
properties:
spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true&useSSL=false&rewriteBatchedStatements=true
spring.datasource.username=used
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
有了这个,我就可以连接到数据库了.
with this, I am able to connect to the database.
现在,我想通过SSH 隧道
连接到另一个数据库.我应该如何指定 spring.datasource.url
来获得这样的连接?
Now, I want to connect to another database through SSH tunnel
. How should I specify spring.datasource.url
to get such connection?
请注意,我不想编写任何样板 Java 代码来执行此 SSH 隧道!
Please note I don't want to write any boiler plate Java code to do this SSH tunneling!
P.S.我用谷歌搜索但在任何地方都找不到满意的答案.
推荐答案
我不认为 spring 可以为您创建 SSH 隧道,如果这就是您正在寻找的.如果要在代码中做,就需要jsch这样的框架.
I don't think spring can create a SSH tunnel for you if that's what you're looking for. If you want to do it in the code, you need a framework like jsch.
最好的解决方案是在服务器上手动或使用 autossh(以保持运行)创建 SSH 隧道.要手动创建隧道,请登录到您的应用服务器并在屏幕中运行以下命令:
The best solution would be to create the SSH tunnel on the server manually or by using autossh (to keep it up). To create a tunnel manually, log on to your app server and run this command in a screen:
ssh -L 3307:localhost:3306 serverIp
3306 是运行 mysql 的端口.3307 是您要转发的端口.使用此隧道,您现在可以使用以下方式进行连接:
3306 is the port mysql is running on. 3307 is the port you are forwarding. With this tunnel you can now connect by using:
spring.datasource.url=jdbc:mysql://localhost:3307/
这篇关于通过 SSH 和使用 Spring Boot 连接到 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!