You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为ADF的MySQL链接服务配置SSH密钥认证?

问题描述

尝试创建连接至MySQL数据库的Azure数据工厂(ADF)链接服务,但目标MySQL数据库要求使用SSH密钥认证,而ADF的MySQL链接服务配置界面未提供SSH相关信息的输入选项,测试连接时出现超时,推测是未传入SSH密钥导致无法建立连接。

在Azure Function中,通过以下代码实现带SSH密钥认证的MySQL连接:

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"
    };
}

其中ConnectSsh方法实现如下:

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(sshUserName))
        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);
}
解决方案

ADF原生的MySQL链接服务不支持直接配置SSH隧道,以下是三种可行的替代方案:

方案1:使用自托管集成运行时(Self-Hosted IR)建立SSH隧道

  • 部署自托管集成运行时到一台可访问SSH服务器的机器上。
  • 在该机器上预先建立SSH隧道,将本地端口转发到MySQL数据库的端口:
    • 使用OpenSSH命令:ssh -i /path/to/ssh-key -L local_port:mysql_host:mysql_port ssh_username@ssh_host
    • 也可配置脚本实现开机自动建立隧道。
  • 在ADF中创建MySQL链接服务时,选择该自托管IR,将Server设为127.0.0.1,Port设为本地转发的端口,填写正常的数据库账号和密码即可。

方案2:通过Azure Function作为中间层

利用已有的Azure Function代码,将其封装为可被ADF调用的服务:

  • 修改Function代码,添加数据读取/写入逻辑,将MySQL操作的结果返回或输出到指定存储。
  • 在ADF中使用Azure Function活动调用该Function,实现与MySQL的数据交互;或者将Function暴露为API,通过ADF的HTTP链接服务调用。

方案3:创建自定义连接器

在ADF中创建自定义连接器,封装SSH隧道和MySQL连接的逻辑:

  • 基于Azure Logic Apps/ADF的自定义连接器框架,将Function中实现的SSH+MySQL连接逻辑转为连接器的操作。
  • 配置连接器的认证方式(比如密钥存储在Azure Key Vault),之后在ADF中直接使用该自定义连接器连接目标MySQL数据库。

内容的提问来源于stack exchange,提问作者bilpor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 16:25:18