A database connection timeout occurs when your application fails to establish a connection to the database server within the allowed time limit. In 2026, these errors stem from network latency, resource exhaustion, or misconfigured connection pools. Fix them by adjusting timeout settings, optimizing queries, and implementing proper connection pooling.
Database connection timeouts can cripple your application’s availability and user experience. According to a 2025 Datadog reliability report, connection pool exhaustion accounts for 34% of database-related outages in cloud environments. When users encounter timeout errors, 88% will abandon the transaction within 3 seconds, directly impacting revenue and engagement. Understanding the root cause and implementing proper fixes ensures your WordPress site, API, or web application remains responsive under load.
Database connection timeouts happen when the client cannot reach the database server within the configured wait period. The most common causes include:
AWS RDS documentation notes that connection timeouts often spike during maintenance windows or when automated backups create I/O bottlenecks. Monitoring connection metrics before they become critical helps prevent cascading failures.
Start by collecting concrete data about where the timeout occurs. Enable detailed logging in your application and database layers:
# Enable MySQL slow query log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
# Check current connections
SHOW PROCESSLIST;
SHOW STATUS LIKE 'Threads_connected';
For PostgreSQL environments, query the pg_stat_activity view to identify blocking queries:
SELECT pid, usename, application_name, state, query_start
FROM pg_stat_activity
WHERE state != 'idle'
ORDER BY query_start;
Use network diagnostic tools to rule out connectivity problems. Test from the application server:
# Test basic connectivity
telnet db.example.com 3306
# Measure connection latency
time mysql -h db.example.com -u user -p -e "SELECT 1;"
# Check DNS resolution
dig db.example.com
Application-level instrumentation reveals whether timeouts happen during connection establishment or query execution. Tools like New Relic or AppDynamics can trace the exact stage where delays occur, helping you focus optimization efforts.
Different layers of your stack have timeout configurations that work together. Set them in this order of priority:
Application connection timeout: Time allowed to establish the initial TCP connection. For WordPress using MySQLi:
// wp-config.php
define('DB_CONNECT_TIMEOUT', 10);
// Or in mysqli
$mysqli = new mysqli();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
Query execution timeout: Maximum time for a single query to complete. In MySQL:
SET GLOBAL max_execution_time = 30000; -- milliseconds
SET SESSION max_execution_time = 30000;
Connection pool settings: For applications using connection pools (Java JDBC, Python SQLAlchemy, Node.js Sequelize):
# Python SQLAlchemy example
engine = create_engine(
'postgresql://user:pass@host/db',
pool_size=20,
max_overflow=10,
pool_timeout=30,
pool_recycle=3600
)
Google Cloud SQL recommends starting with a 10-second connection timeout and 30-second query timeout, then adjusting based on your 95th percentile latency metrics. Never set timeouts so high that they mask underlying performance problems.
Connection pooling reuses database connections instead of creating new ones for each request, dramatically reducing connection overhead. Without pooling, establishing a new MySQL connection can take 50-100ms; with pooling, requests reuse existing connections in under 1ms.
For PHP applications like WordPress running on Apache or Nginx with PHP-FPM:
; php-fpm pool config
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
Match your PHP-FPM max_children to your MySQL max_connections, leaving headroom for maintenance tasks:
# MySQL config (my.cnf)
[mysqld]
max_connections = 200
wait_timeout = 600
interactive_timeout = 600
For Node.js applications using the mysql2 package:
const pool = mysql.createPool({
host: 'localhost',
user: 'app_user',
database: 'app_db',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0
});
Monitor pool saturation metrics. When your application frequently exhausts available connections, either increase pool size or optimize queries to release connections faster. Cloudflare’s performance engineering team found that reducing average query time from 200ms to 50ms allowed a 4x reduction in required pool size.
Network configuration directly impacts connection reliability. Start with security group and firewall rules to ensure unobstructed database access:
AWS security groups: Allow inbound traffic on port 3306 (MySQL) or 5432 (PostgreSQL) from your application server security group, not from 0.0.0.0/0.
Reduce DNS lookup time: Use IP addresses in connection strings for critical paths, or ensure your DNS resolver is reliable and cached:
# /etc/hosts for static mapping
10.0.1.50 db-primary.internal
Enable TCP keepalive: Prevents idle connections from being dropped by firewalls or load balancers:
# Linux sysctl settings
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3
For WordPress sites using managed database services like Amazon RDS or Google Cloud SQL, place your web servers in the same region and availability zone as your database to minimize network latency. Cross-region database connections add 50-200ms of baseline latency that can cause timeout cascades under load.
Set connection timeout to 10 seconds for same-region connections and 20 seconds for cross-region. This allows time for normal network variance while preventing indefinite hangs. Monitor your actual connection times and set timeout to your 99th percentile plus 5 seconds of buffer.
Peak traffic exhausts your connection pool or database resources. Check if your max_connections setting can handle concurrent requests, and verify your connection pool size matches expected load. Connection pool starvation is the most common cause of traffic-correlated timeouts.
Yes. Misconfigured firewalls or security groups that block database ports create immediate connection failures that appear as timeouts. Use telnet or nc to verify port connectivity from your application server to your database server before adjusting application settings.
No. Slow queries indicate database performance problems that increasing timeouts will mask but not solve. Profile your queries with EXPLAIN, add appropriate indexes, and optimize N+1 query patterns. Higher timeouts should only compensate for network conditions, not poor query performance.
Use persistent connections by setting define(‘DB_PERSISTENT’, true) in wp-config.php, implement object caching with Redis or Memcached to reduce database queries, and use a CDN to serve static assets. For high-traffic sites, consider a database proxy like ProxySQL to manage connection pooling.