如何通过symfony4连接到用mamp创建的mysql数据库?

0md85ypi  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(297)

我在mamp中创建了一个名为“project”的数据库。
在我的 .env 文件我添加了这行:

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

现在我想跑了

php bin/console doctrine:database:create

但我收到一条错误信息:
sqlstate[hy000][2002]没有这样的文件或目录
我的条令配置:

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
pbpqsu0x

pbpqsu0x1#

您还可以尝试明确地设置mysql服务器

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
q9rjltbz

q9rjltbz2#

解决了!
在文件“config/packages/document.yaml”中,我必须添加这一行
unix\u socket:/applications/mamp/tmp/mysql/mysql.sock
这意味着改变这一点:

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

进入之内

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

w7t8yxp53#

我也有同样的问题。添加了unix\u套接字,并将127.0.0.1更改为localhost works on mamp 5.5。
条令.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)%'

.环境

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

6ie5vjzr4#

尝试更改:

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

对此:

url: '%env(DATABASE_URL)%'

相关问题