【问题标题】:C# MySQL SSL Connection Error when trying to use conn.Open()尝试使用 conn.Open() 时出现 C# MySQL SSL 连接错误
【发布时间】:2021-08-18 21:21:50
【问题描述】:

在过去的 3 天里,我一直在努力解决 C# 中的 MySQL 连接器问题。 基本上,我按照 MySQLConnector 教程打开连接以发送数据,但是当我使用 MySQLConnection.Open() 方法时,我的代码会引发 SSL 连接错误。 代码如下:

public async void SQLInsert()
        {
            string connStr = "Server=echstreme.de;User=c1Look;Database=c1Look;Port=3306;Password=redacted;";
            MySqlConnection conn = new MySqlConnection(connStr);

            try
            {
                await SendEmbedMessage("MySQL", "Attempting to connect to database");
                conn.Open();
                await SendEmbedMessage("MySQL", "Connection to Database Successful");
                MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);

                cmd.ExecuteNonQuery();


                conn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

我尝试过同时使用域“echstreme.de”和 IP 地址,但是都抛出了类似的错误。

编辑:已修复 Fix => "Server=echstreme.de;Port=3306;Database=c1Look;Uid=c1Look;Pwd=gkmcpLNNF6Y_5;SSL Mode =None";

这是任何需要它的人的完整错误消息

MySql.Data.MySqlClient.MySqlException (0x80004005): SSL Connection error.
 ---> System.AggregateException: One or more errors occurred. (The handshake failed due to an unexpected packet format.)
 ---> System.IO.IOException: The handshake failed due to an unexpected packet format.
   at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslStream.ThrowIfExceptional()
   at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__64_2(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at MySql.Data.Common.Ssl.StartSSL(Stream& baseStream, Encoding encoding, String connectionString)
   at MySql.Data.Common.Ssl.StartSSL(Stream& baseStream, Encoding encoding, String connectionString)
   at MySql.Data.MySqlClient.NativeDriver.Open()
   at MySql.Data.MySqlClient.Driver.Open()
   at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
   at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
   at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
   at MySql.Data.MySqlClient.MySqlPool.GetConnection()
   at MySql.Data.MySqlClient.MySqlConnection.Open()
   at Bot.Admins.AdminCommands.SQLInsert() in C:\Users\Owner\source\repos\Bot\Bot\Admins\AdminCommands.cs:line 71

【问题讨论】:

  • 问题很好,但缺少版本信息。

标签: c# mysql


【解决方案1】:

如果您的主机不支持 SSL,您可以使用 SSL Mode 选项、NoneRequired

连接字符串必须是这样的

"Server=echstreme.de;User=c1Look;Database=c1Look;Port=3306;Password=redacted;SSL Mode=None"

【讨论】:

  • 嘿,很遗憾,这引发了与我上面评论的相同的错误。
  • 精确解决方案!我在这个问题上花了几个星期。您的解决方案对我有用
【解决方案2】:

当你继续mysql connection strings

你会看到像这样的带有 TCP 端口的连接字符串,试试这个连接字符串构造:

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

您还确定您使用的是正确的端口吗?

【讨论】:

  • 嘿,尝试其他构造没有奏效,并给我留下了与以前相同的错误。但是,使用新字符串并将端口更改为 8080(如 db 的标题栏所述),错误更改为 ``` MySql.Data.MySqlClient.MySqlException :从流中读取失败。无法从传输连接中读取数据:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应```
  • 教程中的人是否使用 echstreme.de 作为服务器?你能提供你学习的教程的链接吗?
  • 嘿,不,echstreme.de 是由我的托管服务提供商提供的。我正在使用官方的 MySQL 连接器教程dev.mysql.com/doc/connector-net/en/…
  • 嘿,它似乎已经修复了!感谢您的帮助 修复:“Server=echstreme.de;Port=3306;Database=c1Look;Uid=c1Look;Pwd=redacted;SSL Mode =None”;
  • @Luke 如果 SSL 模式解决了您的问题,您可以将我的答案标记为解决方案吗?
【解决方案3】:

您需要将驱动器连接器更改为正确的数据库版本。如果您使用 mysql 8,则需要使用连接器 v8,反之亦然

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2022-01-22
    • 2019-06-05
    • 1970-01-01
    相关资源
    最近更新 更多