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!
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:
If 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