azure-data-factoryssh-tunnel

How can I link ADF to a MySQL DB


I'm trying to create a service linked to a MySQL DB. The issue I have is that MySSQL is expecting an SSH key, but using the connected service for MySQL does not give an option to enter SSH details. Normally in code, I'd supply localhost as well as the SSH file/key.

linked service input screenshot

How can I add SSH details to the connection? I need a Linked service to an MySql DB, The MySql is configured to use SSH. Setting the linked service up as below and pressing 'Test Connection', I receive a timeout and only assume it's because I've not passed an SSH Key.

If I was writing an Azure Function, I'd connect in the following way:

    var server = "server ip for MySQL";
    var sshUserName = "sshusernameHere";
    var sshPassword = "sshpasswordhere";
    var databaseUserName = "mysqldatabaseuser";
    var databasePassword = "mysqldatabasepassword";

    var (sshClient, localPort) = ConnectSsh(server, sshUserName, sshPassword, "path to my ssh key file");
    using (sshClient)
    {
        MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder
        {
            Server = "127.0.0.1",
            Port = localPort,
            UserID = databaseUserName,
            Password = databasePassword,
            Database = "databasename"
        };

Where ConnectSsh is:

public static (SshClient SshClient, uint Port) ConnectSsh(string sshHostName, string sshUserName, string sshPassword = null,
string sshKeyFile = null, string sshPassPhrase = null, int sshPort = 22, string databaseServer = "localhost", int databasePort = 3306)
{
    // check arguments
    if (string.IsNullOrEmpty(sshHostName))
        throw new ArgumentException($"{nameof(sshHostName)} must be specified.", nameof(sshHostName));
    if (string.IsNullOrEmpty(sshHostName))
        throw new ArgumentException($"{nameof(sshUserName)} must be specified.", nameof(sshUserName));
    if (string.IsNullOrEmpty(sshPassword) && string.IsNullOrEmpty(sshKeyFile))
        throw new ArgumentException($"One of {nameof(sshPassword)} and {nameof(sshKeyFile)} must be specified.");
    if (string.IsNullOrEmpty(databaseServer))
        throw new ArgumentException($"{nameof(databaseServer)} must be specified.", nameof(databaseServer));

    // define the authentication methods to use (in order)
    var authenticationMethods = new List<AuthenticationMethod>();
    if (!string.IsNullOrEmpty(sshKeyFile))
    {
        authenticationMethods.Add(new PrivateKeyAuthenticationMethod(sshUserName,
            new PrivateKeyFile(sshKeyFile, string.IsNullOrEmpty(sshPassPhrase) ? null : sshPassPhrase)));
    }
    if (!string.IsNullOrEmpty(sshPassword))
    {
        authenticationMethods.Add(new PasswordAuthenticationMethod(sshUserName, sshPassword));
    }

    // connect to the SSH server
    var sshClient = new SshClient(new ConnectionInfo(sshHostName, sshPort, sshUserName, authenticationMethods.ToArray()));
    sshClient.Connect();

    // forward a local port to the database server and port, using the SSH server
    var forwardedPort = new ForwardedPortLocal("127.0.0.1", databaseServer, (uint)databasePort);
    sshClient.AddForwardedPort(forwardedPort);
    forwardedPort.Start();

    return (sshClient, forwardedPort.BoundPort);
}

Solution

  • You are using Auto resolved Integration runtime and enabled Not use system trust store, while connecting to SSH enabled on-premises MySQL database using MySQL linked service. That may be the reason to get error while testing the linked service. To resolve the error, you can follow below procedure:

    Configure Self Hosted Integration Runtime, use it in linked service, and enable Use system trust store and provide required details in linked service as shown below:

    enter image description here

    Then you will be able to test the linked service successfully as shown below:

    enter image description here

    For more information you can refer to this.