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

6

6

I´m working with symfony 4 and this error ocurr when runnning doctrine console commands:

In AbstractMySQLDriver.php line 108:

An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory  

I think this is because there´s something wrong with the connection but when I run the application it has access to the database without errors, so I can not imagine what is wrong with the connection.

I was able to continue working by creating manually the database and using

php bin/console doctrine:schema:create --dump-sql

But this is really unwanted because later when the schema needs to be updated all data would be lost because i would need to re-create the schema instead of updating it.

The app works normally after executing manually the SQL (easyadmin works fine).

Here are my configurations:

.ENV:

DATABASE_URL=mysql://root:[email protected]:3306/test

doctrine.yaml:

parameters:
     env(DATABASE_URL): ''

doctrine:
    dbal:
       driver: 'pdo_mysql'
       server_version: '5.7'
       charset: utf8mb4

I tried the drop command just to test and it throwed this:

In DropDatabaseDoctrineCommand.php line 93:


Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.  

So I add dbname to doctrine.yaml and didn´t worked. I also tried removing the cache, creating a new project, using a remote mysql, and using sqlite but the errors are the same in both cases !!

Thanks in advance for the help !

Jesuit answered 7/1, 2018 at 8:25 Comment(1)
I "SOLVED" it by luck when i added the database url in the doctrine.yaml in parameters. But when the file is created the line is commented with "You should not need to change this value." I think this is kind of dirty, but it works.Jesuit
A
4

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}
Ashore answered 20/10, 2018 at 18:43 Comment(0)
E
1

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.

Eloisaeloise answered 7/1, 2018 at 9:56 Comment(1)
The resolve operator is not needed indeed. But the error keeps throwing, I´ve been working around with this error and it seems that when running commands from the console the .env variables are not loaded, i think that must be the problem. I don`t know if maybe there are some docs where it shows how the .env works when running bin/console commands.Jesuit
T
1

My fix is here:

export DATABASE_URL=<value from .env>

And generating SQLs works :)

Tiller answered 22/1, 2018 at 19:39 Comment(0)
T
0

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'
Tare answered 9/3, 2018 at 21:24 Comment(0)
A
0

I solved this change in .env file

DATABASE_URL=mysql://root:[email protected]:3306/test 

to

DATABASE_URL="mysql://root:[email protected]:3306/test"

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

like in Symfony docs: Environment Variables

Arruda answered 20/3, 2018 at 7:32 Comment(0)
M
0

Hi On my side I add the exact same behaviour, on symfony 4, I just changed this

DATABASE_URL=mysql://root:root@localhost:8889/

to this

DATABASE_URL=mysql://root:[email protected]:8889

on my .env or .env.dev file (depend on the env you are working on) hope it helps.

Muricate answered 29/4, 2020 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.