docker中的django mysql连接错误

axr492tv  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(208)

错误:

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撰写:

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',
    },
}

我试图连接django与mysql内部docker使用dockercompose。我可以使用docker容器中的“mysql-u soubhagya-p”访问mysql,但是使用django连接,它显示了以下错误。
请检查一下我的密码。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题