skip to Main Content

I’m trying to run a FastAPI application with a MySQL database in a Docker environment, but I’m encountering a connection error. The application is unable to connect to the database.
Here’s the error I’m getting:

Copysqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name or service not known)")
(Background on this error at: https://sqlalche.me/e/20/e3q8)

docker-compose.yml:

services:
  backend:
    build: .
    ports:
      - "8001:8000"
    networks:
      - music-app
    volumes:
      - .:/app
    env_file:
      - .env
    environment:
      - DATABASE_URL=${BACKEND_DATABASE_URL}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    depends_on:
      - db
    command: bash -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload"

  db:
    image: mysql:8.0
    volumes:
      - mysql_data:/var/lib/mysql
    env_file:
      - .env
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
    ports:
      - "3307:3306"
    networks:
      - music-app
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "mysqladmin ping -h 127.0.0.1 -u root -p$$MYSQL_ROOT_PASSWORD",
        ]
      timeout: 20s
      retries: 10

volumes:
  mysql_data:

networks:
  music-app:
    driver: bridge

In my alembic/env.py, I’m trying to connect to the database using mysql+pymysql as the dialect.
I’ve already tried:

Checking environment variables
Verifying network settings
Setting up a healthcheck for the database

What could be causing this issue, and how can I resolve it?

I’ve tried the following

Checked environment variables:

Verified that the BACKEND_DATABASE_URL in the .env file is correctly set with the appropriate
format: mysql://:[password]@[host]:[port]/[database_name].

Verified network settings:

Confirmed both backend and db services are on the same music-app network in docker-compose.yml.
Expected: Services to be able to communicate with each other.

Despite these attempts, I’m still unable to establish a connection between the FastAPI backend and the MySQL database. I expected the application to start successfully and be able to communicate with the database.

2

Answers


  1. In order to let your backend container connect to db container, you need to expose the port.

        db:
          expose:
            - "3306"
    ... 
    
    
    Login or Signup to reply.
  2. remove one $ from your db health check -p$$MYSQL_ROOT_PASSWORD for healthcheck to pass which will ease if your db is accessible or not
    your:

    "mysqladmin ping -h 127.0.0.1 -u root -p$$MYSQL_ROOT_PASSWORD", 
    

    new:

    "mysqladmin ping -h 127.0.0.1 -u root -p$MYSQL_ROOT_PASSWORD",
    

    change your BACKEND_DATABASE_URL value in .env
    from

    mysql://root:mysecretpassword@db:3306/mydatabase
    
    

    to:

    mysql+pymysql://root:mysecretpassword@db:3306/mydatabase
    
    

    because you are using sqlalchemy as seen in the error therefor you will need pymsql as api driver to access mysql

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search