gpt4 book ai didi

c# - 在远程桌面上通过 SSH 连接 MySql 数据库时出现 1042 错误

转载 作者:行者123 更新时间:2023-11-29 22:30:38 26 4
gpt4 key购买 nike

我无法在远程桌面上通过 SSH 连接到 MySql 数据库。我使用 C# 和 SshNet 库。实际上,在流行工具(例如 SQL Manager Lite)的帮助下连接到数据库没有问题,但是当我使用我的工具使用相同的设置执行相同的操作时 - 总是得到

Mysql 1042 Error (Unable to connect to any of the specified MySQL hosts)

另外,my.cnf中的部分被编辑为 绑定(bind)地址 = 0.0.0.0# 跳过网络。 (是的,我在绑定(bind)地址上尝试使用本地主机和服务器 IP - 同样的问题)

并且我还授予了该用户所有权限。仔细检查了一下。

请看一下代码,也许我漏掉了一些东西。

抱歉,我正在使用表单,我只需要 GUI:{

提前非常感谢大家。

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("serverip", "sshlogin", "sshpass");
connectionInfo.Timeout = TimeSpan.FromSeconds(30);
using (var client = new SshClient(connectionInfo))
{
try
{
client.Connect();
if (client.IsConnected)
{
label1.Text = "ssh ok";
}
else
{
label1.Text = "ssh shit";
}

var port = new ForwardedPortRemote(3306, "serverip", 3306);
client.AddForwardedPort(port);
port.Start();
if (port.IsStarted)
{
label5.Text = "port open";
}
else
{
label5.Text = "port shit";
}
var conn_info = "Server=serverip;Port=3306;Database=database;Uid=remoteuser;Pwd=password;";
bool isConn = false;
MySqlConnection conn = null;
try
{
conn = new MySqlConnection(conn_info);
conn.Open();
isConn = true;
}
catch (ArgumentException a_ex)
{

label3.Text = "Check the Connection String.";
label4.Text = a_ex.Message;
label5.Text = a_ex.ToString();

}
catch (MySqlException ex)
{
string sqlErrorMessage = "Message: " + ex.Message + "\n" +
"Source: " + ex.Source + "\n" +
"Number: " + ex.Number;
label3.Text = sqlErrorMessage;

isConn = false;
switch (ex.Number)
{
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
break;
case 0: // Access denied
break;
default:
break;
}
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
client.Disconnect();
label2.Text = isConn.ToString();
}
catch (SocketException exd)
{
label5.Text = exd.Message;
}

}

最佳答案

在您的代码中,您使用 var port = new ForwardedPortRemote(3306, "serverip", 3306); 它将连接转发到远程计算机上的端口 3306到本地计算机上的 3306。这就是你想做的吗?看起来您想要 ForwardPortLocal 或任何将调用的函数来将连接转发到另一个方向。

关于c# - 在远程桌面上通过 SSH 连接 MySql 数据库时出现 1042 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29831746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com