Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#ssh.net隧道mysql客户端ioexception_C#_Mysql_Ssh_Ioexception - Fatal编程技术网

c#ssh.net隧道mysql客户端ioexception

c#ssh.net隧道mysql客户端ioexception,c#,mysql,ssh,ioexception,C#,Mysql,Ssh,Ioexception,我尝试通过ssh隧道转发端口创建远程MySql连接。 sshClient连接正常。 ForwardedPort启动正常。 当我尝试连接MysqlConnection时,它抛出System.Net.IO.IOException,并显示消息“由于意外的数据包格式,握手失败” 端口100%确定正常,因为如果我用我的应用程序创建此端口,其他本机应用程序(如HeidiSQL)可以连接 PrivateKeyFile file = new PrivateKeyFile(rsaFile);

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

端口100%确定正常,因为如果我用我的应用程序创建此端口,其他本机应用程序(如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从None(在我第一次尝试时未设置)更改为Required或Prefered等。。。 尝试将其设置为MySqlSslMode。无,效果很好:)终于

使用SSH.Net和MySqlClient

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

  • 启动转发端口本地(127.0.0.113306127.0.0.113306)

  • 不使用任何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(); ....
    
    如果你需要帮助,请告诉我