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',
},

}
AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
dMd
  • 61
  • 1
  • 4
  • 11
  • 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. – AdrianHHH Jul 03 '20 at 09:17

1 Answers1

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"
)
vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
  • It ran but i ran into another error caching_sha2_password could not be loaded – dMd Jul 03 '20 at 09:42
  • 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. – vivekyad4v Jul 03 '20 at 09:55
  • Thanks anyways, I got it all figures out – dMd Jul 03 '20 at 10:20