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

…衆ロ難τιáo~ 提交于 2020-01-23 10:09:32

问题


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

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



回答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



回答3:


Try to change this:

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

to this:

url: '%env(DATABASE_URL)%'



回答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


来源:https://stackoverflow.com/questions/50907667/how-can-i-connect-via-symfony-4-to-mysql-database-created-with-mamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!