I am newly learning Docker and compose, I have searched and watched many tutorials but could not understand what is causing this connection error:
_mysql_connector.MySQLInterfaceError: Can't connect to MySQL server on 'localhost:3306' (99)
my file structure: docker-compose.yml .env mysql/ database.sql Dockerfile python/ server.py Dockerfile
docker-compose.yml:
version: '3.8'
services:
mysql:
build:
context: ./mysql
dockerfile: Dockerfile
restart: always
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- '3306:3306'
pythonapp:
build:
context: ./python
dockerfile: Dockerfile
depends_on:
- mysql
ports:
- '8080:8080'
python/Dockerfile:
FROM python:3.11-slim
RUN pip install mysql-connector-python && \
pip install python-dotenv
COPY . /app
WORKDIR /app
CMD ["python", "-u", "server.py"]
EXPOSE 8080
mysql/Dockerfile:
FROM mysql:latest
COPY ./database.sql /docker-entrypoint-initdb.d/
python/server.py:
from http.server import HTTPServer, BaseHTTPRequestHandler
from dotenv import load_dotenv
import mysql.connector
import json
import os
load_dotenv('../.env')
# Establish connection to MySQL
db_connection = mysql.connector.connect(
user=os.getenv('MYSQL_USER'),
password=os.getenv('MYSQL_ROOT_PASSWORD'),
host=os.getenv('_HOST'),
port=3306,
database=os.getenv('MYSQL_DATABASE'))
print("DB connected")
...
.env:
_HOST=localhost
MYSQL_DATABASE=db
MYSQL_USER=root
MYSQL_ROOT_PASSWORD=password
found this: PyMySQL can't connect to MySQL on localhost and:
I have tried _HOST=127.0.0.1, _HOST=mysql, _HOST=host.docker.internal but the error did not change. Also tried to compose for second time to solve connection maybe because the database was not ready for connection, but getting the same error. So the MySQL database is running but the python server does not connect.