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.

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:

http://i.stack.imgur.com/wo1tR.png

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

1 Answer 1

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

Here are two suggestions:

SUGGESTION #1

Try to make all mysql TIME_WAIT connections disconnect faster

Run these commands on all DB and Web Servers

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

SUGGESTION #2

Keep track of all the mysql connections per web server

Create a script called /root/ShowMySQLConnections.sh 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`
do
        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}
done
echo ; echo
netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n | sed 's/d)/d/'

EPILOGUE

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

share|improve this answer

Your Answer

 
discard

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.