SSH.NET 中的 MySQL 端口转发 - 尝试以访问权限禁止的方式访问套接字 [英] MySQL port forwading in SSH.NET - An attempt was made to access a socket in a way forbidden by its access permissions

查看:65
本文介绍了SSH.NET 中的 MySQL 端口转发 - 尝试以访问权限禁止的方式访问套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Installed Plesk on my VS. After this I can't setup SSH-tunnel to get data from other server-database. Using SSH.NET with below code. (This code works locally). Configured to allow communication for port 3306 in Plesk firewall. Any suggestions on how to solve this issue?

错误:

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

[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions]

代码:

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

推荐答案

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

The local port which you are trying to forward is most probably already used by another application (like a local MySQL database server).

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

Use another port. Or even better, let the system pick any free local port:

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 隧道 Postgres 数据库连接

这篇关于SSH.NET 中的 MySQL 端口转发 - 尝试以访问权限禁止的方式访问套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆