C#ssh.net隧道mysql客户端ioexception [英] c# ssh.net tunnel mysql client ioexception

查看:68
本文介绍了C#ssh.net隧道mysql客户端ioexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过ssh隧道forwardedport创建远程MySql连接. sshClient连接确定. ForwardedPort开始正常. 当我尝试与MysqlConnection连接时,它将引发System.Net.IO.IOException,并显示消息由于意外的数据包格式,握手失败"

I've tried to create remote MySql connection via ssh tunnel forwardedport. The sshClient connection OK. ForwardedPort starts OK. When I try to connect with MysqlConnection it throws System.Net.IO.IOException with the message "The handshake failed due to an unexpected packet format"

该端口可以100%确定,因为如果我使用我的应用程序创建此端口,则其他本地应用程序(例如HeidiSQL)可以连接.

The port is OK 100% sure because other native app(eg HeidiSQL) can connect if i create this port with my app.

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或偏好等... 试图将其设置为MySqlSslMode.None,它终于像魅力一样工作:)!

I found a solution for my problem. Because of using the Forwarded Port, the default port 3306 changed to 33306 in connection string, so (and tell me if i'm wrong) the MySQLClient changed the SslMode from None (in my first attempts it was not set) to any of Required or Preffered etc... Tried to set it to MySqlSslMode.None and it worked like charm :) finally!

使用SSH.Net和MySqlClient

Using SSH.Net and MySqlClient

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

  1. Connect to server with SSHClient(ConnectionInfo)

启动ForwardedPortLocal(127.0.0.1、33306、127.0.0.1、3306)

Start ForwardedPortLocal(127.0.0.1, 33306, 127.0.0.1, 3306)

代码在这里!

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(); ....

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

If you need any help let me know.

这篇关于C#ssh.net隧道mysql客户端ioexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆