You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C# MySQL Connector/Net连接LAN内MySQL服务器超时问题求助

.NET 4.8 连接局域网MySQL 8.0.39 超时问题求助

环境信息

  • MySQL版本:8.0.39
  • 目标框架:.NET 4.8

测试连接代码

public bool TestConnection()
{
    try
    {
        MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder
        {
            Server = Program.settings.MySqlServer,
            Port = Convert.ToUInt32(Program.settings.MySqlPort),
            UserID = Program.settings.MySqlUser,
            Password = Program.settings.MySqlPass,
            Database = Program.settings.MySqlDBName,
            //SslMode = MySqlSslMode.Disabled,
            //ConnectionProtocol = MySqlConnectionProtocol.Tcp,
            //Pooling = true
        };

        using (MySqlConnection cn = new MySqlConnection(builder.ConnectionString))
        {
            cn.Open();
        }
        return true;
    }
    catch (Exception ex)
    {
        Program.ErrorLog(ex.Message);
        return false;
    }
}

错误信息

顶层异常

Message: Unable to connect to any of the specified MySQL hosts.

内部异常

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
The operation has timed out.

完整堆栈信息

MySql.Data.MySqlClient.MySqlException was caught
  HResult=-2147467259
  Message=Unable to connect to any of the specified MySQL hosts.
  Source=MySql.Data
  ErrorCode=-2147467259
  Code=0
  Number=1042
  StackTrace:
       at MySql.Data.MySqlClient.NativeDriver.<OpenAsync>d__38.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.Driver.<OpenAsync>d__76.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.Driver.<CreateAsync>d__74.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at MySql.Data.MySqlClient.Driver.<CreateAsync>d__74.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.MySqlPool.<CreateNewPooledConnectionAsync>d__31.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.MySqlPool.<GetPooledConnectionAsync>d__30.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.MySqlPool.<TryToGetDriverAsync>d__34.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.MySqlPool.<GetConnectionAsync>d__35.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
       at MySql.Data.MySqlClient.MySqlConnection.<OpenAsync>d__94.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MySql.Data.MySqlClient.MySqlConnection.Open()
       at ClickCardService.MySQL.Connection.TestConnection() in c:\Team_Click\ClickCardService\ClickCardService\MySQL\Connection.cs:line 103
  InnerException: MySql.Data.MySqlClient.MySqlException
       HResult=-2147467259
       Message=Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
       Source=MySql.Data
       ErrorCode=-2147467259
       Code=0
       Number=0
       StackTrace:
            at MySql.Data.Common.StreamCreator.<GetTcpStreamAsync>d__8.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at MySql.Data.Common.StreamCreator.<GetStreamAsync>d__7.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
            at MySql.Data.MySqlClient.NativeDriver.<OpenAsync>d__38.MoveNext()
       InnerException: System.TimeoutException
            HResult=-2146233083
            Message=The operation has timed out.
            InnerException: 

连接字符串输出

server=192.168.0.21;port=3306;user id=r357;password=Pass1234;database=tidra2

已排查项

  • TCP端口已开放,无防火墙限制
  • Telnet连接服务器IP和端口成功
  • Test-NetConnection 192.168.0.21 -Port 3306测试成功,结果:

ComputerName : 192.168.0.21
RemoteAddress : 192.168.0.21
RemotePort : 3306
InterfaceAlias : Ethernet
SourceAddress : 192.168.0.9
TcpTestSucceeded : True

  • 本地通过phpMyAdmin可正常连接,配置:
/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'r357';
$cfg['Servers'][$i]['password'] = 'Pass1234';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';

/* Bind to the localhost ipv4 address and tcp */
$cfg['Servers'][$i]['host'] = '192.168.0.21';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
  • 测试过多个版本MySQL Connector(8.4.0、8.3.0、8.0.33.1),问题依旧

最新日志信息

设置log-error-verbosity = 3后,MySQL错误日志记录:

2024-08-28T13:28:58.193475Z 10 [Note] [MY-010914] [Server] Got timeout reading communication packets

执行SHOW FULL PROCESSLIST结果:

Id, User, Host, db, Command, Time, State, Info
'12', 'unauthenticated user', 'PetarSPC:12325', NULL, 'Connect', '2', 'Receiving from client', NULL

MySQL通用日志中可看到phpMyAdmin的连接记录:

Timestamp, Thread, Command Type, Detail
2024-08-27 15:11:26.279050, 27, Connect, r357@PetarSPC on using TCP/IP

寻求解决思路。


内容的提问来源于stack exchange,提问作者Petar Stoyanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 03:24:51