Stack Exchange
django-mysql connection inside docker-compose giving connection refused
Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/govtcareer_api

COPY ./ /usr/src/govtcareer_api

RUN pip install -r requirements.txt

CMD ["/bin/bash"]

docker-compose.yml
version: "3"

services:
  govtcareer_api:
    container_name: govtcareer
    build: .
    command: "bash -c 'python manage.py migrate --no-input && python manage.py runserver 0.0.0.0:8000'"
    working_dir: /usr/src/govtcareer_api
    ports:
      - "8000:8000"
    volumes:
      - ./:/usr/src/govtcareer_api
    links:
      - db

  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=Thinkonce
      - MYSQL_USER=soubhagya
      - MYSQL_PASSWORD=Thinkonce
      - MYSQL_DATABASE=freejobalert
    ports:
      - "3306:3306"

django database connection:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'freejobalert',
        'USER': 'soubhagya',
        'PASSWORD': 'Thinkonce',
        'HOST': 'localhost',
        'PORT': '3306',
    },
}

errors:
    govtcareer        |     include_deployment_checks=include_deployment_checks,
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
    govtcareer        |     issues = run_checks(tags=[Tags.database])
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    govtcareer        |     new_errors = check(app_configs=app_configs)
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    govtcareer        |     issues.extend(conn.validation.check(**kwargs))
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    govtcareer        |     issues.extend(self._check_sql_mode(**kwargs))
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    govtcareer        |     with self.connection.cursor() as cursor:
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    govtcareer        |     return self._cursor()
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    govtcareer        |     self.ensure_connection()
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    govtcareer        |     self.connect()
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    govtcareer        |     raise dj_exc_value.with_traceback(traceback) from exc_value
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    govtcareer        |     self.connect()
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    govtcareer        |     self.connection = self.get_new_connection(conn_params)
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
    govtcareer        |     return Database.connect(**conn_params)
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
    govtcareer        |     return Connection(*args, **kwargs)
    govtcareer        |   File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
    govtcareer        |     super(Connection, self).__init__(*args, **kwargs2)
    govtcareer        | django.db.utils.OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")')

Here is my code codes.
i am trying to integrate django with mysql inside docker container
but is is showing connection refused to host.
do i need to use environment variable to use it inside docker?
the same way i used for mongodb but in mysql i am facing this issue.
please have a look into my code.

What does the log of your db container say? Has MySQL definitely started up cleanly?

yes i am able to access mysql shell also which is inside docker.

Remove links from your docker-compose.yml.
links:
  - db

You don't need them - docker will build a network and include all your declared services there. 

what is ping db??

i am able to access mysql-shell from docker

Check if you are not binded to localhost in mysql config

https://stackoverflow.com/questions/1420839/cant-connect-to-mysql-server-error-111

now giving django.db.utils.OperationalError: (2002, 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")')
when i am tying with localhost instead of db

do i need apache2 inside?/

I dont know about apache, but why does your gjango tells that he is connecting locally?
