2
votes

I am trying the connect mariadb with python using docker-compose:

docker-compose.yml

    version: '2'
    services:
      mariadb:
        image: bitnami/mariadb
        ports:
          - "3306:3306"
        environment:
          MARIADB_DATABASE: 'mary_db'
          MARIADB_USER: user2
          MARIADB_PASSWORD: 'pass1234'
      consumer:
          build: ./consumer
          links:
            - mariadb

consumer.py

    import mysql.connector as mariadb1

    mariadb_connection = mariadb1.connect(host='mariadb',
                                  port=3306,
                                 user='user2',
                                 password='pass1234',
                                 database='mary_db')

    cursor = mariadb_connection.cursor(buffered=True)

    try:
        cursor.execute('DROP TABLE names')
    except:
        pass

    comm = "CREATE TABLE names(id VARCHAR(20), location VARCHAR(100)," \
   "PRIMARY KEY (mac,location)) ENGINE=InnoDB"

    cursor.execute(comm)
    print 'created'

The Dockerfile entrypoint is just calling the consumer.py function like:

    ENTRYPOINT python -u consumer.py

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

File "consumer.py", line 7, in database='mary_db') File "/usr/local/lib/python2.7/dist-packages/mysql/connector/init.py", line 179, in connect return MySQLConnection(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 95, in init self.connect(**kwargs) File "/usr/local/lib/python2.7/dist-packages/mysql/connector/abstracts.py", line 719, in connect self._open_connection() File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 206, in _open_connection self._socket.open_connection() File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", 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?

2
Since this is a mysql or mariadb error message, it is irrelevant whether python or a gui application receives the error message, hence the duplicate.Shadow
@Shadow I don't think its a duplicate; The question is in Docker and setup is different.salehinejad
It is irrelevant, since the error is not related to docker. The client tries to talk to the mysql / mariadb server the way you configured it to, but there is no answer. The possible reasons are all discussed in the duplicate topic.Shadow
It was the docker problem.salehinejad

2 Answers

1
votes

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

0
votes

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 time
time.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.