Dear forum members,

I have the following problem:

since the upgrade of my website from Drupal 5.x to Drupal 7.x I frequently get the following error message when trying to access the website via my browser:

PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL
server through socket '/var/run/mysqld/mysqld.sock' (2) in
lock_may_be_available() (line 167 of /is/htdocs/wp1048090_8JCEI180MP/drupal7/

It seems like the connection to the database server is lost. After a while, without doing anything, the website does load again. The problem really is annoying as the visitors expect a reliable and stable website. Further, may the search engine not find my website with a detrimental effect to the SE rankings.

First, I have checked the access information in the file /drupal7/sites/default/settings.php. They are correct. However, sometimes I cannot login to my database through PHPMyAdmin with these login details. Sometimes, it takes a long time for the connection to establish. This is unusual.

I have already got in touch with my hosting company. According to them, there is no unusal activity and everything is deemed to work as it should.

Is there the possibility that the website generates too many database requests at a time and causes the connection to crash?

What can I do to solve the issue?

Thanks a lot for your feedback!! It is much appreciated.


VM’s picture

Is there the possibility that the website generates too many database requests at a time and causes the connection to crash?

It's possible that you are hitting a ceiling the host has set. have you inspected the site with the devel.module?

SilverSunrise1981’s picture

Hi Vm,

thanks a lot for your reply.

Could you please tell me in short how to use the devel module properly? I have no experience with it.

lantha’s picture

I am having the same problem but with a fresh installation of Drupal 7. The connection is lost for some time and then it is restored again. What can be done to remove this problem permanently. Also, how do i check if the database connection has hit the ceiling or how many database connections can Drupal do for a particular hosting?

Sjursei’s picture

This is one of the things that bothers me with Drupal. I can always find someone else having the same problem, but never a solution.

I have a (relatively) fresh install of drupal 7, and I get this error frequently. There is almost no activity on the site, as it is still in development, so I am pretty sure that my host is not the problem.

the only solution I have found so far is to reboot my hosting server, which I am getting pretty tired of.

Hope that someone with more insight than me can give an answer as to why this keeps happening, and how to fix it.

VM’s picture

when there aren't any solutions given it tends to be due to a lack of enough information to debug the issue.

how many queries when it dies?
any pattern to what kind of pages are being viewed when the DB dies, (views, panels, page with multiple blocks, ...)?
Have you employed devel to check slow queries? duplicate queries?

Other helpful information would be version of PHP, version of MySQL.

Obviously the issue isn't so widespread that it's affecting a large percentage of drupal users. Thus try not to be so quick to rule out the host, the hardware, the software (beyond Drupal) or the config of any of aforementioned just because you are the only user or there aren't many users.

Sjursei’s picture

Thank you for your reply.
I was a little frustrated when I wrote the post, things are going much better now.

I run Drupal 7.21 on Amazon EC2.
Server version: Apache/2.2.23 (Unix)
Server version: 5.5.29 MySQL Community Server (GPL)
PHP Version 5.3.20

As for now the error seems to happen at random, but I will try to reproduce it and gain a better understanding of what makes the DB die.

Sparkster’s picture

I seem to be having exactly the same issue and have the same setup as yourself.

Drupal 7.21 on Amazon EC2. [Micro instance]
Server version: Apache/2.2.23 (Unix)
Server version: 5.5.29 MySQL Community Server (GPL)
PHP Version 5.3.20

There seems to be quite a few people having this issue with no clear solution, or the solution which is posed relates to restart mysql which only seems to be a temporary fix.

Eager to find out what you've tried and if you've found a solution yet.

amMain2’s picture

i am using 1&1 Dual Unlimited, well in Germany. threr are 13 sites with databases in 13 languages. today the english one is crushed,
PDOException: SQLSTATE[HY000] [1040] Too many connections in lock_may_be_available() (line 167 of /homepages/.../htdocs/english/includes/

but after a while it is working again. no idea why.

logicbug’s picture

I've had the same issue. The problem IS NOT DRUPAL! There just isn't enough memory with the LAMP stack and MySQL Crashes. Creating a swap file fixed the problem for me as per

gingerfrog’s picture

I was running into the same issue trying to get too much out of a low spec cloud host, MYSQL just kept crashing, needing restarts of both httpd and mysqld services to get site working.

New swap file has fixed it for me.

zooter626’s picture

On a shared hosting service i got this message this morning:


The website encountered an unexpected error. Please try again later.
Error messagePDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in lock_may_be_available() (line 167 of /home/.../public_html/includes/

i contacted my hosting provider and was redirected to this alert:

"We are currently facing issues with the MySQL service on this server. We are working to identify the issue relating to the service.

Services affected include the MySQL at this time. Services not affected include Httpd, IMAP/POP, SSH and FTP.

During this outage, access to MySQL based websites will be slow or unavailable. All other services are online and available. We apologize for this inconvenience and we will continue providing updates as soon as possible."

so, in this case there was nothing i could do about it. :-(

edward0’s picture

#I just finished an Drupal 7.23 clean install on a Debian Wheezy machine with SELinux in enforcing mode.
#When I'm going to the browser address htt://myownhost I get ...

ERROR - The website encounter an unexpected error. Please try again later.
PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13) in lock_may_be_available() (line 167 of /var/www/includes/

#if I run setenforce 0 the problem dissapears. So I run:
ausearch -m avc -ts recent
time->Wed Nov 13 08:46:00 2013
type=SYSCALL msg=audit(1384361160.145:34): arch=40000003 syscall=102 success=no exit=-13 a0=3 a1=bfe38ab0 a2=b5973558 a3=0 items=0 ppid=2211 pid=2397 auid=4294967295 uid=33 gid=33 euid=33 suid=33 fsuid=33 egid=33 sgid=33 fsgid=33 tty=(none) ses=4294967295 comm="apache2" exe="/usr/lib/apache2/mpm-prefork/apache2" subj=system_u:system_r:httpd_t:s0 key=(null)
type=AVC msg=audit(1384361160.145:34): avc: denied { write } for pid=2397 comm="apache2" name="mysqld.sock" dev=tmpfs ino=8211 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:var_run_t:s0 tclass=sock_file
#I see that the source context is:
matchpathcon /usr/lib/apache2/mpm-prefork/apache2
/usr/lib/apache2/mpm-prefork/apache2 system_u:object_r:httpd_exec_t:SystemLow

#but according to the message it should be: scontext=system_u:system_r:httpd_t:s0
##I did change the source process from original #SELINUX System Administration page 96
/usr/lib/apache2/mpm-prefork/apache2 system_u:object_r:httpd_exec_t:SystemLow
chcon -t httpd_t /usr/lib/apache2/mpm-prefork/apache2 && echo $?
#confirm the changes
ll -Z /usr/lib/apache2/mpm-prefork/apache2
-rwxr-xr-x. 1 root root system_u:object_r:httpd_t:SystemLow 439852 Mar 4 2013 /usr/lib/apache2/mpm-prefork/apache2
service apache2 restart
[ ok ] Restarting web server: apache2 ... waiting .
#Test --> SUCCESS :-)
#On this case changing the source process context did the trick :-)#
#I was able to get this resolve thanks to the mentioned book just happened to have received as a present :-)
#Thanks to Sven Vermeulen & my brother who gave me the book :-)

bdanin’s picture

I got this error when I upgraded PHP to Version 5.5.8
So it seems Drupal won't run PHP -v 5.5.x

I'm using macports, so I can easily switch my versions. I reverted back to the default PHP 5 (PHP Version 5.3.28)
When I use PHP -v 5.3.x my sites load.

I simply edit my httpd.conf to change this

LoadModule php5_module modules/
#LoadModule php5_module modules/

I comment out my 5.5.x and restart apache to run Drupal locally.


bdanin’s picture

Note, the real issue has to do with your settings.php

I changed my database connection from "localhost" to ""
and now it's working. I had specifically defined localhost as in my version 5.3

Now I can use Drupal 7 just fine with PHP 5.4.x and 5.5.x as well as 5.3.x


mandclu’s picture

Changing my connection to "" fixed the identical problem for me, on two different local environments. Thanks for sharing the fix!

Moa Ponti’s picture

Seems to me that the problem arises from the use of the "date" in such content. Duplication of this field seems to conflict with other data from existing data in a certain form content type table.

jomarocas’s picture

for solution this issue i change any parameter of database in settings.php and later save, later back to original settings and works, is rarely this error