java 如何使用 IntelliJ 和 JDBC 建立 SSH 隧道并连接到数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35337054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to SSH tunnel and connect to a database using IntelliJ and JDBC?
提问by echoeida
I'm having issues connecting to a database hosted on a server using IntelliJ and JDBC. With the command line, the command:
我在使用 IntelliJ 和 JDBC 连接到托管在服务器上的数据库时遇到问题。使用命令行,命令:
ssh username@server -L 11100:ct1:28017 -N
creates a tunnel successfully and the command:
成功创建隧道,命令:
psql db_name -h localhost -p 11100 db_username
connects to the database fine and allows me to use it. How would this be translated correctly into the IntelliJ Data Sources and the SSH tunnel tools?
连接到数据库很好,并允许我使用它。这将如何正确转换为 IntelliJ 数据源和 SSH 隧道工具?
At the moment, for the SSH tunnel settings I have:
目前,对于 SSH 隧道设置,我有:
Proxy Host: server
代理主机:服务器
Port: 22
端口:22
Proxy user: username
代理用户:用户名
For the general tab I have:
对于常规选项卡,我有:
host: localhost
主机:本地主机
port: 28017
端口:28017
database: db_name
数据库:db_name
user: db_username
用户:db_username
The connection returns successfully after testing the connection. Inside my program I have:
连接测试后连接成功返回。在我的程序中,我有:
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:28017/db_name", user, password);
The URL parameter is the one generated by IntelliJ. This however gives me an error "Connection to localhost:28017 refused."
URL 参数是 IntelliJ 生成的参数。然而,这给了我一个错误“连接到 localhost:28017 被拒绝”。
Any ideas on what I am doing wrong?
关于我做错了什么的任何想法?
采纳答案by Xvolks
Do you connect to SSH server from a java client like SSHJ (https://github.com/hierynomus/sshj) ?
您是否从像 SSHJ ( https://github.com/hierynomus/sshj)这样的 Java 客户端连接到 SSH 服务器?
The URL on your DriverManager seems wrong: try port 11100 instead.
DriverManager 上的 URL 似乎有误:请尝试使用端口 11100。
回答by gmode
Here's my setup via intellij if anyone is looking for simple solution without external libraries.
如果有人正在寻找没有外部库的简单解决方案,这是我通过 intellij 的设置。
- Open Database view (View -> Tool Windows -> Database).
- Add new DataSource (right-click, New -> Data Source -> Postgres/Mysql). In host, use 127.0.0.1 (not localhost, sometimes localhost is not properly resolved).
- Fill in rest of the fields as configured on your remote host.
- 打开数据库视图(视图 -> 工具窗口 -> 数据库)。
- 添加新的数据源(右键单击,新建 -> 数据源 -> Postgres/Mysql)。在主机中,使用 127.0.0.1(不是本地主机,有时本地主机没有正确解析)。
- 填写远程主机上配置的其余字段。
Now click on SSH/SSL tab and configure ssl tunnel:
现在单击 SSH/SSL 选项卡并配置 ssl 隧道: