Home > database >  Docker error : MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL
Docker error : MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL

Time:04-19

I'm trying to start the server through docker-compose up

I'm get an error:

MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

But I'm able to connect to mysql with the below command

 docker exec -it containername  mysql -uroot -p

docker-compose.yml

version: '3.1'
services: 
db:
  image: mysql
  command: --default-authentication-plugin=mysql_native_password
  volumes:
   - ./db:/docker-entrypoint-initdb.d/:ro    
restart: always
environment:
  MYSQL_ROOT_PASSWORD: ****
  MYSQL_DATABASE: emp
  MYSQL_PASSWORD: ****
ports:
 - "33061:3306" 
emp:
  build: .
  restart: always
  ports:
  - 5005:5000    
volumes:
   my-db:

dockerfile

FROM python:3.6-buster
LABEL image for a very management application
# We copy just the requirements.txt first to leverage Docker cache
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN apt-get install -y default-libmysqlclient-dev
RUN pip3 install -r requirements.txt
RUN touch /var/log/cron.log

COPY crontab /etc/cron.d/cjob
RUN chmod 0644 /etc/cron.d/cjob
ENV PYTHONUNBUFFERED 1
CMD cron -f
EXPOSE 5000
COPY . .
CMD [ "python3", "app.py" ]

DE Config:

from flask_mysqldb import MySQL,MySQLdb

app = Flask(__name__)        
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = '*****'
app.config['MYSQL_PASSWORD'] = '****'
app.config['MYSQL_DB'] = 'emp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app) 

Can anyone please help me how to solve this problem. Thank you all

CodePudding user response:

app.config['MYSQL_HOST'] = 'db'

  • Related