Tuesday, October 13, 2015

How To Fix Doctrine PDOException No such file or directory in Symfony?

I had issue when I tried to migrate the DB schema via command line.
php app/console doctrine:migrations:migrate --env=test
I got following errors.
  An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory 

  SQLSTATE[HY000] [2002] No such file or directory

  SQLSTATE[HY000] [2002] No such file or directory 

Open parameters.yml and check for host. If it is stand localhost then replace host as follows:
 'host'=> 'localhost' to 'host'=> ''
If it didn't solve your problem then do following steps:
Assuming Mysql database settings are correctly inserted in the config/parameters.yml file.
And open the Doctrine configuration in config/config.yml and add the following line
unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock after password as below.
# Doctrine Configuration
        default_connection: default
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                unix_socket: "/Applications/MAMP/tmp/mysql/mysql.sock"
                charset:  UTF8
This solved my problem.
The problem is the mysql.default_socket setting in PHP.INI defaults to a different location than where MySQL actually puts that file.
Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock.
mkdir /var/mysql
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock

Ref: http://stackoverflow.com/a/26436016


  1. Hi there, I don't quite understand. Why are you adding a directory with MAMP in it? With Symfony you can connect directly via php bin/console server:run, so what role would MAMP play?

  2. Thank you very much !
    It's work perfectly.