gpt4 book ai didi

c# ssh.net tunnel mysql 客户端 ioexception

转载 作者:行者123 更新时间:2023-11-28 23:22:03 24 4
gpt4 key购买 nike

我尝试通过 ssh 隧道转发端口创建远程 MySql 连接。sshClient 连接正常。ForwardedPort 启动正常。当我尝试连接 MysqlConnection 时,它抛出 System.Net.IO.IOException 并显示消息“由于意外的数据包格式,握手失败”

端口 100% 确定,因为如果我用我的应用程序创建此端口,其他 native 应用程序(例如 HeidiSQL)可以连接。

PrivateKeyFile file = new PrivateKeyFile(rsaFile);
client = new SshClient(host, port, username, file);
forwardBoundHost = "127.0.0.1";
forwardBoundPort = 33306;
forwardHost = "127.0.0.1";
forwardPort = 3306;

port = new ForwardedPortLocal(forwardBoundHost, forwardBoundPort, forwardHost, forwardPort);

if(this.response != null){
port.RequestReceived += response;
}

client.Connect();

client.AddForwardedPort(port);

port.Exception += port_Exception;

port.Start();



if (port.IsStarted)
{
cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.Required,
Keepalive = 60,
ConnectionProtocol = MySqlConnectionProtocol.Tcp,
CharacterSet = "utf8"


};
cb.ConnectionProtocol = MySqlConnectionProtocol.Tcp;


MySqlConnection connection = new MySqlConnection(cb.GetConnectionString(true));



MySqlCommand cmd;
MySqlDataReader reader;
try
{
Console.WriteLine("Mysql client conn");
connection.Open();
}
cmd = connection.CreateCommand();
cmd.CommandText = queryString;
cmd.Prepare();
Array myp = new Array[param.Length];
int i = 0;
foreach (String oneParam in param)
{

myp.SetValue(new MySqlParameter(oneParam, MySqlDbType.String), i);
i++;
}
cmd.Parameters.AddRange(myp);
reader = cmd.ExecuteReader();


}
catch (Exception e)
{
//Logger(e.ToString());
throw (e);
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
connection.Close();
}
return reader;

最佳答案

我找到了解决问题的方法。由于使用了转发端口,连接字符串中的默认端口 3306 更改为 33306,因此(如果我错了请告诉我)MySQLClient 将 SslMode 从无(在我第一次尝试时未设置)更改为任何 Required或首选等...尝试将其设置为 MySqlSslMode.None 并且它像魅力一样工作 :) 最后!

使用 SSH.Net 和 MySqlClient

  1. 使用 SSHClient(ConnectionInfo) 连接到服务器

  2. 启动 ForwardedPortLocal(127.0.0.1, 33306, 127.0.0.1, 3306)

  3. 在没有任何 SSLMode 的情况下连接 MySql (MySqlSSLMode.None)

这是代码!

cb = new MySqlConnectionStringBuilder()
{
AllowBatch = true,
Server = this.host,
Port = this.port,
UserID = this.dbuser,
Password = this.dbpassword,
Database = this.database,
SslMode = MySqlSslMode.None,
Keepalive = 60,
ConnectionProtocol = MySqlConnectionProtocol.Tcp,
CharacterSet = "utf8"


};
cb.ConnectionProtocol = MySqlConnectionProtocol.Tcp;

MySqlConnection connection = new MySqlConnection(cb.GetConnectionString(true));

MySqlCommand cmd;
MySqlDataReader reader;
try
{
Console.WriteLine("Mysql client conn");
connection.Open();
cmd = connection.CreateCommand();
cmd.CommandText = queryString;
cmd.Prepare(); ....

如果您需要任何帮助,请告诉我。

关于c# ssh.net tunnel mysql 客户端 ioexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41152737/

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