0

I am running a docker project with symfony 6 and PHP 8 and I am getting the error:

"An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory in . (which is being imported from "/app/config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type."

I'm trying to display GET request with api platform.

From this error I thought it might be the database connexion (mariadb database) but I checked and it was fine.

in my .env:

DATABASE_URL="mysql://admin@localhost:3307/databasetest"

the url of the symfony server with the swagger of api platform:

http://127.0.0.1:8000/api

I thought it was because the entity I used still had annotations instead of attribute despite being in symfony 6 so I used rector package to upgrade my entity file with attribute. I also added getter et setter to all properties.

I don't understand why the error is pointing toward api platform in the message?

I did run a similar project without docker and with a SQL database and it worked fine. I had this in its .env:

DATABASE_URL="mysql://root@127.0.0.1:3306/databasetest?serverVersion=8&charset=utf8mb4"

I tried adding the version at the end but that's not it:

DATABASE_URL="mysql://admin@localhost:3307/databasetest?serverVersion=10&charset=utf8mb4"

(at least the DATABASE_URL with the version added, displays http://127.0.0.1:8000/api, but the error "An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory" is still present) Any advice please?


edit: I thought I had the solution which is:

I am not sure when or why the problem was solved because I have tried several things in between but using the service name for the DATABASE_URL:

DATABASE_URL="mysql://admin@database:3306/databasetest" or DATABASE_URL="mysql://admin@database:3306/databasetest?serverVersion=10&charset=utf8"

and it works... I tried this at the beginning I think? So I'm not sure if the changes I tried really made it work

Thanks @Hans Kilian for pointing again the service name docker-compose

  • PHP instance seems not to communicate with db service

but no because I need to use:

DATABASE_URL="mysql://admin@database:3306/databasetest"

if I want to use api platform, but I need to use:

DATABASE_URL="mysql://admin@localhost:3307/databasetest"

if I want to change my data in the database, or use symfony commands such as "php bin/console make:migration" which result in error ""An exception occurred in the driver: SQLSTATE[HY000] [2002] No such file or directory" when doing API request on api platform

This is the docker-compose.yml:

services:
  database:
    ports:
      - '3307:3306'

So my final problem is to find out a DATABASE_URL which make both the api platform work and symfony commands work, if someone have any advise...

GrowyTobe
  • 115
  • 1
  • 8
  • 1
    if you are using docker then I will suggest to see networks settings if both db and application are at same network or can communicate with each other I also face that kind of issue too and result is messing with networks – M Maavia Dec 15 '22 at 05:36

1 Answers1

1

I suggest you to add a container_name to your database service, so you will be able to call it as a network in your .env file:

###> doctrine/doctrine-bundle ###
  database:
    container_name: "my_database"
    ...
    ...
###< doctrine/doctrine-bundle ###

then replace localhost by the container name in your .env file:

DATABASE_URL="mysql://admin@my_database:3307/databasetest"

Be aware that I don't know if this is the best solution, but it works fine for development environment.

Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70