"Unable to connect to any of the specified MySQL hosts" 在 C# 中通过 SSH.NET 隧道连接到 MySQL 数据库时
"Unable to connect to any of the specified MySQL hosts" when connecting to MySQL database via SSH.NET tunnel in C#
我正在尝试使用 C# 和 SSH.Net.
连接到 Ubuntu 上的 MySQL 数据库 运行
我们的 MySQL 数据库只允许来自网络服务器的连接,按照我从下面的代码中看到的内容,客户端对象连接到网络服务器,然后我打开一个端口映射到数据库上的 3306服务器。最后,MySQLConnection
对象打开到 MySQL 数据库的连接。
但是我在 sql.Open()
语句中出现异常:
Unable to connect to any of the specified MySQL hosts
我查看了同一主题的其他主题并尝试了他们的建议,但 none 似乎对我有帮助。
Unable to connect to any of the specified MySQL hosts
Unable to connect to any of the specified MySQL hosts
unable to connect to any of the specified MySQL hosts
还有更多。
这是我正在使用的一些测试代码 -
var ci =
new ConnectionInfo(
"server", "userid", new PasswordAuthenticationMethod("userid", "userpwd"));
using (var client = new SshClient(ci))
{
client.Connect();
if (client.IsConnected) //This part works fine - I can connect to my Webserver.
{
//not sure if this is correct in our context.
var local = new ForwardedPortDynamic("127.0.0.1",3306);
client.AddForwardedPort(local);
try
{
local.Start();
}
catch (SocketException se)
{
}
string connStr =
"Server = dbserver;Port = 3306;Database = dbname;Uid = dbuserid;Pwd = dbpwd;";
MySqlConnection sql = new MySqlConnection(connStr);
try
{
sql.Open();
local.Stop();
}
catch (MySqlException se)
{
}
}
}
我在代码中输入的所有凭据都已从 MySQLWorkbench 与 MySQL 数据库的有效连接中删除。
我只在 Windows 环境中工作,所以如果你想让我检查 Ubuntu 服务器上的某些东西,请告诉我,如果可能的话,如何检查它。
您正在通过 SSH 隧道连接。
所以你的主要问题是连接字符串中的Host
必须是localhost
(SSH隧道的本地端),而不是dbserver
。或者更好,使用 ForwardedPort.boundHost
(和 ForwardedPort.boundPort
)。
第二个问题是不能用ForwardedPortDynamic
,而是ForwardedPortLocal
.
var forwardedPort = new ForwardedPortLocal("127.0.0.1", "127.0.0.1", 3306);
client.AddForwardedPort(forwardedPort);
forwardedPort.Start();
string connStr =
string.Format(
"Server = {0};Port = {1};Database = dbname;Uid = dbuserid;Pwd = dbpwd;",
forwardedPort.BoundHost, forwardedPort.BoundPort);
另见 Connection to MySQL from .NET using SSH.NET Library。
如果您的数据库不在 运行 网络服务器上,而是在 dbhost
上,您必须在 ForwardedPortLocal
构造函数中使用它:
var forwardedPort = new ForwardedPortLocal("127.0.0.1", "dbhost", 3306);
我正在尝试使用 C# 和 SSH.Net.
连接到 Ubuntu 上的 MySQL 数据库 运行我们的 MySQL 数据库只允许来自网络服务器的连接,按照我从下面的代码中看到的内容,客户端对象连接到网络服务器,然后我打开一个端口映射到数据库上的 3306服务器。最后,MySQLConnection
对象打开到 MySQL 数据库的连接。
但是我在 sql.Open()
语句中出现异常:
Unable to connect to any of the specified MySQL hosts
我查看了同一主题的其他主题并尝试了他们的建议,但 none 似乎对我有帮助。
Unable to connect to any of the specified MySQL hosts
Unable to connect to any of the specified MySQL hosts
unable to connect to any of the specified MySQL hosts
还有更多。
这是我正在使用的一些测试代码 -
var ci =
new ConnectionInfo(
"server", "userid", new PasswordAuthenticationMethod("userid", "userpwd"));
using (var client = new SshClient(ci))
{
client.Connect();
if (client.IsConnected) //This part works fine - I can connect to my Webserver.
{
//not sure if this is correct in our context.
var local = new ForwardedPortDynamic("127.0.0.1",3306);
client.AddForwardedPort(local);
try
{
local.Start();
}
catch (SocketException se)
{
}
string connStr =
"Server = dbserver;Port = 3306;Database = dbname;Uid = dbuserid;Pwd = dbpwd;";
MySqlConnection sql = new MySqlConnection(connStr);
try
{
sql.Open();
local.Stop();
}
catch (MySqlException se)
{
}
}
}
我在代码中输入的所有凭据都已从 MySQLWorkbench 与 MySQL 数据库的有效连接中删除。
我只在 Windows 环境中工作,所以如果你想让我检查 Ubuntu 服务器上的某些东西,请告诉我,如果可能的话,如何检查它。
您正在通过 SSH 隧道连接。
所以你的主要问题是连接字符串中的Host
必须是localhost
(SSH隧道的本地端),而不是dbserver
。或者更好,使用 ForwardedPort.boundHost
(和 ForwardedPort.boundPort
)。
第二个问题是不能用ForwardedPortDynamic
,而是ForwardedPortLocal
.
var forwardedPort = new ForwardedPortLocal("127.0.0.1", "127.0.0.1", 3306);
client.AddForwardedPort(forwardedPort);
forwardedPort.Start();
string connStr =
string.Format(
"Server = {0};Port = {1};Database = dbname;Uid = dbuserid;Pwd = dbpwd;",
forwardedPort.BoundHost, forwardedPort.BoundPort);
另见 Connection to MySQL from .NET using SSH.NET Library。
如果您的数据库不在 运行 网络服务器上,而是在 dbhost
上,您必须在 ForwardedPortLocal
构造函数中使用它:
var forwardedPort = new ForwardedPortLocal("127.0.0.1", "dbhost", 3306);