Resolve a path error in Symfony2 when using MAMP under OSX Lion

Resolve a path error in Symfony2 when using MAMP under OSX Lion

Resolve a path error in Symfony2 when using MAMP under OSX Lion 1920 1091 Border Crossing UX

You have installed and configured your Symfony2 project, added the Entity classes, prepared everything for persisting them into a database and, bam, you end up with the following error message after executing “php app/console doctrine:database:create” the command prompt:

Could not create database for connection named <your_database_name>
SQLSTATE[XXXXX] [YTYYY] No such file or directory

The reason this is happening is because you are using MAMP’s MySQL server. The location of its unix socket tmp file (/Applications/MAMP/tmp/mysql/mysql.sock) is different from the default location (/var/mysql/mysql.sock or /tmp/mysql.sock) of the MySQL unix socket file of an OSX installation. By default, Symfony2 assumes it should use OSX’s own MySQL server and all of the files associated with it. Therefore, it cannot find the correct files in the places it would normally look.

To fix the issue, edit the file:

symfony2_project/app/config/config.yml

and add the following line:

unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock

inside the dbal: section make the code look like:

doctrine:
    dbal:
        driver: %database_driver%
        host: %database_host%
        port: %database_port%
        dbname: %database_name%
        user: %database_user%
        password: %database_password%
        charset: UTF8
        unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
...

This overwrites the default location of the mysql.sock file and lets Symfony2 know where to find MAMP’s mysql.sock file.

You might need to adapt the examples above to suit your installation in case it uses different paths but this example should help you on your way.

Back to top