[英]I can't to connect to DB with ssh-tunnel using java.
i have sql-BD with ssh-tunnel. 我有带ssh-tunnel的sql-BD。 i want connect to this BD using Java 我想使用Java连接到此BD
the code i use: 我使用的代码:
Connection con = null;
JSch jsch = new JSch();
int localPort = 1234;
Session session = jsch.getSession(proxyUser, proxyHost, 22);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(proxyPassword);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect();
session.setPortForwardingL(localPort, proxyHost, 3306);
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
properties.setProperty("useUnicode", "true");
properties.setProperty("characterEncoding", "UTF-8");
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(
"jdbc:mysql://" + url + ":3306/" + nameBD + "?autoReconnect=true", properties);
Error line "Could not create connection to database server. Attempted reconnect 3 times. Giving up" 错误行“无法创建与数据库服务器的连接。尝试重新连接3次。放弃”
Error while executing this: " con = DriverManager.getConnection(...) " 执行此操作时出错:“ con = DriverManager.getConnection(...) ”
Help me please 请帮帮我
Debugging something like this is probably best done with wireshark , which will allow you to see what's actually happening on the wire, and quickly work out what's not quite right (tm) . 最好使用wireshark调试类似的东西,这将使您看到实际发生的事情,并迅速找出不正确的地方(tm) 。 If you're not familiar with the tool, there are plenty of tutorials around to choose from. 如果您不熟悉该工具,则有很多教程可供选择。
I think that mostly you just need to get things to match and it'll work ok: 我认为大多数情况下,您只需要使事情匹配就可以了,并且可以正常进行:
LocalPort
is currently set to 1234
, but DriverManager.getConnection
is trying to connect to 3306
. LocalPort
当前设置为1234
,但是DriverManager.getConnection
试图连接到3306
。 url
is set to 脚本代码段不完整,因此我假设url
设置为 jsch.getSession
and session.setPortForwardingL
both use proxyHost
as a destination: I assume you have a firewall on the host (which is why you're using ssh) so trying to connect locally to the external interface address may also be blocked. jsch.getSession
和session.setPortForwardingL
都使用proxyHost
作为目的地:我假设您的主机上有防火墙(这就是您使用ssh的原因),因此尝试本地连接到外部接口地址也可能被阻止。 Which means that session.setPortForwardingL
should probably be localhost/127.0.0.1 too. 这意味着session.setPortForwardingL
可能也应该是localhost / 127.0.0.1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.