在远程桌面上通过 SSH 连接到 MySql 数据库时出现 1042 错误
Getting 1042 error while connecting to MySql database via SSH on remote desktop
我无法在远程桌面上通过 SSH 连接到 MySql 数据库。我使用 C# 和 SshNet 库。实际上,借助 SQL Manager Lite 等流行工具,我可以毫无问题地连接到数据库,但是当我使用我的工具以相同的设置做同样的事情时 - 总是得到
Mysql 1042 Error (Unable to connect to any of the specified MySQL hosts)
此外,my.cnf中的部分被编辑为
bind-address = 0.0.0.0
和
# skip-networking
。 (是的,我尝试在绑定地址上使用本地主机和服务器 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 转发到 local[= 上的 3306 16=] 机器。这是你想要做的吗?看起来你想要 ForwardPortLocal
或调用任何函数来将连接转发到另一个方向。
我遇到了同样的问题。 MySql 数据库在 ubuntu 上 运行 并且需要 SSH 连接才能连接。我从 here 下载了 sshnet .NET 4.0 库。然后,将库导入到项目中,进行了以下步骤:
正在建立ssh连接
var client = new SshClient("ip address of linux server", "username", "password"); client.Connect();
检查客户端是否已连接。如果是,则端口转发是从 Linux 服务器到本地服务器完成的。这是一种神奇的线条。
var portForwardedL = new ForwardedPortLocal(3306, "127.0.0.3", 3306);
client.AddForwardedPort(portForwardedL);
portForwardedL.Start();
现在只需使用转发的服务器 IP 和端口
连接 MySql 数据库
using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.3;PORT=3306;UID=username;PWD=password;DATABASE=database_name"))
{
using (MySqlCommand com = new MySqlCommand("SELECT * FROM cities", con))
{
DataSet ds = new DataSet();
MySqlDataAdapter da = new MySqlDataAdapter(com);
da.Fill(ds);
foreach (DataRow drow in ds.Tables[0].Rows)
{
Console.WriteLine("From MySql: " + drow[1].ToString());
}
}
}
就是这样!它像魔术一样工作!请务必关闭ssh连接
我无法在远程桌面上通过 SSH 连接到 MySql 数据库。我使用 C# 和 SshNet 库。实际上,借助 SQL Manager Lite 等流行工具,我可以毫无问题地连接到数据库,但是当我使用我的工具以相同的设置做同样的事情时 - 总是得到
Mysql 1042 Error (Unable to connect to any of the specified MySQL hosts)
此外,my.cnf中的部分被编辑为
bind-address = 0.0.0.0
和
# skip-networking
。 (是的,我尝试在绑定地址上使用本地主机和服务器 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 转发到 local[= 上的 3306 16=] 机器。这是你想要做的吗?看起来你想要 ForwardPortLocal
或调用任何函数来将连接转发到另一个方向。
我遇到了同样的问题。 MySql 数据库在 ubuntu 上 运行 并且需要 SSH 连接才能连接。我从 here 下载了 sshnet .NET 4.0 库。然后,将库导入到项目中,进行了以下步骤:
正在建立ssh连接
var client = new SshClient("ip address of linux server", "username", "password"); client.Connect();
检查客户端是否已连接。如果是,则端口转发是从 Linux 服务器到本地服务器完成的。这是一种神奇的线条。
var portForwardedL = new ForwardedPortLocal(3306, "127.0.0.3", 3306); client.AddForwardedPort(portForwardedL); portForwardedL.Start();
现在只需使用转发的服务器 IP 和端口
连接 MySql 数据库using (MySqlConnection con = new MySqlConnection("SERVER=127.0.0.3;PORT=3306;UID=username;PWD=password;DATABASE=database_name")) { using (MySqlCommand com = new MySqlCommand("SELECT * FROM cities", con)) { DataSet ds = new DataSet(); MySqlDataAdapter da = new MySqlDataAdapter(com); da.Fill(ds); foreach (DataRow drow in ds.Tables[0].Rows) { Console.WriteLine("From MySql: " + drow[1].ToString()); } } }
就是这样!它像魔术一样工作!请务必关闭ssh连接