Docker内部的django-mysql连接错误

时间:2018-12-16 13:55:24

标签: mysql django docker

错误:

govtcareer        | 
govtcareer        | Traceback (most recent call last):
govtcareer        |   File "manage.py", line 15, in <module>
govtcareer        |     execute_from_command_line(sys.argv)
govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
govtcareer        |     utility.execute()
govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
govtcareer        |     self.fetch_command(subcommand).run_from_argv(self.argv)
govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
govtcareer        |     self.execute(*args, **cmd_options)
govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 350, in execute
govtcareer        |     self.check()
govtcareer        |   File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
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")')

Dockerfile:

FROM python:3.6
ENV PYTHONUNBUFFERED 1
WORKDIR /usr/src/govtcareer_api

COPY ./ /usr/src/govtcareer_api

RUN pip install -r requirements.txt

RUN apt-get install nano

CMD ["/bin/bash"]

docker-compose:

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

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

settings.py中的Django数据库连接

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

我正在尝试使用docker-compose在Django内部将django与mysql连接。 我能够在docker容器中使用“ mysql -u soubhagya -p”访问mysql 但是使用django连接时,它会显示以下错误。

请查看我的代码。

0 个答案:

没有答案