django.db.utils.OperationalError: (2002, "Can't connect to server on 'mysql' (115)")

599 views Asked by At

I did find some related questions, but the answers don't work. I think the problem might be the configuration problem. When I run sudo docker-compose up, it create 4 images. But the error occurs like that:

rest          | Exception in thread django-main-thread:
server-redis  | 1:C 17 Oct 2023 10:37:36.248 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
mysql         | 2023-10-17T11:35:37.321727Z 8 [Warning] [MY-013360] [Server] Plugin mysql_native_password reported: ''mysql_native_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
rest          | Traceback (most recent call last):
server-redis  | 1:C 17 Oct 2023 10:37:36.248 * Redis version=7.2.1, bits=64, commit=00000000, modified=0, pid=1, just started
mysql         | 2023-10-17T11:35:47.435037Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.1.0).
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
server-redis  | 1:C 17 Oct 2023 10:37:36.248 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
mysql         | 2023-10-17T11:35:48.479576Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.1.0)  MySQL Community Server - GPL.
rest          |     self.connect()
server-redis  | 1:M 17 Oct 2023 10:37:36.249 * Increased maximum number of open files to 10032 (it was originally set to 1024).
mysql         | 2023-10-17T11:35:48.483266Z 0 [System] [MY-015016] [Server] MySQL Server - end.
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
server-redis  | 1:M 17 Oct 2023 10:37:36.249 * monotonic clock: POSIX clock_gettime
rest          |     return func(*args, **kwargs)
server-redis  | 1:M 17 Oct 2023 10:37:36.263 * Running mode=standalone, port=6379.
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
server-redis  | 1:M 17 Oct 2023 10:37:36.273 * Server initialized
rest          |     self.connection = self.get_new_connection(conn_params)
server-redis  | 1:M 17 Oct 2023 10:37:36.274 * Ready to accept connections tcp
server-redis  | 1:signal-handler (1697539188) Received SIGTERM scheduling shutdown...
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
server-redis  | 1:M 17 Oct 2023 10:39:48.171 * User requested shutdown...
rest          |     return func(*args, **kwargs)
server-redis  | 1:M 17 Oct 2023 10:39:48.172 * Saving the final RDB snapshot before exiting.
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
server-redis  | 1:M 17 Oct 2023 10:39:48.175 * DB saved on disk
rest          |     connection = Database.connect(**conn_params)
server-redis  | 1:M 17 Oct 2023 10:39:48.176 # Redis is now ready to exit, bye bye...
rest          |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/__init__.py", line 121, in Connect
server-redis  | 1:C 17 Oct 2023 10:40:08.865 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
rest          |     return Connection(*args, **kwargs)
server-redis  | 1:C 17 Oct 2023 10:40:08.865 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
rest          |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 193, in __init__
server-redis  | 1:C 17 Oct 2023 10:40:08.865 * Redis version=7.2.1, bits=64, commit=00000000, modified=0, pid=1, just started
rest          |     super().__init__(*args, **kwargs2)
server-redis  | 1:C 17 Oct 2023 10:40:08.865 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
rest          | MySQLdb.OperationalError: (1045, "Access denied for user 'kds_admin'@'172.18.0.5' (using password: YES)")
server-redis  | 1:M 17 Oct 2023 10:40:08.866 * Increased maximum number of open files to 10032 (it was originally set to 1024).
rest          |
server-redis  | 1:M 17 Oct 2023 10:40:08.866 * monotonic clock: POSIX clock_gettime
rest          | The above exception was the direct cause of the following exception:
rest          |
rest          | Traceback (most recent call last):
rest          |   File "/usr/local/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
rest          |     self.run()
rest          |   File "/usr/local/lib/python3.10/threading.py", line 953, in run
rest          |     self._target(*self._args, **self._kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
rest          |     fn(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 136, in inner_run
rest          |     self.check_migrations()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 574, in check_migrations
rest          |     executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
rest          |     self.loader = MigrationLoader(self.connection)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
rest          |     self.build_graph()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
rest          |     self.applied_migrations = recorder.applied_migrations()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
rest          |     if self.has_table():
server-redis  | 1:M 17 Oct 2023 10:40:08.876 * Running mode=standalone, port=6379.
server-redis  | 1:M 17 Oct 2023 10:40:08.880 * Server initialized
server-redis  | 1:M 17 Oct 2023 10:40:08.881 * Loading RDB produced by version 7.2.1
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
rest          |     with self.connection.cursor() as cursor:
server-redis  | 1:M 17 Oct 2023 10:40:08.881 * RDB age 20 seconds
server-redis  | 1:M 17 Oct 2023 10:40:08.881 * RDB memory usage when created 0.90 Mb
server-redis  | 1:M 17 Oct 2023 10:40:08.881 * Done loading RDB, keys loaded: 0, keys expired: 1.
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
rest          |     return func(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 330, in cursor
rest          |     return self._cursor()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 306, in _cursor
rest          |     self.ensure_connection()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
rest          |     return func(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
rest          |     with self.wrap_database_errors:
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
rest          |     raise dj_exc_value.with_traceback(traceback) from exc_value
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
rest          |     self.connect()
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
rest          |     return func(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/base/base.py", line 270, in connect
rest          |     self.connection = self.get_new_connection(conn_params)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
rest          |     return func(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
rest          |     connection = Database.connect(**conn_params)
rest          |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/__init__.py", line 121, in Connect
rest          |     return Connection(*args, **kwargs)
rest          |   File "/usr/local/lib/python3.10/site-packages/MySQLdb/connections.py", line 193, in __init__
rest          |     super().__init__(*args, **kwargs2)
rest          | django.db.utils.OperationalError: (1045, "Access denied for user 'kds_admin'@'172.18.0.5' (using password: YES)")

My settings.py database setting is:

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

My Dockerfile:

FROM python:3.10

ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev -y

RUN mkdir -p /app
WORKDIR /app

RUN pip install pip -U

COPY requirements.txt /app/

RUN pip install --no-cache-dir -r requirements.txt

COPY . /app/

My docker-compose.yml:

version: '3'
services:
  redis:
    image: redis:alpine
    container_name: server-redis
    ports:
      - "6379:6379"
  mysql:
    image: mysql
    container_name: mysql
    hostname: mysql
    command: --default-authentication-plugin=mysql_native_password --mysqlx=0
    volumes:
      - ./mysql:/var/lib/mysql
    environment:
      - MYSQL_HOST=localhost
      - MYSQL_DATABASE=kds_db
      - MYSQL_USER=kds_admin
      - MYSQL_PASSWORD=Kds63893
    ports:
      - "3307:3306"
    restart: always
  django_q:
    build: .
    command: python manage.py qcluster
    container_name: django_q
    volumes:
      - .:/app
    depends_on:
      - redis
      - mysql
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: rest
    container_name: rest
    restart: always
    command: "python manage.py runserver 10.36.0.149:8000 && ./wait-for-it.sh mysql:3306"
    ports:
      - "8000:8000"
    volumes:
      - .:/app
    depends_on:
      - mysql
      - django_q

I want to deploy these four services, but now web and django-q services cannot work because of the error on connection to MySQL. So how can I fix it ? did I miss something?

I got some update on my docker-compose.yml and Django settings.py, but there is a new error.

0

There are 0 answers