Mac, WordPress: “Error establishing a database connection”

If you get “Error establishing a database connection” when trying to set up WordPress.org on your Mac, are sure that the database name (DB_NAME), username (DB_USER) and password (DB_PASSWORD) are correct, the solution is very likely that you need to set mysql.default_socket = /tmp/mysql.sock in /etc/php.ini.

I read on the WordPress Support forums “Error establishing a database connection on OS X 10.5” where jonokane had the novel solution of changing the database host (DB_HOST) to capital L, ‘Localhost’.

mrogers writes that “This is definitely a bug in Wordpress 2.5, because every other web app I have running on this machine uses “localhost” and it connects to the MySQL server just fine.”

The problem can be isolated to the PHP function mysql_connect() and it’s error return value is being suppressed:

Warning: mysql_connect() [function.mysql-connect]: Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’

There is quite a bit of misinformation about what the problem and solution out there. The Apple doc “Mac OS X Server 10.4.4: Improvements to Apache/PHP/MySQL interaction” from January 2006 gives clues as to the problem. On Server, a custom MySQL is included and the doc includes:

If you have downloaded and installed MySQL yourself but are using the pre-installed version of PHP, note that your custom version of MySQL might be configured to use the old MySQL socket location, /tmp/mysql.sock. The version of PHP in this software update uses the newer location /var/mysql/mysql.sock by default.

The problem with Apple using the “newer location” is that MySQL hasn’t had a major release since 2005 and so their packages don’t use the newer location, and there is no custom MySQL package for the rest of us. We have to download the regular MySQL 5 package from MySQL.com.

Jonokane’s novel solution likely works because instead of using a socket for the local connection, it attempts a TCP/IP connection, and ‘Localhost’ resolves to ‘localhost’. That will only solve the problem for WordPress though, and you would have the same problem with other PHP applications.

The correct solution is to:

$cp /etc/php.ini.defaults /etc/php.ini
$sudo chmod +w /etc/php.ini

And in your favorite editor find the line that starts mysql.default_socket and set mysql.default_socket = /tmp/mysql.sock.

Now restart Apache, and it should be golden.

You won’t encounter this problem if you are using the packages from Mac Ports or MAMP, but those would be their own adventures.

Possibly Related Classroom Projects From DonorsChoose.org Powered by Social Actions

2 Comments

  1. Dayakar
    Posted September 27, 2008 at 5:50 am | Permalink

    Thank you very much, Lloyd! Your entry “Error establishing a database connection” had helped me to install Wordpress in my system Mac OS 10.5.4.

  2. Posted October 14, 2008 at 9:44 am | Permalink

    Thank you so much for this solution. I was nearly going nuts with this problem.

    Greetings

Post a Comment

Your email is never shared. Required fields are marked *

*
*