Neil Ang

Developer

A stunning likeness of Neil Ang
Hello world

Sockets problem with MySQL/PHP and Mac OS X

Posted on

After installing MySQL 5 on OS X 10.4, you may have noticed that the install package has placed 'mysql.sock' in '/tmp/mysql.sock' instead of '/var/mysql/mysql.sock'. This will cause the following error when you attempt to access MySQL through PHP:

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)

A simple solution is to place a symbolic link between the expected folder and the install directory. This can be achieved with the following terminal commands:

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

To test a database connection between PHP and MySQL, you can use the following script:

<?php
   $dbServer='localhost';
   $dbUser='root';
   $dbPass='root';
   $dbName='test';
   $link = mysql_connect("$dbServer", "$dbUser", "$dbPass") or die ('Could not connect.');

   echo 'Connected successfully<br />';
   mysql_select_db("$dbName") or die('Could not select database.');
   echo 'Database selected successfully<br />';
   mysql_close($link);