Question

I have just shifted my website to Linode and I am currently on its 1 GB RAM Plan. By default, number of mysql users was set to 100. This lead to Too many connections error.

I edited the mysql settings again with the help of this . However, now the database is crashing almost every day !

Here are my current mysql settings are :-

key_buffer              = 100M
max_allowed_packet      = 1M
thread_stack            = 128K
thread_cache_size       = 8
myisam-recover         = BACKUP
max_connections        = 150
table_cache       ​ ​     = 1024​
query_cache_limit       = 300M
query_cache_size        = 300M ​
max_allowed_packet      = 16M
key_buffer              = 100M

Can anyone suggest me some changes or the reason why my databases are crashing so frequently ? I can also add more details about my server on demand.

EDIT

I have added mysql_close($con) everywhere, but that has further increased problems for my website somehow.Connections is increasing exponentially in the result shown below :-

mysql> show status like '%onn%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| Aborted_connects         | 48    |
| Connections              | 1634  |
| Max_used_connections     | 8     |
| Ssl_client_connects      | 0     |
| Ssl_connect_renegotiates | 0     |
| Ssl_finished_connects    | 0     |
| Threads_connected        | 4     |
+--------------------------+-------+
7 rows in set (0.00 sec)

Thanks !

Was it helpful?

Solution

If you are not closing connections then obviously this would cause database to eventually crash. A restart will flush all connections that is why you server is probably restarting.

So fix your application and close connections. In the meanwhile you can have this script to kill the opened connections staying dead in your process-list. The script will kill everything that is stalled in the processlist for 500 sec. You may change the time as per need basis.

SECONDS_TOO_LONG=500
QUERIES_RUNNING_TOO_LONG=`/mysql/bin/mysql -u user -p'password' -ANe"SELECT COUNT(1) FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"`
if [ ${QUERIES_RUNNING_TOO_LONG} -gt 0 ]
then
    KILLPROC_SQLSTMT="SELECT GROUP_CONCAT(CONCAT('KILL QUERY ',id,';') SEPARATOR ' ') KillQuery FROM information_schema.processlist WHERE user<>'system user' AND time >= ${SECONDS_TOO_LONG}"
    /mysql/bin/mysql -u user -p'pass' -ANe"${KILLPROC_SQLSTMT}" | /mysql/bin/mysql -u user -p'pass'
fi;

OTHER TIPS

Actually, the main source of increasing exponential connections was my table itself. I was using MYISAM db. It actually locks the entire table before making any update. So, as a result if too many users come to my website, it was creating trouble.

Solution :-

I changed my database to InnoDB. It locks only individual rows which are required. This has limited to no of connections at a time to as low as single digit. Here is another question which helped me in solving the issue completely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top