简体   繁体   中英

unable to access mysql db using python (both running on different containers)

I am trying to connect to mysql db using a python program. When run locally it works.

But while dockerizing the application I created, one container is for the python code and the other for the mysql db, when ran i this manner it fails to connect.

Python_code:

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

docker-compose:

version: "3"
  services:
    app:
      image: app:latest
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And the docker-compose up fails with the eroor:

pony.orm.dbapiprovider.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")

Where am I going wrong? Please advise!

Try using container port 3306 -

db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)

Also, add depends_on attribute, you can remove the links attribute -

 depends_on:
    - db

YOu can use depends_on flag as mentioned in accepted answer. If it does not solve your problem them use this approach.

After starting the container, your server will try to connect to the database server. sometimes database server may take some time to boot up and in this window, if the server tries to connect to database server it will face problems. Try adding logic to reconnect database server after few seconds if the connection fails.

try{
   connectToDatabase();
   }catch(error){
   waitForHalfMinute();
   connectToDatabase();
}

I would recommend you to exit your application in case it cannot connect to MySQL and set the restart policy to always, because depends_on does not guarantee that MySQL will be totally up when app starts but it is good to have it there.

version: "3"
  services:
    app:
      image: app:latest
      restart: always
      links:
        - db
      ports:
        - "8001:8081"
      environment:
        - DB_HOST= db
      depends_on:
        - db

    db:
      image: mysql:5.7.26
      restart: always
      environment:
        MYSQL_DATABASE: 'my_db'
        MYSQL_USER: 'docker_root'
        MYSQL_PASSWORD: 'password'
        MYSQL_ROOT_PASSWORD: 'password'
      ports:
        - "3306:3306"
      volumes:
        - ./DB_config/:/etc/mysql/mysql.conf.d

And your application code should be something like:

try:
    db.bind(provider='mysql', user='docker_root', password='password', host='db', database=database, port = 3306)
except:
    # write some logs
    exit(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM