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 its 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:

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

And in your favorite editor find the line that starts mysql.default_socket and update it to read

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.

This entry was posted in WordPress and tagged , , , . Bookmark the permalink.

13 Responses to Mac, WordPress: “Error establishing a database connection”

  1. Dayakar says:

    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. Patrick says:

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

    Greetings

  3. Pingback: How to Install WordPress on Mac OS X (Leopard) :: Software

  4. Jens says:

    Thanks! You saved my day. I was configuring the time zone in php.ini.default because I got a lot af warnings in my local php site. It didn’t work untill I copied the file to php.ini. But then MySQL didn’t work and I was about to loose my patience. But your solution worked! Thanks :)

  5. Scrimmers says:

    Dude.. you rock.. i was bugged by this all afternoon

  6. Cody Robbins says:

    This saved me some debugging—thanks!

  7. Louis says:

    Thank you! Glad to have this resolved.

  8. Pingback: How to Install WordPress on Mac OS X (Leopard)

  9. Boris says:

    Thanks a lot. I did waste some time trying to figure this out. Wish WordPress would put this info on their website close to the “Download” button when they detect a Mac browser :)

  10. Dave Gordon says:

    got an error: “cp: /etc/php.ini: Permission denied” when I tried to copy /etc/php.ini.default to /etc/php.ini so I was unable to go any further.

    Any suggestions?

    Thanks.

    • Lloyd says:

      That should be sudo cp /etc/php.ini.default /etc/php.ini. I’ve updated the post above. Thanks for contacting me.

  11. Dave Gordon says:

    Thanks for your help. I was able to copy the file to php.ini but I was unable to modify the file using vi editor. It said the file was “read only”. Not sure how to change that. Any suggestions?

    many thanks

  12. Gerald says:

    Thanks a lot on this! I was pulling what little hair I have left completely out. Funny, this was working back in May on this same test machine I am testing my website on. Cranked it up this morning and BAM! I am not sure what I might have changed since then.

    I may post a link to this on the WordPress support forums. I did not see anything about it there, but I did not look through all of the posts with this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>