How can I connect via Symfony 4 to mySQL database created with MAMP?

17,184

Solution 1

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)%'

Solution 2

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

Solution 3

Try to change this:

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

to this:

url: '%env(DATABASE_URL)%'

Solution 4

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
Share:
17,184

Related videos on Youtube

peace_love
Author by

peace_love

Sometimes you win & sometimes you learn

Updated on September 22, 2022

Comments

  • peace_love
    peace_love over 1 year

    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
    
  • Rufinus
    Rufinus almost 6 years
    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.
  • peace_love
    peace_love almost 6 years
    @Rufinus I tied to use 127.0.0.1 instead of localhost but then I got the error "Connection failed"
  • thi3rry
    thi3rry over 3 years
    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_so‌​cket=/Applications/M‌​AMP/tmp/mysql/mysql.‌​sock
  • devXen
    devXen about 2 years
    this worked for me