If you are getting this error on your local WordPress installation using Mac OS X Leopard, there is a simple fix. You might be seeing this error even though PHP is working fine, and MySQL is working fine. In order to get WordPress to work create a php.ini file.

sudo cp /private/etc/php.ini.default /private/etc/php.ini

Change permissions and edit the file

sudo chmod 777 /private/etc/php.ini
vi /private/etc/php.ini

Change mysql.default_socket = [blank] to

mysql.default_socket = /private/tmp/mysql.sock

Restart Apache and you should be good to go!

sudo apachectl restart

20 Comments

Alf Mikula

Thanks, that worked like a charm!

Dibi Store

Hey thanks you!

kw

Cool… can you edit php.ini with pico? It makes more sense to me. :-)

I’m on 10.4 but am going to give it a try… thanks for the tip!

Wordpress Themes

How effective has wordpress been on Mac? I have not heard alot about its use on Mac.

Jeff

@Wordpress Themes, It doesn’t matter if you’re running a Mac or a PC or anything else for that matter, as long as you have the correct versions of PHP and MySQL. See http://wordpress.org/about/requirements/ for more info.

:(

What does that mean? Lol.

L. S.

Yes, that works, but don’t chmod 777; you end up giving anyone the right to edit that file and possibly hack parts of your system. Just set the “mysql socket” line mentioned.

L. S.

Yes, that works, but don’t chmod 777; you end up giving anyone the right to edit that file and possibly hack parts of your system. Just set the “mysql socket” line mentioned. There shouldn’t even be a “7″ anywhere in those permissions.

bimble

Brilliant, worked perfectly. and even better, you were the first hit in google :)

Wordpress on OS X Tiger - “error establishing a database connection”

[...] info on locating and modifying your php.ini file, see the Bust Out Solutions [...]

Leslie Wong

Thanks for this tip. It worked for me in Tiger, 10.4.11 on a PowerBook G4 12″.

Chris

Absolutely amazing, this one frustrated me for a long time. One thing to note is that I have found multiple possible causes for this. For future people coming here, to diagnose whether this is the issue causing the problem it is useful to add:

echo(mysql_error());

to the wp-db.php file within the wp-includes directory just before the html for the error is printed. If it mentions socks in the output error message on refresh, you can know this is the problem. cheers!

Michael Grech

Good work, this works on Snow Leopard as well.

thiudis

Thanks – worked great!

Spenser

Thanks for this. @Chris, good advice for future errors.

Dave White

Thanks for the fix. What was strange for me was that everything was working fine one day and the next I was getting this error. I did run a security update from Apple in between though.

Stephan Kristyn

Great! I will mention this in my blog article at http://meshfields.de/install-magento-on-unix/

» Install Magento on Unix with mcrypt

[...] Make sure you have php.ini in the /etc directory. It will probably be php.ini.default to start, particularly if you have just done the Snow Leopard upgrade, so rename it with cp or mv. Ensure that you have enable_dl = On but do not remove the ; in front of ;extension_dir = "./". Add one line to the .ini file in the Dynamic Extensions section. extension=mcrypt.so. Also comment out the line that contains /var/mysql/mysql.sock or change the path to /tmp/mysql.sock. The default mysql.sock location in the php.ini.default file is a bug. In case you are running in problems with your other databases it is always a good idea on MacOSX to use the /tmp/mysql.sock path because this reportedly solved problems in the past as desrcibed here. [...]

Issues caused by upgrade to Snow Leopard « Helmers Software

[...] When I tried to access my local WordPress instance in the browser it started complaining “Error establishing a database connection“. It turned out that (again) the upgrade had thrown away a file, this time the victim was /private/etc/php/ini. This guy has the solution: [...]