Main Content

Connecting to MySQL 5 from PHP on Mac OSX Leopard

Archive - Originally posted on "The Horse's Mouth" - 2007-12-03 09:40:53 - Graham Ellis

What is the difference between "localhost" and "127.0.0.1"? Everything, it seems, when you're trying to connect MySQL (and I have version 5.0.37 Community at the moment) running on Mac OSX - the latest Leopard release - with their PHP release including the MySQL drivers. It's PHP release 5.2.4.

In a nutshell:
mysql_connect("localhost","wellho","nottherealpassword");
FAILS with an error message:
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Library/WebServer/live_html/test/test.php on line 2

but:
mysql_connect("127.0.0.1","wellho","nottherealpassword");
works just fine.

Another gem ... to add to the loss of classic mode (serious issue for us FrameMaker users!) and the movement of configuration files. I have to admit this hasn't been the easiest of upgrades, and I wait to see whether I have more issues to find.

If you want to learn all about Apache httpd installation / configuration - and the PHP and MySQL elements too - see our LAMP deployment course. I'm the tutor and the "L" for Linux is close enough to the "M" for Mac OSX for it to be pretty damned close and very effective training. And I can and will show you how it works on my Mac too ;-)