【问题标题】:Unable to connect to MySQL Database with Java through SSH无法通过 SSH 使用 Java 连接到 MySQL 数据库
【发布时间】:2018-09-26 23:28:18
【问题描述】:

我知道关于这个问题已经有很多问题了,相信我我已经看过所有这些问题了。

一些背景知识,我正在使用 IntelliJ,我已经能够使用 IntelliJ 的数据源工具连接到数据库,并且我已经能够使用终端通过 SSH 连接到服务器本身。出于某种原因,即使我尝试用 Java 做同样的事情,它也不起作用。这是我的代码:

String serverIP = "123.45.67.890"
String username = "root";
String password = "password";

// Establish SSH tunnel with server if not running locally
try {
    if (!Inet4Address.getLocalHost().getHostAddress().equals(ServerIP) {
        System.out.println("Remote connection detected.");
        System.out.println("Establishing SSH Tunnel...");
        JSch jSch = new JSch();
        Session session = jSch.getSession(username, ServerIP, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        System.out.printf("Connecting to remote server at %s...\n", ServerIP);
        session.connect();
        System.out.println("Connected!");
        session.setPortForwardingL(3306, ServerIP, 3306);
    }
} catch (Exception e) {
    e.printStackTrace();
}

// Load mySQL Driver
System.out.println("Loading driver...");
try {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
    throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}

String url = String.format("jdbc:mysql://%s:3306/db_name", ServerIP);

// Connect to database
System.out.println("Connecting to database...");

try (Connection connection = DriverManager.getConnection(url, username, password)) {
    System.out.println("Database connected!");
    this.connection = connection;
} catch (SQLException e) {
    this.connection = null;
    throw new IllegalStateException("Cannot connect to database!", e);
}

这是我得到的错误:

Remote connection detected.
Establishing SSH Tunnel...
Connecting to remote server at 123.45.67.890...
Connected!
Loading driver...
Driver loaded!
Connecting to database...

java.lang.IllegalStateException: Cannot connect to database!

    ...
    a bunch of crap
    ...
Caused by: java.sql.SQLException: null,  message from server: "Host 'my.personal.computer.local.hostname' is not allowed to connect to this MySQL server"
    ...
    a bunch more crap
    ...

在我看来,出于某种原因,Java 代码没有使用我投入大量精力复制 StackOverflow 代码来制作的 SSH 隧道,而是粗鲁地尝试正常连接。我做错了什么?

更多信息:服务器本身配置为在 0.0.0.0 上侦听 SQL,我还将防火墙设置为接受端口 3306 上的连接。

为清楚起见进行编辑:我知道我可以通过在服务器上授予本地计算机权限来解决此问题,但我希望能够通过 SSH 连接。

【问题讨论】:

  • xkcd 979:另外,如果因为在连接关闭后进行更改而出现错误,则第三个try 块实际上会在连接完成后关闭连接;有点像 Java 等价于 Python 的 with 语句。
  • @Cœur 好的,谢谢!

标签: mysql sql ssh


【解决方案1】:

OP 的解决方案。

SSH 不工作的原因是因为我需要使用本地开放端口,通过它连接到 SQL,然后将其转发到远程端口 3306。另外,我需要通过 localhost 转发端口,而不是服务器地址. (即,将session.setPortForwardingL(3306, ServerIP, 3306); 行更改为session.setPortForwardingL(some-open-port, "localhost", 3306);,将"jdbc:mysql://%s:3306/db_name" 更改为"jdbc:mysql://%s:same-open-port/db_name"。

【讨论】:

    猜你喜欢
    • 2010-12-30
    • 2019-04-05
    • 1970-01-01
    • 2016-10-25
    • 2013-08-06
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    相关资源
    最近更新 更多