2016-12-14 71 views
1

我試圖通過SSH隧道forwardedport創建遠程MySql連接。 sshClient連接正常。 ForwardedPort開始OK。 當我嘗試使用的MySqlConnection連接它拋出與消息System.Net.IO.IOException「失敗握手由於意外的數據包格式」c#ssh.net隧道mysql客戶端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; 

回答

1

我找到了我的問題的解決方案。由於使用了轉發端口,所以連接字符串中的默認端口3306更改爲33306,因此(並告訴我,如果我錯了),MySQLClient將SslMode從None(在我第一次嘗試時未設置)中更改爲Required或優先等... 試圖將其設置爲MySqlSslMode.None,它的工作就像魅力:)終於!

使用SSH.Net和了MySqlClient

  1. 連接到與SSHClient(ConnectionInfo)服務器

  2. 開始ForwardedPortLocal(127.0.0.1,33306,127.0.0.1,3306)

  3. 連接MySql沒有任何SSLMode(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(); .... 

如果您需要任何幫助,請告訴我。