开发者

Should $new_link be used in mysql_connect()?

I'm maintaining an inherited site built on Drupal. We are currently experiencing "too many connections" to the database.

In the /includes/database.mysql.inc file, @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2) (mysql_connect() documentation) is us开发者_如何学Ced to connect to the database.

Should $new_link = TRUE be used? My understanding is that it will "always open a new link." Could this be causing the "too many connections"?


Editing core is a no no. You'll forget you did, upgrade the version, and bam, changes are gone.


Drupal runs several high-performance sites without problems. For instance, the Grammy Awards site switched to Drupal this year and for the first time the site didn't go down during the cerimony! some configuration needs tweaking on your setup. Probably mysql.

Edit your my.cfg and restart your mysql server (/etc/my.cfg in fedora, RH, centos and /etc/mysql/my.cfg on *buntu)

[mysqld] max_connections=some-number-here

alternatively, to first try the change without restarting the server, login to mysql and try:

show variables like 'max_connections' #this tells you the current number set global max_connections=some-number-here

Oh, and like another person said: DO. NOT. EDIT. DRUPAL. CORE. It does pay off if you want to keep your site updated, may cause inflexible headache and bring you a world of hurt.


MySQL, just like any RDBMS out there will limit the amount of connections that it accepts at any time. The my.cnf configuration file specifies this value for the server under the max_connections configuration. You can change this configuration, but there are real limitations depending on the capacity of your server.

Persistent connections may help reducing the amount of time it takes to connect to the database, but it has no impact on the total amount of connections MySQL will accept.

Connect to MySQL and use 'SHOW PROCESSLIST'. It will show you the currently open connections and what they do. You might have multiple connections sitting idle or running queries that take way too long. For idle connections, it might just be a matter of making sure your code does not keep connections open when they don't need them. For the second one, they may be parts of your code that need to be optimized so that the queries don't take too long.

If all connections are legitimate, you simply have more load than your current configuration allows for. If you MySQL load is low even with the current connection count, you can increase it a little and see how it evolves.

If you are not on a dedicated server, you might not be able to do much about this. It may just be someone else's code causing trouble.

Sometimes, those failures are just temporary. When it fails, you can simply retry the connection a few milliseconds later. If it still fails, it might be a problem and stopping the script is the right thing to do. Don't put that in an infinite loop (seen it before, terrible idea).


FYI: using permanent connections with Drupal is asking for trouble. Noone uses that as far as I know.


The new_link parameter only has effect, if you have multiple calls to mysql_connect(), during 1 request, which is probably not the case here.

I suspect it is caused by too many users visiting your site, simultaneously, because for each visitor, a new connection to the DB will be made.

If you can confirm that this is the case, mysql_pconnect() might help, because your problem is not with the stress on your database server, but the number of connections. You should also read Persistent database connections to see if it is applicable to your webserver setup, if you choose to go this route.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜