This is the personal blog of Neil Ang. Simple and interesting technology articles written by a developer for developers. Feel free to comment on posts or link to this site. Constructive feedback is always welcomed.

Sockets problem with MySQL/PHP and Mac OS X

Posted: 21 December 2006

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);
?>

Your comments

No comments have been made. Why not be the first?

Post a comment

Comment Guidelines

  • Have no more than 2 links, otherwise your comment will be flagged as spam.
  • Links are automagically generated.
  • <em>text</em> to make text italic.
  • <strong>text</strong> to make text bold.

JavaScript needs to be enabled to comment.