Cannot connect to Docker mySQL instance from Tortoise/Python, until I first connect to mySQL instance from Docker PHPMyAdmin

Viewed 108

The 'rest' service described in the following docker-compose.yaml runs a python script, on launch, that attempts to connect to the 'db' services's mysql instance.

Each time I run docker-compose up, all services start up successfully, however the 'rest' service reports 'Unable to connect to mySQL db', continiously in the debug console.

A very odd fix for the issue seems to be opening phpMyAdmin, and logging in to the mySQL instance via it, first. Once I have logged in to the mySQL db from phpMyAdmin, I check back to the docker-compose console, and I see that the 'rest' python script has successfully established its db connection.

I have verified the behaviour over and over, but am at a loss as to why the mySQL instance requires me to log in with phpMyAdmin, first, before the python script can establish it's own connection...

My python/tortoise connection codeis as follows:

DATABASE_URL = "mysql://{}:{}@{}:{}/{}".format(
    os.environ["MYSQL_USER"],
    os.environ["MYSQL_PASSWORD"],
    os.environ["MYSQL_HOST"],
    os.environ["MYSQL_PORT"],
    os.environ["MYSQL_DATABASE"],
)

register_tortoise(
    app, db_url=DATABASE_URL, modules={"models": ["models"]}, generate_schemas=False
)

Docker-compose.yaml


services:
    db:
        image: mysql:latest
        restart: always
        environment:
            MYSQL_DATABASE: "bbms"
            MYSQL_USER: "bbms"
            MYSQL_PASSWORD: "B37PTdaWvbUQm"
            MYSQL_ROOT_PASSWORD: "asdasdasdasd"
        ports:
          - "3306:3306"
        volumes:
          - dbdata:/var/lib/mysql

    phpmyadmin:
        image: phpmyadmin:latest
        depends_on: [ db ]
        ports:
          - "8080:80"
        environment:
          - PMA_HOST=db
          - PMA_PORT=3306
        restart: unless-stopped

    rest:
        build:
          context: ./
          dockerfile: ./docker/rest/Dockerfile
        restart: always
        depends_on: [ db ]
        environment:
            MYSQL_DATABASE: "bbms"
            MYSQL_USER: "bbms"
            MYSQL_PASSWORD: "B37PTdaWvbUQm"
            MYSQL_HOST: db
            MYSQL_PORT: 3306
        volumes:
          - ./src:/app
        ports:
          - "5000:5000"

networks:
    default:
      name: qibbms_net
      ipam:
          driver: default
          config:
              - subnet: "172.12.2.0/24"
volumes:
    dbdata:
1 Answers

I had the same problem with my application. When I checked the exception stacktrace I found this line of code:

'cryptography' package is required for sha256_password

So I installed the cryptography package:

pip install cryptography

And my problem was resolved.

Related