MariaDB / mariadb-docker

Docker Official Image packaging for MariaDB
https://mariadb.org
GNU General Public License v2.0
770 stars 438 forks source link

cant connect to DB from another container #392

Closed mrjoli021 closed 3 years ago

mrjoli021 commented 3 years ago

I am running MariaDB 10.5 with the following compose file. If I connect to the cli of the DB container I am able to connect to the db with the user. But the Python container is not able to. I keep getting the following error messages on the console of the MariadB container.

[Warning] Aborted connection 11 to db: 'db' user: 'db_user' host: '172.29.0.3' (Got an error reading communication packets).

---
version: "3.7"
services:

  mariadb:
    image: mariadb:10.5
    container_name: mariadb_api
    restart: unless-stopped
    volumes:
      - ./DATA/mariadb_api/:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: rootPassword
      MYSQL_DATABASE: db
      MYSQL_USER: db_user
      MYSQL_PASSWORD: dbPassword

    healthcheck:
      test: "/usr/bin/mysql --user=root --password=rootPassword --execute \"SHOW DATABASES;\""
      interval: 2s
      timeout: 20s
      retries: 10

  python:
    depends_on:
    - mariadb
    build:
      context: ./App
    container_name: python3.9_api
    restart: unless-stopped
    #volumes:
    #- ./DATA/python3.9/:/app
grooverdan commented 3 years ago

try --protocol tcp in your mysql command in your healthcheck. I think its falsely reporting active during the initialization stages where the server does start up (on unix socket only, no tcp, hence using it in the healthcheck).

grooverdan commented 3 years ago

Also check the logs of the python container, especially after changing the healthcheck. If the mariadb container is definitely running, then a stack track of the python connector used may help resolve it having some protocol error with MariaDB.

Its not a connection refused so that a good sign the containers are connected.

mrjoli021 commented 3 years ago

Changed it to TCP and now it is connecting, but I am still getting these errors. Not as frequent as before, before is was just filling up the log constantly. Now I am getting them one every few min.

grooverdan commented 3 years ago

Can you show part of the python backtrace when this occurs? Which python connector and version are you using?

mrjoli021 commented 3 years ago

I am new to Alchemy. So I am literally just copying and pasting the code from their intro site, just with user/pass https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-1/

I am not getting any errors on the Python container and it prints the info correctly. The only errors that show up is on the DB container and again they are showing up once every 10-15 min apart. If I remove the TCP option, I start getting the errors constantly and my python is still running now.

grooverdan commented 3 years ago

for comparison pip3 install PyMySQL and use mariadb+pymysql:// as the beginning of the url.

mrjoli021 commented 3 years ago

After I changed it to pymsql I have not gotten any more errors and my app is working correctly now. Thanks

grooverdan commented 3 years ago

I suspect the error message was an idle pool connection reaching its idle_timeout. Provided it was truly an idle connection not being used by you application there probably wasn't an error as such. I'm not sure why it didn't cleanly shutdown the connection.

It could also be timing out at the network tcp layer if you are traversing a stateful firewall.

If you do go back to using MariaDB Connector/Python you can create bug reports on JIRA on the CONPY project.

Welcome to MariaDB and Python programming.