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:


  • How to set up a LAMP stack in Debian containers using Docker Compose?
  • Build context folder in a remote registry?
  • Kafka with Docker Problems
  • on Docker
  • libGL error: failed to load driver: swrast - Running Ubuntu Docker container on Mac OS X host
  • com.spotify.docker.client.DockerRequestException: Request error: DELETE unix://localhost:80/v1.12/containers/…: 409
  •     version: '2'
            image: bitnami/mariadb
              - "3306:3306"
              MARIADB_DATABASE: 'mary_db'
              MARIADB_USER: user2
              MARIADB_PASSWORD: 'pass1234'
              build: ./consumer
                - mariadb

        import mysql.connector as mariadb1
        mariadb_connection = mariadb1.connect(host='mariadb',
        cursor = mariadb_connection.cursor(buffered=True)
            cursor.execute('DROP TABLE names')
        comm = "CREATE TABLE names(id VARCHAR(20), location VARCHAR(100)," \
       "PRIMARY KEY (mac,location)) ENGINE=InnoDB"
        print 'created'

    The Dockerfile entrypoint is just calling the function like:

        ENTRYPOINT python -u

    The problem is very randomly (more than %90 of trials), python code cannot connect with database and prints this error:

    File “”, line 7, in
    database=’mary_db’) File “/usr/local/lib/python2.7/dist-packages/mysql/connector/”,
    line 179, in connect
    return MySQLConnection(*args, **kwargs) File “/usr/local/lib/python2.7/dist-packages/mysql/connector/”,
    line 95, in init
    self.connect(**kwargs) File “/usr/local/lib/python2.7/dist-packages/mysql/connector/”,
    line 719, in connect
    self._open_connection() File “/usr/local/lib/python2.7/dist-packages/mysql/connector/”,
    line 206, in _open_connection
    self._socket.open_connection() File “/usr/local/lib/python2.7/dist-packages/mysql/connector/”,
    line 475, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err))) mysql.connector.errors.InterfaceError: 2003: Can’t connect to MySQL
    server on ‘mariadb:3306’ (111 Connection refused)

    Any idea what is going on?

  • Docker warning: failed to get default registry endpoint from daemon
  • docker stack deploy from private repo
  • Unable to properly connect to Redis in Kubernetes
  • ssh executing nsenter as remote command with interactive shell in golang to debug docker container
  • How to install php-redis extension using the official PHP Docker image approach?
  • run jenkins pipeline agent with sudo
  • One Solution collect form web for “Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python”

    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 ( container launches faster than database one.

    Possible solutions:

    docker healthcheck

    waittime in docker

    add a delay to the python consumer

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.