Zend Framework error MySQL server through socket /tmp/mysql.sock

On a new apache/mysql installation, i got following error when connecting to mysql database in zend framework.

An error occurred
Application error
Exception information:

Message: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This is because no socket /tmp/mysql.sock exists. Can be fixed by symlink to actual socket.

To find mysql socket, in mysql command prompt run

mysql> show variables like '%socket%';
| Variable_name | Value                       |
| socket        | /var/run/mysqld/mysqld.sock | 
1 row in set (0.00 sec)


Now we got actual socket location. Now create a symlink with command

sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock

This will make it work, but a reboot will lose the changes, so permanent solution is to edit /etc/mysql/my.cnf

vi /etc/mysql/my.cnf 


socket          = /var/run/mysqld/mysqld.sock

Replace with (all occurrence, 3 times)

socket          = /tmp/mysql.sock

Posted in Programming