docker中的django mysql连接被拒绝

y0u0uwnf  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(405)

dockerfile文件

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

COPY ./ /usr/src/govtcareer_api

RUN pip install -r requirements.txt

CMD ["/bin/bash"]

docker-compose.yml公司

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
    links:
      - db

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

django数据库连接:

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

错误:

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")')

这是我的密码。
我试图在docker容器中集成django和mysql,但是显示连接被拒绝了。
我需要在docker中使用环境变量吗?
与我在mongodb中使用的方法相同,但在mysql中我面临这个问题。
请检查一下我的密码。

eoxn13cs

eoxn13cs1#

删除 links 从你的 docker-compose.yml .

links:
  - db

你不需要他们-docker将建立一个网络,并在那里包含你所有声明的服务。

相关问题