MySqlException: Timeout expired in a .NET app
MySql.Data.MySqlClient.MySqlException: Timeout expiredShort answer
Timeout expired means a wait ended before it finished. Two waits matter in .NET: a command that runs longer than Default Command Timeout (30 seconds), and a connection request that waits for a pooled connection until Connection Timeout. Find which one fits, then fix the query or the connection use.
Fix it fast
1Find out which cause you have
Look at the server's running statements while the error happens. Long-running queries and a pile of sleeping connections tell the two causes apart.
SHOW FULL PROCESSLIST;2Match what you see, then jump to its fix
- 1
The failing statement shows in the process list as running for 30 seconds or moreThe query runs longer than the command timeoutFix - 2
The process list fills with many Sleep connections from the app and new opens waitConnections are leaked, so the pool runs outFix - 3
Timeouts appear only with parallel work or inside a reader loopOne connection is used by several operations at onceFix
- 1
What does the error look like?
MySql.Data.MySqlClient.MySqlException: Timeout expired
Cause 1 of 3
The query runs longer than the command timeout
1Check
Default Command Timeout is 30 seconds. After that the command is cancelled on the server. Zero turns the timeout off.
SHOW FULL PROCESSLIST;2Fix
Fix the query first: add the missing index or cut the amount of data. Only if the work is legitimately slow, raise Default Command Timeout in the connection string.
Server=db.example.com;Database=app;User ID=app;Password=...;Default Command Timeout=1203Verify
Run the slow operation again.
EXPLAIN SELECT ... ;Confirmed by mysqlconnector.net
Cause 2 of 3
Connections are leaked, so the pool runs out
1Check
Pooling is on by default and the maximum is 100 connections (Maximum Pool Size in MySqlConnector). A connection that is not disposed never goes back to the pool.
SHOW STATUS LIKE 'Threads_connected';2Fix
Create the connection in a using block for each operation instead of keeping a global connection that you open and close by hand. Dispose returns it to the pool.
using (var conn = new MySqlConnection(connectionString))
{
conn.Open();
// run commands here
}3Verify
The number of connections stays flat while the app is under load.
SHOW STATUS LIKE 'Threads_connected';Confirmed by dev.mysql.com
Cause 3 of 3
One connection is used by several operations at once
1Check
A MySqlConnection runs one operation at a time. Parallel calls on one connection, or a new command inside a reader.Read() loop, are not allowed.
grep -R -n "Task.WhenAll" --include=*.cs .2Fix
Open a separate connection per parallel operation, or await them in sequence. Read the values into memory and close the reader before the next command.
await using var conn = new MySqlConnection(connectionString);
await conn.OpenAsync();3Verify
Repeat the parallel workload.
dotnet testConfirmed by mysqlconnector.net
How do I stop it coming back?
- Dispose every connection and reader with using or await using.
- Watch Threads_connected on the server so a leak shows before the pool is empty.
- Fix slow queries with an index or less data before raising any timeout.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
EF Core logs 'An error occurred using the connection to database' with MySQL
Can't connect to MySQL server on 'host_name' (111)
This EF Core message is only a wrapper.
3 causes, each with a fixDatabases
DbContextOptionsBuilder has no UseMySql method
Cannot find the UseMysql method on DbContextOptions
UseMySql comes from a provider package, not from EF Core itself.
3 causes, each with a fixDatabases
There is already an open DataReader on the MySQL connection
The error appears inside a while (reader.Read()) loop
One MySQL connection runs one operation at a time.
3 causes, each with a fixDatabases
C# INSERT fails with 'Fatal error encountered during command execution'
MySql.Data.MySqlClient.MySqlException: Fatal error encountered during command execution
A fatal error during command execution means the connection broke while the INSERT was in flight.
3 causes, each with a fixDatabases
C# cannot reach MySQL: connection attempt failed, no response in time
System.Net.Sockets.SocketException (10060)
SocketError 10060 means the connection attempt timed out, or the host did not respond.
3 causes, each with a fixDatabases
MySqlException: unable to connect to any of the specified MySQL hosts
Can't connect to MySQL server on 'host' (10061)
The connector could not open a TCP connection to the host and port in your connection string.
4 causes, each with a fix