Problem
Running PHPUnit on a Mac running Zend Server produces the following error message:
ERROR 1049 (42000): Unknown database 'databases;' mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)' Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
Solution
Before we proceed with the solution, let’s ensure that our user account is in the wheel. To check this, bring up your terminal and type the following command:
dscl . -read /Groups/wheel GroupMembership
The output should display something like this:
GroupMembership: rootIf your username doesn’t appear next to root.. let’s add it.
dscl . -append /Groups/wheel GroupMembership YOUR_USER_ACCOUNT
Replace YOUR_USER_ACCOUNT with your username.
Zend Server places the mysql.sock file in a location that the rest of the system doesn’t know about. The rest of the system is looking for /tmp/mysql.sock however Zend places the mysql.sock file in /usr/local/zend/mysql/tmp/mysql.sock.
To solve this, we need to create a symlink for mysql.sock in the /tmp directory.
sudo ln -s /usr/local/zend/mysql/tmp/mysql.sock /tmp/mysql.sock
Another thing to keep in mind is that the my.cnf file is also located in a special Zend directory. The rest of the system is going to look for the my.cnf file in the /etc directory. Let’s go ahead and create that sym link also.
sudo ln -s /usr/local/zend/mysql/data/my.cnf /etc/my.cnf
PDO MYSQL Users
If you are using pdo_mysql, you will still receive the /tmp/mysql.sock not found issues everytime you reboot your machine, it’s best to simply pass the socket information with each connection.
For Zend Users:
$params = array( 'host' => 'localhost', 'username' => '***', 'password' => '***', 'dbname' => '***', 'unix_socket' => '/var/run/mysqld/mysqld.sock', 'driver_options' => $pdoParams ); $db = Zend_Db::factory('Pdo_Mysql', $params);
Non Zend Users:
$dbc = new PDO( 'mysql:host=localhost;dbname=test;unix_socket=/var/run/mysqld/mysqld.sock', 'user', 'passwd' );
Update – April 20th 2011
I have received numerous emails about the sym linking option in this article. This is what I have done to circumvent that.
You will need to locate your my.cnf which is the mysql configuration file and add the following entries:
Filename: my.cnf [client] #... entries removed socket = /usr/local/zend/mysql/tmp/mysql.sock [mysqld] #... entries removed socket = /usr/local/zend/mysql/tmp/mysql.sock

4 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
I have the weirdest thing. My symlink for the mysql.sock disappears when I restart my Macbook. Do you have any idea what would be the cause of that? Thanks in advance!
Check to make sure that you have a socket entry in your my.cnf, another option is to pass the unix_socket when you create your MySQL connection.
For example:
If you are using zend framework you can add a db param:
Is there any option for adding the symlink to bash_profile?, so that it will be automatically gets created when Macbook starts, avoiding manual execution of the command.
@Danie, please review the updated article.