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.

21 thoughts on “Mac, WordPress: “Error establishing a database connection”

  1. 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. Pingback: How to Install WordPress on Mac OS X (Leopard) :: Software

  3. 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 :)

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

  5. 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 :)

  6. 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.

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

  7. 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

  8. 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.

  9. Pingback: Got an “Error establishing a database connection” in Mac OS X 10.6? « Gabriel de Kadt

  10. You need to update the line:

    mysql.default_socket = /tmp/mysql.sock.

    There should not be a period at the end of the line.

    Thanks for the post, this helped me tremendously!

  11. I’m going nuts, I’ve tried all the above an still the same.
    The stupid thing is I just did a full time machine restore. All my sites (running as vhosts) work, except for one whose vhost entry had gone missing for some strange reason. After adding the entry back I’m now getting the dreaded ‘Error establishing a database connection’ even though the other sites, running WordPress as well, work like a charm. Anyone else have a clue as to what I might be missing?

  12. And never mind, turns out Time Machine was even pickier than I thought and restored all MySQL database users, except for the one matching the site where it also decided not to restore the vhost bit.

  13. DUDE You?e a lifesaver. I’ve reinstalled MySQL twice already. All I had to do was change the location of the mysql.sock. Thanks. Merry Christmas and have a Happy New Year

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>