Symfony 4 Doctrine not working from console [2002] No such file or directory

烂漫一生 提交于 2019-12-05 18:32:55
Jonathan KABLAN

Please change DB_HOST=localhost to DB_HOST=127.0.0.1

> doctrine/doctrine-bundle

DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}

So i suppose that you showed the whole content of your doctrine.yml.

If so, you might want to consider changing your doctrine.yml as follows: add an additional line in the dbal: section

doctrine:
    dbal:
       driver: 'pdo_mysql'
       server_version: '5.7'
       charset: utf8mb4
       url: '%env(resolve:DATABASE_URL)%'

Watch the last line, beginning with url:, closely.

The 'resolve:" operator looks up DATABASE_URL in your env-Variables and if it is found, it then replaces the expression with its content.

This is a doctrine.yml, that gets auto-generated by symfony.

The 'resolve' operator migth not be needed though. This could potentially work too:

doctrine:
    dbal:
       driver: 'pdo_mysql'
       server_version: '5.7'
       charset: utf8mb4
       url: '%env(DATABASE_URL)%'

This method is proposed in the symfony docs, which might be interesting to read, to understand further configuration of symfony.

My fix is here:

export DATABASE_URL=<value from .env>

And generating SQLs works :)

config/packages/doctrine.yaml

I solved this error adding my database url in the "config/packages/doctrine.yaml" file the "parameters" > "env(DATABASE_URL)" field:

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): 'mysql://your_db_url'

I solved this change in .env file

DATABASE_URL=mysql://root:root@127.0.0.1:3306/test 

to

DATABASE_URL="mysql://root:root@127.0.0.1:3306/test"

I added quotes. Maybe you are using windows? It works for me.

like in Symfony docs: Environment Variables

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