1

this is my docker-compose-yml file. first i tried to run the db service it work fined and docker running successfully. but when I run the app service, in the terminal it says connected to database but I get an error as (2003, "Can't connect to MySQL server on '127.0.0.1')

 version: '4'
    
    services:
      app:
        build: .
        ports:
          - "8000:8000"
        env_file:
          - .env
        depends_on:
          - db
        links:
          - db  
      db:
        environment:
              - MYSQL_ROOT_PASSWORD=root
              -
        image: mysql
        ports:
          - "3307:3307"
        env_file:
          - .env

enter image description here

3
  • The default port of MySQL is 3306. Probably, you might need to map the ports like this - "3307:3306" Commented Aug 21, 2021 at 16:09
  • I tried, same error Commented Aug 21, 2021 at 16:13
  • provide your DATABASE_URL it should be like this: DATABASE_URL = "mysql+pymysql://user:password@db/mydatabase" Commented Jul 17, 2024 at 22:33

1 Answer 1

0

What environment variable is the FastAPI app using to connect to the MySQL host?

If you're using docker-compose networking, services need to use the container name in order to reach each other (i.e. mysql://db:3307/db_name_here) not localhost.

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

More information on networking can be found in their docs here.

Also worth noting, since you're using links you can set aliases as well like so:

version: "3.9"
services:

  web:
    build: .
    links:
      - "db:database"
  db:
    image: postgres

Links Docs Source

Sign up to request clarification or add additional context in comments.

7 Comments

mysql://db:3307/db_name_here what do you mean db in here? i have mentioned the same string. i got an different error
How are you trying to connect to MySQL inside your FastAPI app? Can you show the env vars and/or config being used? It's hard to tell what's going wrong without that. What I meant was when connecting to MySQL inside a docker-compose you need to use the container name as the hostname for internal DNS
fastapi database connection-> SQLALCHEMY_DATABASE_URL=mysql+pymysql://'+USER_NAME+':'+PASSWORD+'@'+HOST+'/'+DB env vars HOST=127.0.0.1 PORT=3307 USER_NAME=root PASSWORD=root DB=eventDb this is how it has been connected inside fastapi. I understand docker not working because I'm using localhost. how to overcome this. highly appreciatable for your valuable time
try changing HOST to db which is the container name for your MySQL DB based on your docker-compose.yaml
it saying as unknown database
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.