我无法使用 JDBC 连接通过 SSH-隧道连接到 Azure DB.出于安全原因,我不能直接访问 Azure DB,但我有一个跳转服务器隧道虚拟机,可以用来间接连接到 DB。
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驱动的客户端,就可以工作。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.".
127.0.0.1:randomport <--> 127.0.0.1:1433
),但随后切换到隧道外,使用 my-external-IP:randomport <--> xxx.yyy.zzz.worker.database.windows.net:11111
由于防火墙的原因而失败。我缺少什么来让这个运行?
使用的驱动。
测试程序。
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();
}
}
}
}
的,但我有一个跳转服务器隧道的虚拟机,可以用它来连接Azure DB。连接政策 的DB服务器需要 代理 以便与ssh隧道一起工作。
有关可用连接策略差异的详细内容可以在 Azure SQL数据库和Azure Synapse Analytics连接架构 文章: