I get this error whenI go to my drupal 7 site:

PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in lock_may_be_available() (line 165 of /home/xxxxxxxx/public_html/includes/lock.inc)...

Too many connections? How do I fix this?

Thanks

Comments

Technonow’s picture

In Sites/default/settings.php try changing 'localhost' to '127.0.0.1'

It's easier for the programme to find the numbers than the words.

Not sure why, but it does work.

davidk2’s picture

I am trying to find solution...

juroon’s picture

Put whatever domain you're using at the moment into your base URL in settings.php. If you're working with a test URL before activating your domain name, set the base URL to your test domain. You'll have to remember to put it back when you activate your domain.

Also if you're using a test URL, you'll need the name of your site folder in sites to be default and not your final domain name.

cooldeeponline’s picture

Worked for me... thanks!

# $cookie_domain = '.example.com';
# $cookie_domain = 'localhost';
   $cookie_domain = '.testwebsite.com';

Make sure to always start the $cookie_domain

* with a leading dot, as per RFC 2109.

eVagabond

Karll’s picture

how would this apply to a multisite install?

Karll

markosaurus’s picture

It's not easier for the program to find anything.

"localhost" will just be an alias of "127.0.0.1" in the same way that "www.yourdomain.com" would be an alias of "254.432.456.554" or something else online. It's just a human-friendly version of the same thing.

If the alias is missing, drupal won't find the database though.

darol100’s picture

Works thanks

- Darryl Norris
Be Connected: Website | Twitter | LinkendIn | GitHub

robdroy’s picture

Changing from localhost to 127.0.0.1 worked for me. Thanks!

Robert Roy

Mamouri’s picture

Arwym’s picture

I am having a similar or identical error ever since I uploaded my Drupal 7 site to my hosting provider's server. Locally, I wasn't having any problems, so I assumed it could be cache-related, because none of the solutions above worked for me. I cannot access anything on the site; it's always the same error. I also truncated the cache tables in the database to see if that helped. Nothing so far. I am in a real hurry right now, and don't know what else to do.

Is there anything else, caching-related, that I could try? Since I cannot access the GUI, I cannot use the administrative options for clearing the site's caches. Is there anything else I can do? Again, I am using Drupal 7.9, and all my modules were updated and tested before uploading the installation to the production server.

kbrinner’s picture

I have drupal commerce working locally on my machine but am getting the same error after uploading my site to my hosting provider. I uploaded the files and imported the db from my local machine, and I also adjusted the settings.php file as needed for the db.

This is the error I get:

Error

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

Tried making the change suggested above (in settings.php, change localhost to 127.0.0.1) and got a very similar error after this change:

Error

The website encountered an unexpected error. Please try again later.
Error messagePDOException: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in lock_may_be_available() (line 167 of [path to my subdomain]/includes/lock.inc).

Switched from 127.0.0.1 in settings.php, and then tried changing the base url in settings.php to the staging site I'm setting up and got the same error as the first error

Error

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

kbrinner’s picture

When I uploaded my site to a different hosting provider, I had no problems, and I followed exactly the same procedure for both hosting providers. Of course the first hosting provider (where it didn't work) claims the issue is on my side (could be) but it is suspicious that I had no problem with a different hosting provider. It works using KnownHost, not working using APlus.

obutuz’s picture

Am getting the same error , Drupal 7, any solution yet/ I tried changing "localhost" to "127.0.0.1" but still nothing. Help a bro!

PDOException: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in lock_may_be_available()

gloscon’s picture

Sometimes this happens when you run out of disk space and hence mysql doesn't start.

Gloscon Solutions Inc : Drupal Development and Support, India
+1 (323) 31903609
Skype : bpocanada

camerongreen’s picture

I've seen this same error in a number of places, and I'm yet to see a solution so would appreciate anyone above who has solved it posting.

PDOException: 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/content/foo/bar/html/includes/lock.inc).

Lots of answers without any explanation of why they will work (which often a sign that they won't work).

Drupal 6 was working fine, but when I upgrade to 7 I get this error. I got this after transferring a working site on my local machine to my hosting provider which is something I've seen come up a few times. I can connect fine from the command line to mysql using exactly the same parameters, just not in the settings.php. The server is not on the localhost so the 127.0.0.1 solution won't work, though I did try swapping the host name for the IP just to check, no luck.

Edit: OK I worked it out, was simple in the end and was a user error. I'd used some old code from my D6 settings.php which was based on the old url connection strings. The new D7 settings file was overwriting this at the bottom of the file for development but not production. I pulled down a pristine copy of the latest D7 did a vim diff on my settings.php and the default.settings.php and the error became evident.

jonline’s picture

I had the similar issue... After doing some investigation I found the problem with host name. So I have changed it to the host name from localhost and it resolve the issue. Hope this will help...

With Regards

Jilani Jidni

HFlame7’s picture

In my case, the process/service "mysqld" had stopped (not sure why). But starting it up again solved it.

a.knutson’s picture

Thanks a lot for posting this solution. It worked in my case as well! Although I had to '/etc/init.d/mysql restart' for ubuntu

commonpike’s picture

It means, as it says, "Can't connect to local MySQL server".

"lock_may_be_available" is basicly the first database call drupal makes. if it cant connect there, it will fail there first, and that has nothing to do with a lock.
so mysql isnt running, or your config is wrong, or the webserver cant connect to the database server, or something like that.

if there is something wrong with a lock, it will return a different error.

*-pike

*-pike

doliveros’s picture

.

Karll’s picture

This is the error I am getting about once a week to two weeks, once I restart it is fine but eventually reoccurs. I am running a multisite configuration with about 8 sites running independent databases. (each site has its own database) This is installed on Amazons EC2(Ubuntu 12.04) and running on a micro instance, Each time this has happened all sites cannot connect - On one site I changed the database configuration from "localhost" to 127.0.0.1 but still wont connect for that site as well.

I suspect it may be MYSQL crashing and have never had this issue on other Drupal installs. If I figure out what is causing it I will post back

Error Message:
Error 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/run/mysqld/mysqld.sock' (111) in lock_may_be_available() (line 167 of /var/www/drupal/includes/lock.inc).

Karll

a.knutson’s picture

I'm not so sure it has anything to do with the fact that it's a multi site install. I'm having the exact same scenario reoccur on a similar setup. I'm running multiple EC2 micro instances for a few different drupal sites. Two of my EC2's have the mysql crashing issue that you have described. Once or twice a week the PDO error is thrown and doing "sudo service mysql status" on the server's terminal shows that mysql stopped. If I restart mysql, all is good for another 3-7 days. Inevitably it crashes all over again and I'm repeating the process.

Please keep informed if you have found a solution Karll. Thanks for posting this!

ykyuen’s picture

I am getting the same error even for single drupal instance on a EC2 micro instance with MariaDB. So i guess it is not related to multi-site.

Vale80’s picture

Hello,
I'm getting an error which I believe to be very similar to the most of this topic.

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1226 User 'MYDBUSER' has exceeded the 'max_queries_per_hour' resource (current value: 150000) in lock_may_be_available() (line 167 of /MYDOMAIN/includes/lock.inc).

I'm using a MySQL db and Drupal 7.16.

I noticed that the error comes out when I'm using Views and a Block created from Views. And after I have installed Userpoints with all the Contributed Modules available. Maybe this can help. :)

I have tried to follow all the steps mentioned above but I'm still receiving this error and my hosting is not helping me because the error is on my site.

Does anyone have a solution please?
Regards,

Cheers,
VP

dberlind’s picture

Hi,

I have Drupal 7 installed on a Ubuntu-based EC2 Microinstance and am having the exact same problem. In my case, the site is going down almost daily. I'm desperate for a solution if anybody has figured this out.

Thanks!

David

Tux Love’s picture

Same here, again on an EC2 Microinstance that I use for testing a production site that runs fine on a 'proper' server.

I don't have an answer (yet), but note these comments in this link on the AWS forums:

  • "I can confirm the replication of this issue. I have attempted this on multiple micro instances and they all display the same error. Websites function properly for a while, but all ultimately fail with the mysql error."
  • "Moving to a higher grade Instance does the job. I am not sure if there are certain setting then can make a micro instance work."

I set a tail -f /var/log/mysql/error.log and get this following every crash:

130713 14:33:07 [Note] Plugin 'FEDERATED' is disabled.
130713 14:33:07 InnoDB: The InnoDB memory heap is disabled
130713 14:33:07 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130713 14:33:07 InnoDB: Compressed tables use zlib 1.2.3.4
130713 14:33:07 InnoDB: Initializing buffer pool, size = 10.0M
130713 14:33:07 InnoDB: Completed initialization of buffer pool
130713 14:33:07 InnoDB: highest supported file format is Barracuda.
InnoDB: Log scan progressed past the checkpoint lsn 1247297728
130713 14:33:07  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
InnoDB: Doing recovery: scanned up to log sequence number 1247300088
InnoDB: 1 transaction(s) which must be rolled back or cleaned up
InnoDB: in total 1 row operations to undo
InnoDB: Trx id counter is A9700
130713 14:33:07  InnoDB: Starting an apply batch of log records to the database...
InnoDB: Progress in percents: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
InnoDB: Apply batch completed
InnoDB: Starting in background the rollback of uncommitted transactions
130713 14:33:08  InnoDB: Rolling back trx with id A95AF, 1 rows to undo
130713 14:33:08  InnoDB: Waiting for the background threads to start

InnoDB: Rolling back of trx id A95AF completed
130713 14:33:08  InnoDB: Rollback of non-prepared transactions completed
130713 14:33:09 InnoDB: 5.5.31 started; log sequence number 1247300088
130713 14:33:09 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306
130713 14:33:09 [Note]   - '127.0.0.1' resolves to '127.0.0.1';
130713 14:33:09 [Note] Server socket created on IP: '127.0.0.1'.
130713 14:33:09 [Note] Event Scheduler: Loaded 0 events
130713 14:33:09 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.31-0ubuntu0.12.04.1'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

Can anyone spot any clues in there?

bazza14’s picture

I've just had this problem on a Drupal 7 site with Blacknight shared hosting. The basic problem is, as others have said elsewhere on this thread, that it can't connect to the database. In my case it was because this hosting company assigns the data base name, user and localhost when you create the database. Changing these settings in /sites/default/settings.php fixed the problem so try checking with your hosting company that you have these set correctly.

<?php
$databases
= array (
 
'default' =>
  array (
   
'default' =>
    array (
     
'database' => 'db121xxx_mysite',
     
'username' => 'u121xxx_auser',
     
'password' => 'xxxxxxxxxx',
     
'host' => 'mysqlxxxxxint.xx.blacknight.com',
     
'port' => '3306',
     
'driver' => 'mysql',
     
'prefix' => '',
    ),
  ),
);
?>
unnikrishnan’s picture

Hi,

Restarting Mysql fix this error for me.

Also check mysql error logs for any error that might cause the crashing of the process.

Thanks,
Unnikrishnan B.

jayprakasht’s picture

Hi
I faced similar issue and error while installing Drupal 7 on Ipage hosting site. But after trying some permutations and combinations
I could resolve this issue by changing the localhost to correct mysql hostname

Go to your cpanel – then mysql database – find the generate code column and hit the black board icon – that should give you your hostname!!!!

Good Luck
Jayprakash

michaelversus’s picture

The solution for me was to change the host from "localhost" to "db21.xxxxx.com" and port to "3306" under the advanced settings during the installation
you can see the host in the PHPmyAdmin page.

Good Luck!

bluesky_still’s picture

this disk is full or mysqld is shutdown for me

天空依然蔚蓝...
http://www.yongbuzhixi.com

sharif.elshobkshy’s picture

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

sudo service mysql start fixed the issue for me.

Regards.

malovanets’s picture

That's obvious, however many people (me as well) have this error as recurring. You can't just monitor whether your mysql server crashed or not and restart it every single day.

jessewwilson’s picture

I was getting this error, similar but not exactly the same however I found the solution in this thread after 8 1/2 hours of banging my head off the wall.

"PDOException: SQLSTATE[HY000] [2002] Connection timed out in db_table_exists() (line 2761 of...."

The problem does seem to be in the settings.php file. I was trying to migrate a local build from acquia dev desktop to a 1&1 hosted site. All of the settings were correct but apparently the location within the settings file was the problem? Everything else seems to be the same.

I moved the db array block from the end of the file (the way that it's created by acquia dev desktop) to before the "$update_free_access = FALSE;" code.

Very strange, but I'm relieved just the same.

robinthakur’s picture

Ok, this has happened every single time I have deployed to Production on Go Daddy, has anyone worked out what is causing it?

  • I clear my cache
  • Tar up my files
  • Export all my database from MySQL
  • FTP my tarred up files
  • extract the tarred files
  • Import my database
  • upload a settings.php file to the root of the sites/default containing the Godaddy MySQL host i imported to and the username, password and database.
  • I browse to the site and I get this error every single time
  • If I browse to install.php it detects that drupal is already installed, but then if I click on view current site, I get the same error

Usually it seems to just go away, but this time it won't., no matter what I do. Any suggestions? I develop using Acquia Drupal's MAMP stack, could this be causing it?

Any help is gratefully received, Rob

Jaypan’s picture

I use this method, works every time:

1) Install the Backup and Migrate module on the existing installation
2) Take a backup using this module
3) Copy the entire Drupal file system (minus settings.php) to the new server
4) Install Drupal as a fresh installation with this file system
5) Enable the Backup and Migrate module
6) Restore the database in this new installation using the backup from step 2

er.pushpinderrana’s picture

I also followed this approach and it work for me.

Pushpinder Rana

robinthakur’s picture

Thanks to both of you for your advice, I might just do that in future! I found that it is Acquia Drupal leading to these issues, specifically the setttings.php format which basically stops the site connecting to the database in MySQL. I took a "vanilla" Drupal installation (from my NAS!) and looked at the settings.php file which had a completely different layout, copied it to my hosted copy, changed the db values, and it worked first time. This is rather annoying that it is not more widely known. Had I known I'd have such issues deploying an Acquia Drupal site, I'd have used another method to build my dev environment, but I think Jaypan's suggestion get's around the issue
Cheers,
Rob

GiorgosK’s picture

my error was similar to all above but SQLSTATE different number
PDOException: SQLSTATE[42000] [1044] Access denied for user
and it had to do with the fact that the user was not actually permitted to access the database
and this happened when renaming database through the CPANEL/phpmyadmin rename operation !!

Zollcore’s picture

I've had about as much fun with this one as a lot other people seem to of had from all the threads with no concise answer.
My answer may be of no assistance because it will probably only work in certain environments. My environment is a local XAMPP install.
I tried it all: localhost, 127.0.0.1, different ports, different users, restarts.... No change.
I was staring at settings.php, thinking of what else to attempt. Heck, lets just go with my local IP 192.168.1.##. Save, refresh, boom. Everything was back to the way it used to be.
Why did it require this change? No idea, I didn't change any settings. Just shutdown XAMPP, left for the day to come back with a morning of troubleshooting.
I am not looking forward to migrating upon completion of this site.

Hope this helps some of you out, and you don't have dynamic lan IPs.....

Copy, Paste, Bash Head, Google, Fix

alexharries’s picture

Very strangely, this (changing "localhost" to "127.0.0.1") has also worked for me on Greengeeks shared hosting.

I'm VERY confused...!!

Jaypan’s picture

I just had this error suddenly, without having made any changes. I contacted my host, in this case, it was a bug in Cpanel, and required a MySQL restart.

alt36’s picture

A comment which might clarify something that's come up a few times here: for MySQL on linux, connecting via localhost is *not* the same as connecting via 127.0.0.1 (even though they will both resolve to "this computer"). With MySQL, connecting to "localhost" will connect via a local unix socket, whereas connecting to 127.0.0.1 uses a network connection. Which one you should use will, as always, depend on local configuration - but they are *not* the same, and there are plenty of reasons why one might work and the other fail.

See e.g. http://dev.mysql.com/doc/refman/5.1/en/connecting.html

yuliansu’s picture

I am clueless now, after this error happened few times, I think it must be a way to solve but I didn't get yet, so I really need some guidance.

It recovered by restarting MySQL at first time, and Changing from localhost to 127.0.0.1, port to 3306 works in second time, but I still getting this error and recovered after restarting MySQL:

The website encountered an unexpected error. Please try again later.
Error messagePDOException: SQLSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in lock_may_be_available() (line 167 of /var/drupal/includes/lock.inc).

I really don't want to repeating this process all over again as I always need ask someone's help on. So can anyone tell me what I should do step by step?

Thanks!

Drupal theme design and implementation:
www.yuliansu.name/portfolio.html