1

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"

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;

1 Answer 1

1

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!

Using SSH.Net and MySqlClient

  1. Connect to server with SSHClient(ConnectionInfo)

  2. Start ForwardedPortLocal(127.0.0.1, 33306, 127.0.0.1, 3306)

  3. Connect MySql WITHOUT ANY SSLMode (MySqlSSLMode.None)

Here is the code!

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.