4

I created a database in MAMP called "project".

In my .env file I added this line:

DATABASE_URL=mysql://root:root@localhost:3306/project

Now I want to run

php bin/console doctrine:database:create

But I get an error message:

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

my doctrine configurations:

parameters:
    # Adds a fallback DATABASE_URL if the env var is not set.
    # This allows you to run cache:warmup even if your
    # environment variables are not available yet.
    # You should not need to change this value.
    env(DATABASE_URL): ''

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
5
  • Can you show please the doctrine configuration? Commented Jun 18, 2018 at 10:43
  • @AlessandroMinoccheri Where do I find it? Commented Jun 18, 2018 at 10:44
  • you mean config/packages/doctrine.yaml? Commented Jun 18, 2018 at 10:44
  • yes exactly please add it Commented Jun 18, 2018 at 10:45
  • @AlessandroMinoccheri Added it Commented Jun 18, 2018 at 10:46

4 Answers 4

9

Solved it!

in the file "config/packages/doctrine.yaml" I had to add this line

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

This means change this:

 dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'

into

 dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        url: '%env(resolve:DATABASE_URL)%'
Sign up to request clarification or add additional context in comments.

3 Comments

I can give you some background on your error: if you use localhost it has special meaning and mysql-client lib tries to connect via socket. as your socket is not in one of the default locations you have to give dbal the exact location. You also can change localhost to 127.0.0.1 to enforce a TCP connection, which would not use the socket.
@Rufinus I tied to use 127.0.0.1 instead of localhost but then I got the error "Connection failed"
You can also put a unix_socket param in the DATABASE_URL of your environment file e.g. .env.local : DATABASE_URL=mysql://root:root@localhost:3306/dbname?unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock
6

Got the same problem. Added the unix_socket and changed 127.0.0.1 to localhost works on MAMP 5.5.

doctrine.yaml

doctrine:
dbal:
    # configure these for your database server
    driver: 'pdo_mysql'
    server_version: '5.7'
    charset: utf8mb4
    unix_socket: /Applications/MAMP/tmp/mysql/mysql.sock
    default_table_options:
        charset: utf8mb4
        collate: utf8mb4_unicode_ci

    url: '%env(resolve:DATABASE_URL)%'

.env

DATABASE_URL=mysql://root:root@localhost:3306/mydb

1 Comment

this worked for me
1

Try to change this:

url: '%env(resolve:DATABASE_URL)%'

to this:

url: '%env(DATABASE_URL)%'

Comments

1

What you can also try is setting the mysql server expliclitly

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                dbname:         local_api
                user:           root
                password:       null
                host:           localhost
                driver:         pdo_mysql
                server_version: '5.5' # in case you are using mysql 5.5

1 Comment

I have the same problem today. It's 5 years later after this question date. Terrible! Why do maintainers can't make this setting easier?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.