Operations 6 min read

Eliminate Excessive MySQL TIME_WAIT Connections with Simple Kernel Tweaks

This guide shows how to diagnose a flood of MySQL TIME_WAIT sockets, adjust Linux kernel parameters (or Windows registry) to shorten the wait period, verify the improvement, and understand why improper connection handling can cause the issue.

ITPUB
ITPUB
ITPUB
Eliminate Excessive MySQL TIME_WAIT Connections with Simple Kernel Tweaks

Problem Identification

When logging into the server, running netstat -an|grep mysql reveals many connections stuck in the TIME_WAIT state, which can consume resources and degrade MySQL performance.

Linux Solution: Adjust Kernel Parameters

Edit /etc/sysctl.conf and add the following lines:

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30

Then apply the changes with /sbin/sysctl -p. The parameters mean:

net.ipv4.tcp_syncookies = 1 : Enables SYN cookies to protect against SYN‑queue overflow.

net.ipv4.tcp_tw_reuse = 1 : Allows reuse of TIME_WAIT sockets for new connections.

net.ipv4.tcp_tw_recycle = 1 : Enables faster recycling of TIME_WAIT sockets.

net.ipv4.tcp_fin_timeout = 30 : Reduces the default FIN timeout to 30 seconds.

After the change, verify the reduction with netstat -ae|grep "TIME_WAIT" | wc -l. The number of TIME_WAIT sockets should drop dramatically, and MySQL’s CPU usage returns to normal.

Common Application‑Level Cause

Often a large number of TIME_WAIT sockets appear because application code does not properly close MySQL connections (missing mysql.close()), leaving sockets in the wait state.

Windows Alternative

On Windows servers, modify the registry key:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]
"TcpTimedWaitDelay"=dword:0000001e

This sets the maximum TIME_WAIT duration to 30 seconds (default is 240 seconds).

Understanding TIME_WAIT

TIME_WAIT lasts for twice the Maximum Segment Lifetime (MSL), typically 240 seconds. Its purpose is to ensure that delayed packets from a closed connection do not interfere with new connections on the same port. Reducing this interval speeds up port reuse but must be done carefully.

When many connections enter TIME_WAIT simultaneously, each connection’s TCP Control Block remains allocated, potentially exhausting memory.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceTCPLinuxmysqlTIME-WAITsysctlKernel Tuning
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.