Cryn
Cryn

Reputation: 1025

Failing to connect to Azure DB via SSH-Tunnel using a JDBC connection

I am unable to connect to an Azure DB via SSH-Tunnel using a JDBC connection. For security reasons, I cannot directly access the Azure DB, but I have a jump server / tunnel VM that I can use to indirectly connect to the DB.

What am I missing to get this running?

Drivers used:

Test program:

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();
      }
    }
  }
}

Upvotes: 1

Views: 1188

Answers (1)

Cryn
Cryn

Reputation: 1025

The connection policy of the DB server needs to be proxy in order to work with the ssh tunnel.

Details on the differences of the available connection policies can be found in the Azure SQL Database and Azure Synapse Analytics connectivity architecture article.

Upvotes: 1

Related Questions