Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/...

Caprice

Active Member
Bumping my thread, because I have a few new problems.

My sites are all on the same dedicated server. They were loading really slow for a few hours and now they are completely down. And here is the error:

Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/caprice/public_html/system/database/mysql.php on line 6
Error: Could not make a database connection using caprice_opct1@localhost
Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/caprice/public_html/system/database/mysql.php on line 66

Where is the problem? In the server or somewhere else? The working processes were 2/25. What causes the problem with the high number of connections? Thank you.
 
Последно редактирано от модератор:
Warning: mysql_connect(): Too many connections

This limit is written in the global configuration fail of MySQL - /etc/my.cnf. The variable is named:

max_connections=

In the moment the limit exceeds, the error message starts to pop out. I think this is a problem with the server and the best solution is to contact the support of the service.
 
Последно редактирано от модератор:
Warning: mysql_connect() [function.mysql-connect]:

I’ve had a problem of this kind too. I will start from the beginning and I don’t want to understate you.

In cases like this you have to follow the start of the problem. The slow loading of a website in combination with the above means you have slow queries and your mysql server just can’t handle. The apache service starts to overload the server. I hope you have a root access so you can do this:
Open the config file which is placed in /etc/my.cnf Add the following lines (this will save all the queries to the database which need longer than 5 seconds to execute)

Код:
log-slow-queries = /var/log/mysql-slow.log
long_query_time = 5
2. Add this into the file - max_connections=300 (this will make the limit for max connections to the base more liberal – from the default value of 100 to 300. This will help you find the problem.

3. Now you have to create the following file with the touch command- touch /var/log/mysql-slow.log Give him rights to write on the server - chmod 755 /var/log/mysql-slow.log

4. Restart mysqld with /etc/init.d/mysqld restart

5. With the help of phpmyadmin optimize every table in your data bases. It is really possible that a broken table causes the slow queries.

6. Now you have to just sit back and wait for something to pop in the log above.

Consider that the limit we’ve upped above will make the server use more ram for the mysqld

If you are using a shared server, most of the services are letting users to use custom config file for mysql. Well, you can’t log the information there but you can get the parameter from step two running on the server by putting the file in /home/caprice/public_html/

Again, if you have root access to the server and if you don’t want to watch the terminal all day, you can use the script below, which will send you mail notifications when the server is overloaded. Here are the steps for using it.

1. Create a file /home/caprice/restart.sh, which contains something like this:

Код:
#!/bin/sh
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
if [ $check -gt 8 ]
then
[email protected]

host=`hostname`
res=`top -bn 1`
/usr/sbin/sendmail -f $ADMIN -t << sendemail
To: $ADMIN
Subject: High load on [$host]
Load Average : $check

Top command :
$res
sendemail
killall httpd
killall mysqld
wait 10
/etc/init.d/httpd restart
/etc/init.d/mysqld restart
fi
The script checks the load level of the server and if the value is above 8, you will get an e-mail. After that it stops all the mysqld and apache processes, waits 10 seconds for the load to get back in normal state and turns everything on again. If you want to just get an e-mail, without the restart module of the service, the script looks like this:
Код:
#!/bin/sh
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
if [ $check -gt 8 ]
then
[email protected]

host=`hostname`
res=`top -bn 1`
/usr/sbin/sendmail -f $ADMIN -t << sendemail
To: $ADMIN
Subject: High load on [$host]
Load Average : $check

Top command :
$res
sendemail
fi
2. Give it rights to run chmod 744 /home/caprice/restart.sh

3. Create a file /etc/cron.d/auto_restart which has:

Код:
* * * * * root /home/caprice/restart.sh >> /dev/null 2>&1
This will start the script every minute. It will check the server loads without bothering you with notifications you don’t want to see.

4. Restart the cron demon /etc/init.d/crond restart

If you have other problems write them here or send me a message at Skype, because we will solve the problems faster.
 
Последно редактирано от модератор:
Server down - Solution?

Alex, the first thing I’ve did was to contact the support. Unfortunately they didn’t get the problem like they shout and the answer they gave me doesn’t suit. They didn’t tell me what I can do to solve the problem.

Krasen, thank you man, really. Your answer is helping me a lot. I’m using a shared hosting service. After I get home, I will try to do what you’ve written, but I’m not sure in my success. I will contact you for some support.
 
Последно редактирано от модератор:
От: Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/

I’ve devided your old thread and create a new one from this posts. I will put it as a sticky because it is very important resource. About ktomov and his posts – Man I can handle to give you enough reputation :)
 
Последно редактирано от модератор:
От: Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/

For sure, this exceeded my expectations, really. ;)

I want to fire a little reminder. You can’t use the steps mentioned above if you are using a shared hosting service.
 
Последно редактирано от модератор:
От: Warning: mysql_connect() [function.mysql-connect]: Too many connections in /home/

It is normal, because you need root access to do something like that. Root access is given only to the people who work in the hosting company. You can’t get a root. If you use a dedicated server or a VPS then you can handle everything by yourself. Tomov is crazy about VPSs and I’m sure he doesn’t use shared hosting for his websites.

And after you started the discussion on reputation, I can’t give, because I’ve spread a lot to him. But I have to admit that he is the man who helped me really a lot with all my VPS issues.
 
Последно редактирано от модератор:
Open a connection to a MySQL Server - PHP

My opinion is that a query time of 5 seconds it too much. Every MySQL query that exceeds 1 or 2 seconds needs a revision.
 
Последно редактирано от модератор:

Горе