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.
[Doctrine\DBAL\Exception\ConnectionException]                                     
  An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory 

[Doctrine\DBAL\Driver\PDOException]               
  SQLSTATE[HY000] [2002] No such file or directory

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

Solution:
Open parameters.yml and check for host. If it is stand localhost then replace host as follows:
 'host'=> 'localhost' to 'host'=> '127.0.0.1'
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
doctrine:
    dbal:
        default_connection: default
        connections:
            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
http://andreys.info/blog/2007-11-07/configuring-terminal-to-work-with-mamp-mysql-on-leopard

1 comment:

  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?

    ReplyDelete