I'm trying to connect my application to my mysql database which I've got up and running in a docker-compose file. I'm using flask and trying to connect using DBUtils I keep getting the error message described in my title:
(pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 8] nodename nor servname provided, or not known)"))
I've tried using the IPAddresses of my docker instances as well as several other solutions in similar problems discussed here on StackOverflow: Docker-Compose can't connect to MySQL
Connecting to MySQL from Flask Application using docker-compose.
however, the offered solutions don't seem to be working for me.
my docker-compose file looks as follows:
version: '3.3' services: db: image: mysql:8.0 container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: 'myPassword' MYSQL_DATABASE: 'databaseName' volumes: - .:/dockerFiles ports: - "3306:3306" expose: - "3306" phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin restart: always ports: - "8080:80" volumes: - /sessions and my connect string looks as follows:
def connect_db(): # Connects to the database and takes care of the connection return PersistentDB( creator=pymysql, host='db', user='root', password='myPassword', database='databaseName', port=3306, autocommit=True, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)