Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My server goes down repeatedly. using htop I found that mysql and apache use almost all of RAM memory. When I restart apache everything is ok, but after a while httpd and mysql take all memory as you see in the following sreenshot:

I'm sure that while the screenshot has been taken, there is maximum of 2-3 users online. and therefore httpd(or sometims mysqld) doesn't free memory after ending a connection.

What should I do?

share|improve this question

You may need to check netstat for mysql connections marked as TIME_WAIT

Here are two suggestions:


Try to make all mysql TIME_WAIT connections disconnect faster

Run these commands on all DB and Web Servers

echo ${SEC_TO_TIMEWAIT} > /proc/sys/net/ipv4/tcp_tw_recycle
echo ${SEC_TO_TIMEWAIT} > /proc/sys/net/ipv4/tcp_tw_reuse


Keep track of all the mysql connections per web server

Create a script called /root/ with these lines

netstat | grep mysql > /root/mysql_netstat.txt
cat /root/mysql_netstat.txt | awk '{print $5}' | sed 's/:/ /' |  awk '{print $1}' | sort -u | grep -v local > /root/mysql_netstat_iplist.txt
for IP in `cat /root/mysql_netstat_iplist.txt`
        ESCOUNT=`cat /root/mysql_netstat.txt | grep ESTABLISHED | awk '{print $5}' | grep -c "${IP}"`
        TWCOUNT=`cat /root/mysql_netstat.txt | grep TIME_WAIT   | awk '{print $5}' | grep -c "${IP}"`
        IPPAD=`echo "${IP}........................................" | cut -b -40`
        (( ESCOUNT += 1000000 ))
        (( TWCOUNT += 1000000 ))
        ES=`echo ${ESCOUNT} | cut -b 3-`
        TW=`echo ${TWCOUNT} | cut -b 3-`
        echo ${IPPAD} : ESTABLISHED:${ES} TIME_WAIT:${TW}
echo ; echo
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n | sed 's/d)/d/'


After killing TIME_WAIT connections, run watch /root/ and see if the TIME_WAITs disappear.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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