In a previous post I talked about MySQL 5.5 and Mac OSX.
In this post I’ll go through fixing the problems with PHP and connecting to your local mysql install, using “localhost”.
The problems began a while back, with lots of errors in my apache error log saying:
[error] [client ::1] PHP Warning: mysql_connect():  No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in ...
In the rush I was in, I quickly changed my mysql connection to use: 127.0.0.1, which is the IP of your localhost. So basically the same thing.
Today I’m doing some freelance work for a customer, who has some problems with his server, after the PHP version was upgraded. I decided to fetch all his php files to my local Mac and then run through his webshop, fixing any errors I might see in the log etc.
When starting the my apache and running the website, I quickly found the MySQL connect error again.
Since this project should be a “search and fix” mission, I didn’t have time to change the mysql_connect(xxx) statements in the code (yes yes, not my code, so I didn’t create the mess…), so instead, I wanted to fix my local PHP->MySQL connection.
The fix was relatively easy, and only contains 1 to 2 steps:
Step 1 (if needed)
If you haven’t activated php.ini on your local install, open a Terminal and write the following command:
sudo cp /etc/php.ini.default /etc/php.ini
This copies the default php settings to the php.ini file, which the apache server uses.
Then restart your apache server. (Using System Preferences->Sharing->Web sharing)
Your PHP is now using the php.ini file.
Open /etc/php.ini file using your favorite text editor.
Goto line 1216 (or search for “mysql.default_socket = ” without the quotes) and change /var/mysql/mysql.sock to /tmp/mysql.sock
Restart your apache server and you should now be able to connect to localhost again.
Still have problems?
If you still have problems, then try the following:
Open Terminal and write:
It should print something like this:
mysqladmin Ver 8.42 Distrib 5.1.53, for apple-darwin10.3.0 on i386 Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL license Server version 5.1.53 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /tmp/mysql.sock Uptime: 2 hours 52 min 6 sec Threads: 3 Questions: 58 Slow queries: 0 Opens: 16 Flush tables: 1 Open tables: 9 Queries per second avg: 0.5
The path in the UNIX socket is the “localhost” connection point. So go back to Step 2 and use that path instead.