使用 JDBC 连接通过 SSH-Tunnel 连接到 Azure DB 失败。

问题描述 投票:0回答:1

我无法使用 JDBC 连接通过 SSH-隧道连接到 Azure DB.出于安全原因,我不能直接访问 Azure DB,但我有一个跳转服务器隧道虚拟机,可以用来间接连接到 DB。

  • 命令行访问(通过JDBC)到Azure DB可以从隧道虚拟机工作。
  • 建立一个SSH隧道(ssh -f vm_user@tunnel_vm_host -L 127.0.0.1:1433:mydb-server.database.windows.net:1433 -N),然后连接到Azure DB,通过 127.0.0.1:1433[email protected] 如果我使用一个使用MS OLE DB SQL驱动的客户端,就可以工作。
  • 使用 MS SQL JDBC 驱动程序建立连接失败。com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host xxx.yyy.zzz.worker.database.windows.net, port 11111 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
  • 从我在Wireshark上看到的情况来看,如果使用JDBC驱动程序,则使用隧道开始与Azure DB进行通信(127.0.0.1:randomport <--> 127.0.0.1:1433),但随后切换到隧道外,使用 my-external-IP:randomport <--> xxx.yyy.zzz.worker.database.windows.net:11111由于防火墙的原因而失败。

我缺少什么来让这个运行?

使用的驱动。

  • JDBC驱动版本是mssql 8.2.2.0 jre11。
  • MS OLE DB Driver for SQL Server 18.3.0.0.

测试程序。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class AzureDbTest {
  public static void main(final String[] args) throws SQLException {
    final String user = "[email protected]";
    final String password = "password";
    final int portNumber = 1433;
    final String databaseName = "mydb";
    final String serverName = "127.0.0.1";
    final String url = String.format("jdbc:sqlserver://%s:%s;database=%s;user=%s;password=%s;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
        serverName, portNumber, databaseName, user, password);
    Connection connection = null;
    try {
      connection = DriverManager.getConnection(url);
      final String schema = connection.getSchema();
      System.out.println("Successful connection - Schema: " + schema);
    } catch (final Exception e) {
      e.printStackTrace();
    } finally {
      if (connection != null) {
        connection.close();
      }
    }
  }
}
jdbc azure-sql-database ssh-tunnel
1个回答
0
投票

的,但我有一个跳转服务器隧道的虚拟机,可以用它来连接Azure DB。连接政策 的DB服务器需要 代理 以便与ssh隧道一起工作。

有关可用连接策略差异的详细内容可以在 Azure SQL数据库和Azure Synapse Analytics连接架构 文章:

© www.soinside.com 2019 - 2024. All rights reserved.