skip to Main Content

I deploy my Django, Django-Q, mysql, metabase, redis on docker, but recently, I run the services and use simpleui to change admin page, but it shows the error as follow:

Traceback (most recent call last):
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
django_q           |     self.connect()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
django_q           |     self.connection = self.get_new_connection(conn_params)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
django_q           |     connection = Database.connect(**conn_params)
django_q           |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/__init__.py", line 121, in Connect
django_q           |     return Connection(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 193, in __init__
django_q           |     super().__init__(*args, **kwargs2)
django_q           | MySQLdb.OperationalError: (2002, "Can't connect to server on 'mysql' (115)")
django_q           |
django_q           | The above exception was the direct cause of the following exception:
django_q           |
django_q           | Traceback (most recent call last):
django_q           |   File "/app/manage.py", line 22, in <module>
django_q           |     main()
django_q           |   File "/app/manage.py", line 18, in main
django_q           |     execute_from_command_line(sys.argv)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
django_q           |     utility.execute()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
django_q           |     self.fetch_command(subcommand).run_from_argv(self.argv)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 412, in run_from_argv
django_q           |     self.execute(*args, **cmd_options)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 453, in execute
django_q           |     self.check()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 485, in check
django_q           |     all_issues = checks.run_checks(
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/registry.py", line 88, in run_checks
django_q           |     new_errors = check(app_configs=app_configs, databases=databases)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 14, in check_url_config
django_q           |     return check_resolver(resolver)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/core/checks/urls.py", line 24, in check_resolver
django_q           |     return check_method()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 494, in check
django_q           |     for pattern in self.url_patterns:
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
django_q           |     res = instance.__dict__[self.name] = self.func(instance)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 715, in url_patterns
django_q           |     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
django_q           |     res = instance.__dict__[self.name] = self.func(instance)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
django_q           |     return import_module(self.urlconf_name)
django_q           |   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
django_q           |     return _bootstrap._gcd_import(name[level:], package, level)
django_q           |   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
django_q           |   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
django_q           |   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
django_q           |   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
django_q           |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
django_q           |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
django_q           |   File "/app/server/urls.py", line 25, in <module>
django_q           |     path('', include('kds.urls'))
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/urls/conf.py", line 38, in include
django_q           |     urlconf_module = import_module(urlconf_module)
django_q           |   File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
django_q           |     return _bootstrap._gcd_import(name[level:], package, level)
django_q           |   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
django_q           |   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
django_q           |   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
django_q           |   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
django_q           |   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
django_q           |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
django_q           |   File "/app/kds/urls.py", line 2, in <module>
django_q           |     from . import views
django_q           |   File "/app/kds/views.py", line 17, in <module>
django_q           |     from .tasks import open_reset, close_reset
django_q           |   File "/app/kds/tasks.py", line 97, in <module>
django_q           |     if not Schedule.objects.filter(func='kds.tasks.close_all_section').exists():
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 1241, in exists
django_q           |     return self.query.has_results(using=self.db)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/query.py", line 598, in has_results
django_q           |     return compiler.has_results()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1530, in has_results
django_q           |     return bool(self.execute_sql(SINGLE))
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1560, in execute_sql
django_q           |     cursor = self.connection.cursor()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 330, in cursor
django_q           |     return self._cursor()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 306, in _cursor
django_q           |     self.ensure_connection()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
django_q           |     with self.wrap_database_errors:
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
django_q           |     raise dj_exc_value.with_traceback(traceback) from exc_value
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
django_q           |     self.connect()
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
django_q           |     self.connection = self.get_new_connection(conn_params)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
django_q           |     return func(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
django_q           |     connection = Database.connect(**conn_params)
django_q           |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/__init__.py", line 121, in Connect
django_q           |     return Connection(*args, **kwargs)
django_q           |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 193, in __init__
django_q           |     super().__init__(*args, **kwargs2)
django_q           | django.db.utils.OperationalError: (2002, "Can't connect to server on 'mysql' (115)")

Obvious it is because of disconnection with mysql, but I don’t know why my DRF service works and my client can get data from mysql via RESTful API, but my django-q service shows the disconnection error.

"settings.py" is as follow:

from pathlib import Path
import os
import socket

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-8(c-)h1d3#buf!14@6)w^n4sk&gkpa0ij2bc(1c)i1#868kwfc'

ALLOWED_HOSTS = ['*']

CSRF_TRUSTED_ORIGINS = ['https://*']
CSRF_COOKIE_SECURE = False

# Application definition

INSTALLED_APPS = [
    'simpleui',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'kds.apps.KdsConfig',
    'rest_framework',
    'corsheaders',
    'django_q',
    'django_filters',
    'gunicorn',
]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

# Simpleui Configuration
SIMPLEUI_STATIC_OFFLINE = True

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'kds.middleware.GzipResponseMiddleware',
]


ROOT_URLCONF = 'server.urls'

# Caches settings
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://server-redis:6379/1',
    }
}

ASGI_APPLICATION = 'server.routing.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'server.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'kds_db',
        'USER': '***',
        'PASSWORD': '***',
        'HOST': 'mysql',
        'PORT': '3306',
        'CONN_MAX_AGE': 60,
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/London'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

BASE_LOG_DIR = os.path.join(BASE_DIR, "log")
LOGGING = {
    'version': 1,  
    'disable_existing_loggers': False,  
    'formatters': {
        'verbose': {
            'format': '[%(asctime)s][%(threadName)s:%(thread)d] %(message)s'
        },
    },
# 过滤器
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],  # 只有在Django debug为True时才在屏幕打印日志
            'class': 'logging.StreamHandler',  #
            'formatter': 'verbose'
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],  
            'level': 'DEBUG',
            'propagate': True,  
        },
    },
}

# Django-Q
Q_CLUSTER = {
    'name': 'server', # project name
    'workers': 4, # number of workers. Default CPU cores
    'recycle': 500, # number of tasks before workers recycling memory
    'timeout': 60,
    'retry': 300,
    'compress': True, # data compression
    'cpu_affinity': 1, # the number of CPU the worker can use
    'save_limit': 250, # limitation on successful tasks
    'queue_limit': 500, # number of tasks in queue
    'label': 'Django Q',
    'redis': {
        'host': 'server-redis',
        'port': 6379,
        'db': 0,
    }
}

And my docker-compose.yml is as follow:

version: '3'
networks:
  nginx_network:
    driver: bridge
  db_network:
    driver: bridge
  redis_network:
    driver: bridge
  django_network:
    driver: bridge
  web_network:
    driver: bridge
services:
  redis:
    image: redis:alpine
    networks:
      - redis_network
    container_name: server-redis
    ports:
      - "6379:6379"
  mysql:
    image: mysql
    networks:
      - db_network
    container_name: mysql
    hostname: mysql
    restart: always
    command: --default-authentication-plugin=mysql_native_password --mysqlx=0
    ports:
      - 3307:3306
    volumes:
      - ./db:/var/lib/mysql
    environment:
      - MYSQL_HOST=mysql
      - MYSQL_PORT=3307
      - MYSQL_DATABASE=kds_db
      - MYSQL_USER=***
      - MYSQL_PASSWORD=***
      - MYSQL_ROOT_PASSWORD=***
  adminer:
    image: adminer
    restart: always
    environment:
      ADMINER_DEFAULT_SERVER: mysql
    ports:
      - 3300:8080
  metabase:
    image: metabase/metabase
    networks:
      - db_network
    ports:
      - 3000:3000
    volumes:
      - .:/app/metabase
    depends_on:
      - mysql
  django_q:
    build:
      context: .
      dockerfile: Dockerfile.prod
    command: python manage.py qcluster
    container_name: django_q
    volumes:
      - .:/app
    networks:
      - db_network
      - redis_network
      - django_network
    depends_on:
      - redis
      - mysql
  web:
    build:
      context: .
      dockerfile: Dockerfile.prod
    image: web
    ports:
      - 8000:8000
    container_name: web
    restart: always
    command: gunicorn server.wsgi:application --bind 0.0.0.0:8000 -w 8 -k gthread && python manage.py collectstatic --no-input
    volumes:
      - .:/app
    expose:
      - 8000
    networks:
      - db_network
      - redis_network
      - django_network
      - nginx_network
    depends_on:
      - mysql
      - django_q
  nginx:
    image: nginx:1.25
    container_name: nginx
    restart: always
    ports:
      - 80:80
    volumes:
      -  ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    networks:
      - nginx_network
      - web_network
    depends_on:
      - web

Could someone tell me why it happen and how to solve it? I try to modify mysql configuration, but it doesn’t work and cause my DRF connot access to mysql.

2

Answers


  1. In Django settings.py file mention the database port as 3307. Because the docker internally runs the MySQL server in 3306 which was mentioned in docker-compose 3307:3306. For external connection you need to use 3307.

    Login or Signup to reply.
  2. I checked in the location of your CACHE settings, you added the db querystring as 1,

    # Caches settings
    CACHES = {
      'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://server-redis:6379/1',
       }
    }
    

    But in Q_CLUSTER settings you mentioned redis db as 0.

    Q_CLUSTER = {
      'redis': {
          'host': 'server-redis',
          'port': 6379,
          'db': 0,
        }
    }
    

    Change your cache location to 0 like below & try it. I hope it’ll help you.

    # Caches settings
    CACHES = {
      'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://server-redis:6379/0',
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search