If you're seeing PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server, it's because Drupal can't connect to the database server specified in your settings.php. There are several possible reasons for this:

  1. Your MySQL database server is not responding because it has crashed or been stopped. If you have shell access to your server and command-line knowledge, check to see if the MySQL process is running and accepting connections. It may need to be restarted. Check with your hosting company or system administrator to investigate. This can happen at any time, even if you haven't made any configuration changes.
    • In many of the case, if no change done at server configuration or code level, there is possibility that Disk space of MySQL data partition is exhausted and almost fully utilized. To check quickly with df command
  2. The connection information you specified in settings.php is incorrect. Pay particular attention to the 'host' parameter as it instructs Drupal where to reach the database server. This is most likely to happen when you have moved the site to a new server or changed the server or database configuration.
  3. If you are checking your settings.php into version control and sharing with other developers who use a different local setup, the port number may differ. For instance, development environments using DrupalVM use the standard port of 3306. Acquia Dev Desktop uses 33067 by default.
  4. If you are running a localhost on a macbook pro your permissions could have gotten corrupted, and you are simply being denied access. Check other applications that may have strict permission requirements (such as a VPN). If those fail, you may need to use Disk Utility and rebuild or repair you permissions.
  5. If you are using something like MAMP Pro, and in your settings.php (or local.settings.php if you use that), if the host is set to 127.0.01 rather than localhost, in MAMP Pro under MySQL, there is an option to 'Allow network access to MySQL - select 'only from this Mac'. If you are using 127.0.0.1 instead of localhost, you will need to select this option.

Comments

Siridivi’s picture

This one was very frustrating. Fixed this one on the command line. Using RHEL 6.

Check if SELinux is running. Run the following command (Linux newbies, ignore the starting # it's just there to indicate a command prompt).

# getenforce

If the result is "Enforcing", then turn off SELinux temporarily by running the next command. If it's not SELinux is not the issue.

# echo 0 > /selinux/enforce

Try your site again. If it comes up correctly then SELinux is blocking the site. We really do want a secure server, so turn enforcing back on.

# echo 1 > /selinux/enforce

I have found two pieces to this puzzle: mysql and httpd.

The following command opens up the standard 3306 for MySQL communications (probably already set).

# semanage port -a -t mysqld_port_t -p tcp 3306

The following commands opens up communication between Apache and MySQL (don't get me started!). Thanks goes to http://serverfault.com/questions/240015/how-do-i-allow-mysql-connections....

# setsebool httpd_can_network_connect_db 1

This change is not persistent across reboots. To make changes persistent across reboots run the following command.

# setsebool -P httpd_can_network_connect_db on

Those were the steps I needed to take. If your server isn't hosted locally (on the same server as your web server), you may also need to allow remote connections to MySQL. Here are some resources for that.

http://wiki.r1soft.com/display/CDP3/Allowing+Remote+Connections+to+MySQL...
https://dev.mysql.com/doc/refman/5.5/en/create-user.html