Ubuntu thesql too many connections

MySQLUbuntu

I am using django with mysql on ubuntu 12.04.

It's strange that when I tried to do very simple command in mysql, it will suggest error "ERROR 1040 (HY000): Too many connections"

All I want to do is create about 20 tables using a django mysql framework, south. I am the only user connecting mysql, how come there are too many connections?

I did not change the configuration file in mysql, so the max connections should be default 150, I believe.

Could anyone help me with that? Thanks a million

update 1:
After I restart mysql server and run show processlist, I found 26 connections even though I am not doing anything. Sometimes the number would drop to 10, and come up again at 25 or something. Is that normal?

update 2:

Okay, I found what's the problem. Thanks for your help.

Best Answer

MySQL Connections are sometimes held in abeyance by the OS.

Many times a week, someone's application has lingering MySQL Connections that were supposedly closed by Apache or Python or PHP. Sure, it is closed in the eyes of the Application, but TIME_WAITs sometimes take precedence. There is a hack I recent discovered in ServerFault. I now this hack:

Here is a nice question about TIME_WAIT : How to forcibly close a socket in TIME_WAIT?. I use this answer: https://serverfault.com/a/329848/69271

In essence, I would run this on the Ubuntu machine:

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