如何使用IntelliJ和JDBC进行SSH隧道连接并连接到数据库? [英] How to SSH tunnel and connect to a database using IntelliJ and JDBC?
问题描述
我在使用IntelliJ和JDBC连接到服务器上托管的数据库时遇到问题.在命令行中,命令:
I'm having issues connecting to a database hosted on a server using IntelliJ and JDBC. With the command line, the command:
ssh username@server -L 11100:ct1:28017 -N
成功创建隧道,并执行以下命令:
creates a tunnel successfully and the command:
psql db_name -h localhost -p 11100 db_username
可以很好地连接到数据库,并允许我使用它.如何将其正确转换为IntelliJ数据源和SSH隧道工具?
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?
目前,对于SSH隧道设置,我有:
At the moment, for the SSH tunnel settings I have:
代理主机:服务器
端口:22
代理用户:用户名
对于常规"标签,我有:
For the general tab I have:
主机:本地主机
端口:28017
数据库:db_name
database: db_name
用户:db_username
user: 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);
URL参数是IntelliJ生成的参数.但是,这给了我一个错误拒绝与localhost:28017的连接."
The URL parameter is the one generated by IntelliJ. This however gives me an error "Connection to localhost:28017 refused."
关于我在做什么错的任何想法吗?
Any ideas on what I am doing wrong?
推荐答案
Do you connect to SSH server from a java client like SSHJ (https://github.com/hierynomus/sshj) ?
DriverManager上的URL似乎是错误的:请尝试使用端口11100.
The URL on your DriverManager seems wrong: try port 11100 instead.
这篇关于如何使用IntelliJ和JDBC进行SSH隧道连接并连接到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!