Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm getting errors whenever I try to connect locally to mySQL on Mac OS Snow Leopard 10.6.4. I'm running mySQL 5.1.46. I can log in using the console. I'm running PHP 5.3.2 and my phpinfo() page is working fine. So I've got PHP running, I can log into the mySQL server using both the console and Sequel Pro.

So I suspected that it was a socket issue. In my /etc directory, I don't have php.ini, I have php.ini.default. So I go in there, and every place I see "default_socket" I change it from:

/var/mysql/mysql.sock

to:

/tmp/mysql.sock

I see it in the following locations:

pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket = /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock

And when I go to connect to phpMyAdmin, I get the following error: Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly.

And when I look in my phpinfo() file, I get the following under mySQL:

mysql.default_socket /var/mysql/mysql.sock /var/mysql/mysql.sock

I restart Apache... heck, I restart the whole computer. Still, nothing. I know mySQL is working, I know PHP is working, but I can't get them talking.

Any help?

share|improve this question

migrated from stackoverflow.com Sep 9 '10 at 2:01

This question came from our site for professional and enthusiast programmers.

7 Answers 7

It does not have to be a problem related to sockets. Try connecting to MySQL from a simple PHP script:

<?php
error_reporting(E_ALL);
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
echo 'No errors';
?>

For your PHP.ini path, look in phpinfo(); for "Loaded Configuration File". It does not have to be located in /etc/php.ini.default

share|improve this answer
    
Lekensteyn, I created a page named "test.php" and pasted your code with the appropriate un/pw and got this error: Warning: mysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Library/WebServer/Documents/test.php on line 3 Warning: mysql_connect() [function.mysql-connect]: No such file or directory in /Library/WebServer/Documents/test.php on line 3 No such file or directory –  Jeff Sep 7 '10 at 19:46

PHP should be looking for a php.ini file (and not php.ini.default) to load, so try renaming php.ini.default to php.ini. See Install Apache/PHP/MySQL on Snow Leopard.

share|improve this answer
1  
This was the one that fixed it. Then I read Vikash's answer to see why. Thanks so much! –  Jeff Sep 7 '10 at 19:53

Bring the mysql.sock to PHP

One problem that has come about with MySQL and Leopard is the location of the mysql.sock file. Previously, the default location for this file was in the /tmp directory. That location has now moved to the /var/mysql directory. PHP will look for it there. Unfortunately, the default location from the MySQL will still place it in the old location. We can fix this by creating a my.cnf configuration file in the /etc directory. Save a file with the following contents to /etc/my.cnf:

[client] socket = /var/mysql/mysql.sock

[mysqld] socket = /var/mysql/mysql.sock

In the terminal window, type the following commands to create the directory for the sock file:

sudo mkdir /var/mysql sudo chown _mysql /var/mysql

Source: http://www.procata.com/blog/archives/2007/10/28/working-with-php-5-in-mac-os-x-105/

share|improve this answer
    
Thank you so much for the awesome answer. Renaming the php.ini.default file worked, but I understand it much better thanks to this response. –  Jeff Sep 7 '10 at 20:11

If you don't want to muck about with config settings for either mysql or php, then set up this hack:

mkdir /var/mysql

ln -s /tmp/mysql.sock /var/mysql/mysql.sock

This creates an alias in the location that PHP is looking for that connects to the real mysql.sock.

share|improve this answer

Simply creating the /etc/my.cnf file with contents and creating the directory, per Vikash's recommendation, did not solve the problem. mysql.sock still was living in /tmp, even after doing this and restarting the machine. I manually moved the file from /tmp to /var/mysql, and then the error resolved. I had a feeling, though, that upon restart the problem would resurface, and it did. This time the error was slightly different - connection denied. I found that a new socket had been opened in /tmp and the old one was still there in /var/mysql; the code was trying to use the old one and getting denied access. So I used Sid's hack with the symlink and that fixed the problem. I'm sure when I restart, this fix will still work. However, I'd love to know what is causing the system to open new sockets in /tmp instead of the configured /var/mysql.

share|improve this answer

I did the following to resolve the issue (Mac OS x) -

1.) First go to your apache webroot (path available from /etc/apache2/httpd.conf -> DocumentRoot). For me it was /Library/WebServer/Documents/

1.1) Create a showphpconfig.php at that location

2.) Go and visit this file in your browser : http://127.0.0.1/showphpconfig.php

This will show you what configuration file your apache server is using

For me it was using /private/etc/php.ini

I also saw that php was using the mysql sock file from /var/mysql/mysql.sock

3.) I made a copy of the php.ini file. Open it and find all the lines which have mysql.sock.

copy those lines and create a new entry, like below -

;pdo_mysql.default_socket=/var/mysql/mysql.sock
pdo_mysql.default_socket=/tmp/mysql.sock

IMP the ";" in front of the first line, that is commenting out the first line
There should be about 4 such lines

Now you have told php, that the mysql socket file is actually present in the /tmp folder

4.) restart your apache server - apachectl restart

And you should see the wordpress configuration screen

share|improve this answer

Try just doing a symlink. For my XAMPP installation this worked:

sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/ /var/mysql
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.