Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

When I use netstat command it shows..

tcp 0 0 localhost:18056 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16683 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16701 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16888 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16832 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17725 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17682 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17414 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17606 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17737 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16632 localhost:mysql TIME_WAIT
tcp 0 0 localhost:16825 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17807 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17715 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17304 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17217 localhost:mysql TIME_WAIT
tcp 0 0 localhost:18098 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17624 localhost:mysql TIME_WAIT
tcp 0 0 localhost:17734 localhost:mysql TIME_WAIT

Time_wait connection is around 2000.

To avoid this I added net.ipv4.tcp_fin_timeout=30 to /etc/sysctl.conf

But still I have some problem,,how to avoid it?

share|improve this question

marked as duplicate by Michael Hampton, David Schwartz, MadHatter, Dave M, mgorven Feb 15 '13 at 0:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
You forgot to tell us what problem you are having! Are you running out of local ports? –  David Schwartz Feb 14 '13 at 7:14
    
I want to reduce these TIME_WAIT connections .... –  Himanshu Matta Feb 14 '13 at 7:18
1  
That's not a problem. –  Michael Hampton Feb 14 '13 at 7:35

2 Answers 2

up vote 1 down vote accepted

TIME_WAIT exists for a reason and the reason is that TCP packets can be delayed and arrive out of order. Messing with it will cause extra broken connections when they ought to have succeeded. There's an excellent explanation of all of this here.

Your problem is that you are not reusing your MySQL connections within your app but instead you are creating a new connection every time you want to run an SQL query. This involves not only setting up a TCP connection, but then also passing authentication credentials across it. And this is happening for every query (or at least every front-end web request) and it's wasteful and time consuming.

If you don't know how to enable persistent MySQL connection pooling in whatever language you are using, StackOverflow would be a good place to ask.

share|improve this answer
    
time_wait is a problem or not ??? you mean I am opening a connection but not closing it?? –  Himanshu Matta Feb 14 '13 at 7:56
1  
TIME_WAIT means a connection is closed (FIN packets have been sent) but we're holding the ports in reserve in case some more packets come through due to delays. It also means you can't reuse that combination until it times out. On Linux, only the ports matter and you can't reuse them even on a different IP address. You can have about 30,000 before you run into problems and you can increase the ephemeral port range to delay dealing with that. –  Ladadadada Feb 14 '13 at 8:07
    
ok..I got you point. Last question- as you said 'time-wait means connection is closed but we are holding the ports..' Can we reduce this holding time ??? –  Himanshu Matta Feb 14 '13 at 8:12
    
You can, but it is not needed. If you want a very simple overview of what TIME_WAIT is, see the diagram in this post: serverfault.com/questions/450055/… –  Hennes Feb 14 '13 at 8:18
1  
You can avoid the TIME_WAIT state altogether if you avoid the "active close" action however if you are creating TCP connections to yourself (such as you are here with MySQL on localhost) then one end of the connection or the other will have to initiate closing the connection. Keeping these connections open is by far the best solution. –  Ladadadada Feb 14 '13 at 9:01

I want to reduce these TIME_WAIT connections

The best solution is to find something else to occupy your time or something else you enjoy doing so that you no longer want to reduce these TIME_WAIT connections. If you still find that you have this irrational desire, therapy may be effective.

share|improve this answer
    
Being a therapist must be difficult... –  Nik Bougalis Feb 14 '13 at 8:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.