5

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?

7 Answers7

7

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.

Daniel Vandersluis
  • 481
  • 1
  • 6
  • 17
  • 1
    This was the one that fixed it. Then I read Vikash's answer to see why. Thanks so much! –  Sep 07 '10 at 19:53
5

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.

Sid Ahuja
  • 51
  • 1
  • 1
1

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/

Vikash
  • 111
  • 4
  • 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. –  Sep 07 '10 at 20:11
1

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.

barclay
  • 111
  • 3
1

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

PlanetUnknown
  • 149
  • 1
  • 7
1

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

sudo ln -s /Applications/XAMPP/xamppfiles/var/mysql/ /var/mysql
rantoniuk
  • 131
  • 3
0

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

Lekensteyn
  • 6,111
  • 6
  • 37
  • 55
  • 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 –  Sep 07 '10 at 19:46