PHP脚本生成太多的MySQL连接

I have a script that check for several twitch channels every 5 minutes, so I can have a list of the streams I want on my website that are currently live.

The problem is, this script is generating a lot of mysql_connections and i'm getting some errors about max connections limit.

As far as I know, if I open a connection to mysql and run several queries on my script, it's still one connection right?

My script goes like this:

1) connect to the database

2) check the current time

3) mysql query that updates a table with site tasks, inserting current time

4) mysql query on the streamers list table

5) for every record, it gets a xml on twitch with the data and saves to the database if it's online or not, how many viewers, etc

6) mysql_close

Does anyone have any idea why this is happening? At first I thought it was another GoDaddy bug, but since this only happens when I'm running this task, I didn't bother to talk to them before check this.

Try persistent conections in php.ini, http://php.net/manual/en/mysql.configuration.php also since you will use persistant and the next script's instance might reuse this connection you might want to avoid mysql_close

Edit the my.cnf configuration file. On CentOS, RedHat and similar distributions this is at /etc/my.cnf; other distros will store it elsewhere.

Under the [mysqld] section add the following setting:

max_connections = 200

Now when you restart MySQL the next time it will use this setting instead of the default.

200 is a number that you are specifying that is more than the existing no. So assuming the current setting is 100 you can set a higher number.

Additionally, if you have root/admin access to MySQL then you can send the following command to increase the max connection on the fly till the server is restarted.

set global max_connections = 200;

Note that increasing the number of connections that can be made will increase the potential amount of RAM required for MySQL to run. Increase the max_connections setting with caution!

Use include_once ('connectdb.php'); at the top of your script. and connectdb.php should be something like you wrote at your comment.

connectdb.php

$conex_site = mysql_connect(server, user, password); mysql_select_db(database); 

EDIT 1:

<?php

    $MyConnection = mysql_connect(server, user, password) or die(mysql_error());
    mysql_select_db(database) or die(mysql_error());
    return $MyConnection;//or return true;
}
?>
------------ mysql_connect.php -------------

------------ update_streams.php ------------
<?php
$con=include_once('mysql_connect.php');//or just include once('mysql_connect.php');