Skip to content Skip to sidebar Skip to footer

Mariadb Docker Container Can't Connect To Mysql Server On Host (111 Connection Refused) With Python

I am trying the connect mariadb with python using docker-compose: docker-compose.yml version: '2' services: mariadb: image: bitnami/mariadb ports:

Solution 1:

The problem is not with Database or Python code; Its a raise problem coming from Docker, as database needs some time to load and python (consumer.py) container launches faster than database one.

Possible solutions:

docker healthcheck

waittime in docker

add a delay to the python consumer

Solution 2:

This problem is due to python containers executing prior then properly executing the database container. To remove this issue use below lines of code before mysql connection.

import timetime.sleep(1)

Your code should look like this:

import time
time.sleep(1)

mydb = mysql.connector.connect(
  host="<host>",
  user="<user_name>",
  passwd="<pswd>",
  database = "<DB_name>",
  buffered=True
)

cursor= mydb.cursor()

It worked in my case. Hope it will work for you as well.

Post a Comment for "Mariadb Docker Container Can't Connect To Mysql Server On Host (111 Connection Refused) With Python"