skip to Main Content

I am trying to start django project using MySQL and Docker to containerize it. But get an error. Here is it’s traceback:

testcasebooks-backend-1  | Traceback (most recent call last):
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
testcasebooks-backend-1  |     self.connect()
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
testcasebooks-backend-1  |     self.connection = self.get_new_connection(conn_params)
testcasebooks-backend-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
testcasebooks-backend-1  |     connection = Database.connect(**conn_params)
testcasebooks-backend-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/MySQLdb/__init__.py", line 121, in Connect
testcasebooks-backend-1  |     return Connection(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/MySQLdb/connections.py", line 193, in __init__
testcasebooks-backend-1  |     super().__init__(*args, **kwargs2)
testcasebooks-backend-1  | MySQLdb.OperationalError: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
testcasebooks-backend-1  | 
testcasebooks-backend-1  | The above exception was the direct cause of the following exception:
testcasebooks-backend-1  | 
testcasebooks-backend-1  | Traceback (most recent call last):
testcasebooks-backend-1  |   File "/service/./manage.py", line 22, in <module>
testcasebooks-backend-1  |     main()
testcasebooks-backend-1  |   File "/service/./manage.py", line 18, in main
testcasebooks-backend-1  |     execute_from_command_line(sys.argv)
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
testcasebooks-backend-1  |     utility.execute()
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
testcasebooks-backend-1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
testcasebooks-backend-1  |     self.execute(*args, **cmd_options)
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
testcasebooks-backend-1  |     output = self.handle(*args, **options)
testcasebooks-backend-1  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 106, in wrapper
testcasebooks-backend-1  |     res = handle_func(*args, **kwargs)
testcasebooks-backend-1  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 100, in handle
testcasebooks-backend-1  |     self.check(databases=[database])
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
testcasebooks-backend-1  |     all_issues = checks.run_checks(
testcasebooks-backend-1  |                  ^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
testcasebooks-backend-1  |     new_errors = check(app_configs=app_configs, databases=databases)
testcasebooks-backend-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/core/checks/database.py", line 13, in check_database_backends
testcasebooks-backend-1  |     issues.extend(conn.validation.check(**kwargs))
testcasebooks-backend-1  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/validation.py", line 9, in check
testcasebooks-backend-1  |     issues.extend(self._check_sql_mode(**kwargs))
testcasebooks-backend-1  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/validation.py", line 14, in _check_sql_mode
testcasebooks-backend-1  |     self.connection.sql_mode & {"STRICT_TRANS_TABLES", "STRICT_ALL_TABLES"}
testcasebooks-backend-1  |     ^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
testcasebooks-backend-1  |     res = instance.__dict__[self.name] = self.func(instance)
testcasebooks-backend-1  |                                          ^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 443, in sql_mode
testcasebooks-backend-1  |     sql_mode = self.mysql_server_data["sql_mode"]
testcasebooks-backend-1  |                ^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
testcasebooks-backend-1  |     res = instance.__dict__[self.name] = self.func(instance)
testcasebooks-backend-1  |                                          ^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 399, in mysql_server_data
testcasebooks-backend-1  |     with self.temporary_connection() as cursor:
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/contextlib.py", line 137, in __enter__
testcasebooks-backend-1  |     return next(self.gen)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 705, in temporary_connection
testcasebooks-backend-1  |     with self.cursor() as cursor:
testcasebooks-backend-1  |          ^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 330, in cursor
testcasebooks-backend-1  |     return self._cursor()
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 306, in _cursor
testcasebooks-backend-1  |     self.ensure_connection()
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
testcasebooks-backend-1  |     with self.wrap_database_errors:
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
testcasebooks-backend-1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
testcasebooks-backend-1  |     self.connect()
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
testcasebooks-backend-1  |     self.connection = self.get_new_connection(conn_params)
testcasebooks-backend-1  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
testcasebooks-backend-1  |     return func(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
testcasebooks-backend-1  |     connection = Database.connect(**conn_params)
testcasebooks-backend-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/MySQLdb/__init__.py", line 121, in Connect
testcasebooks-backend-1  |     return Connection(*args, **kwargs)
testcasebooks-backend-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
testcasebooks-backend-1  |   File "/usr/local/lib/python3.11/site-packages/MySQLdb/connections.py", line 193, in __init__
testcasebooks-backend-1  |     super().__init__(*args, **kwargs2)
testcasebooks-backend-1  | django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
testcasebooks-backend-1 exited with code 1

Here is docker-compose.yml:

services:

  # Backend Django
  backend:
    build: .
    command:
      bash -c "./manage.py collectstatic --noinput && ./manage.py migrate && 
      ./manage.py runserver 0.0.0.0:8000"
    ports:
      - '8000:8000'
    depends_on:
      - db
    volumes:
      - .:/service
    env_file:
      - .env

  # MySQL
  db:
    image: mysql:8.2.0
    env_file:
      - .env
    volumes:
      - ./data/mysql/db:/var/lib/mysql
    ports:
      - '3306:3306'


  # Redis
  redis:
    image: redis:7.0.5-alpine

  # Celery Worker
  celery:
    restart: always
    build:
      context: .
    entrypoint: celery
    command: -A celery_app.app worker --loglevel=info
    volumes:
      - .:/service
    container_name: celery
    depends_on:
      - db
      - redis
      - backend
    links:
      - redis
    env_file:
      - .env

.env file with env varibles:

MYSQL_DATABASE=test_db
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password
MYSQL_PORT=3306
MYSQL_HOST=localhost

and settings for django database:

DB_USER = os.environ.get('MYSQL_USER')
DB_PASS = os.environ.get('MYSQL_PASSWORD')
DB_NAME = os.environ.get('MYSQL_DATABASE')
DB_PORT = os.environ.get('MYSQL_PORT')
DB_HOST = os.environ.get('MYSQL_HOST')

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASS,
        'HOST': DB_HOST,
        'PORT': DB_PORT,
    }
}

 

I’ve been struggling with this problem for a long period of time, but can not find the solution eventhough I’ve tried a lot.

2

Answers


  1. you must change the environment variable DB_HOST on the backend container, and it’s better to set a hostname for the mysql container here’s how :

     # Backend Django
      backend:
        build: .
        hostname: djangoserver
        command:
          bash -c "./manage.py collectstatic --noinput && ./manage.py migrate && 
          ./manage.py runserver 0.0.0.0:8000"
        ports:
          - '8000:8000'
        depends_on:
          - db
        volumes:
          - .:/service
        environment:
          - MYSQL_DATABASE=test_db
          - MYSQL_USER=user
          - MYSQL_PASSWORD=password
          - MYSQL_ROOT_PASSWORD=password
          - MYSQL_PORT=3306
          # host alias "mysql"
          - MYSQL_HOST=mysql
        networks:
          db-network:
            aliases:
              - djangoserver
    
      # MySQL
      db:
        image: mysql:8.2.0
        hostname: mysql
        env_file:
          - .env
        volumes:
          - ./data/mysql/db:/var/lib/mysql
        ports:
          - '3306:3306'
        networks:
          db-network:
            aliases:
              - mysql
    ...
    
    networks:
      db-network:
        driver: bridge
    

    I hope this work for you, ask me any question if you face another problem with this, i didn’t try your docker-compose on my computer but this is how you can solve your problem, happy coding

    Login or Signup to reply.
  2. You can try to change MYSQL_host to db instead of localhost as you config in docker-compose.yml

      # MySQL
      db: // here is the host of mysql
        image: mysql:8.2.0
        env_file:
          - .env
        volumes:
          - ./data/mysql/db:/var/lib/mysql
        ports:
          - '3306:3306'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search