【问题标题】:MySQL port forwading in SSH.NET - An attempt was made to access a socket in a way forbidden by its access permissionsSSH.NET 中的 MySQL 端口转发 - 尝试以访问权限禁止的方式访问套接字
【发布时间】:2020-03-10 03:45:49
【问题描述】:

在我的 VS 上安装了 Plesk。在此之后,我无法设置 SSH 隧道以从其他服务器数据库获取数据。使用带有以下代码的 SSH.NET。 (此代码在本地工作)。配置为允许 Plesk 防火墙中的端口 3306 进行通信。有关如何解决此问题的任何建议?

错误:

[SocketException (0x271d): 试图访问一个套接字 访问权限禁止的方式]

代码:

DataTable dt = new DataTable();
string IP = "ssh.xxxxxxxx.xxx";
string Username = "xxxxxxxxxx";
string password = "xxxxxxxxxx";
var connInfo = new Renci.SshNet.PasswordConnectionInfo(IP, Username, password);
using (var sshClient = new Renci.SshNet.SshClient(connInfo))
{
    sshClient.Connect();

    if (sshClient.IsConnected)
    {
        Renci.SshNet.ForwardedPortLocal port =
            new Renci.SshNet.ForwardedPortLocal("127.0.0.1", 3306, "xxxxxxxx", 3306);
        sshClient.AddForwardedPort(port);
        port.Start();
        using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.1;PORT=3306;UID=xxxxxx;PASSWORD=xxxxxxx;DATABASE=xxxxxxx; convert zero datetime=True"))
        {
            string tmpsql = "Select fname,lname,username FROM tbluser Where id=@id";

            using (MySqlCommand cmd = new MySqlCommand(tmpsql, con))
            {
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                cmd.Parameters.AddWithValue("id", MySqlDbType.Int16).Value = UserID;
                da.Fill(dt);
            }
        }
    }
    sshClient.Disconnect();
}

【问题讨论】:

    标签: c# mysql ssh portforwarding ssh.net


    【解决方案1】:

    您尝试转发的本地端口很可能已被另一个应用程序(如本地 MySQL 数据库服务器)使用。

    使用另一个端口。或者更好的是,让系统选择任何空闲的本地端口:

    var port = new ForwardedPortLocal("127.0.0.1", "dbserver.example.com", 3306);
    client.AddForwardedPort(port);
    port.Start();
    
    var connectionString =
        $"SERVER={port.BoundHost};PORT={port.BoundPort};" + 
         "UID=xxxxxx;PASSWORD=xxxxxxx;DATABASE=xxxxxxx; convert zero datetime=True";
    using (MySqlConnection con = new MySqlConnection(connectionString))
    {
        // ...
    }
    

    相关:C# SSH tunnel Postgres database connection

    【讨论】:

      猜你喜欢
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多