0
  • I used mysql connector in my django web app.
  • I've dockerized the app and mysql is throwing this error.
  • I've tried restarting mysql server,
  • i've also used 127.0.0.1 instead of localhost, it still throws the
    same error.
  • I'm trying to run "docker-compose up" here and it's bringing this error mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
  • "docker-compose build" runs fine but "docker-compose up" and python manage.py runserver" throws the mysql error

Here is my utils.py file

import mysql.connector
mydb = mysql.connector.connect(
   host="127.0.0.1",
   user="root",
   passwd="aspilos",
   database="aspilos_log"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT PHONE_NUMBER FROM category2")
results = mycursor.fetchall()
for i in zip(*results):
   number = list(i)
   number1 = '+2348076548894'
   print (number)

Here is my docker-compose.yml file

version: '3.4'

services:
 db:
   image: mysql
 ports:
  - '3306:3306'
environment:
   MYSQL_DATABASE: 'app'
   MYSQL_USER: 'root'
   MYSQL_PASSWORD: 'aspilos'
   MYSQL_ROOT_PASSWORD: 'aspilos'
web:
  build: .
  command: python manage.py runserver 0.0.0.0:8000
  volumes:
  - .:/aspilos
  ports:
  - "8000:8000"
  depends_on:
  - db

Here is my settings.py file

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'app',
'USER': 'root',
'PASSWORD': 'aspilos',
'HOST': 'db',
'PORT': '3306',
},

}
1
  • Asking for off-site conversations is not allowed. Questions and their answers should be self contained. Off-site links should only be used to add clarity but the details should be here on this site. Commented Jul 3, 2020 at 9:17

1 Answer 1

1

In your utils.py, you are using host as "127.0.0.1", change it to db.

mydb = mysql.connector.connect(
   host="db",
   user="root",
   passwd="aspilos",
   database="aspilos_log"
)
Sign up to request clarification or add additional context in comments.

3 Comments

It ran but i ran into another error caching_sha2_password could not be loaded
That's a different concern altogether. This one was to resolve " Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)" error. Please raise a new question for the new error.
Thanks anyways, I got it all figures out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.