开发者问题收集

无法使用 docker-compose up 命令运行 Django

2020-03-03
292

我无法使用 docker-compose up 命令运行我的 Django 和 MySql 应用程序。我不知道如何从 Django 项目设置中配置数据库的主机参数。之前为其他应用程序配置相同的参数时,它运行良好。我不明白这次到底出了什么问题。

settings.py

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'eitan_database',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'mysql_db',
        'PORT': 3306,
    }
}

docker-compose.yaml

version: "3"

services:
  eitan-application:
    restart: always
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py migrate && python manage.py makemigrations
      && python manage.py runserver 0.0.0.0:8000"
    depends_on:
      - mysql_db
  mysql_db:
    image: mysql:5.7
    volumes:
      - "./mysql:/var/lib/mysql"
    ports:
      - "8080:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=eitan_database
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root

Dockerfile

FROM python:3.7-slim

ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev gcc  -y
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app

WORKDIR /app
COPY ./app /app

堆栈跟踪

Traceback (most recent call last):
eitan-application_1  |   File "manage.py", line 21, in <module>
eitan-application_1  |     main()
eitan-application_1  |   File "manage.py", line 17, in main
eitan-application_1  |     execute_from_command_line(sys.argv)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
eitan-application_1  |     utility.execute()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
eitan-application_1  |     self.fetch_command(subcommand).run_from_argv(self.argv)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
eitan-application_1  |     self.execute(*args, **cmd_options)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 366, in execute
eitan-application_1  |     self.check()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 395, in check
eitan-application_1  |     include_deployment_checks=include_deployment_checks,
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 63, in _run_checks
eitan-application_1  |     issues = run_checks(tags=[Tags.database])
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
eitan-application_1  |     new_errors = check(app_configs=app_configs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
eitan-application_1  |     issues.extend(conn.validation.check(**kwargs))
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
eitan-application_1  |     issues.extend(self._check_sql_mode(**kwargs))
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
eitan-application_1  |     with self.connection.cursor() as cursor:
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 24, in inner
eitan-application_1  |     return func(*args, **kwargs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 260, in cursor
eitan-application_1  |     return self._cursor()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 236, in _cursor
eitan-application_1  |     self.ensure_connection()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 24, in inner
eitan-application_1  |     return func(*args, **kwargs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
eitan-application_1  |     self.connect()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
eitan-application_1  |     raise dj_exc_value.with_traceback(traceback) from exc_value
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
eitan-application_1  |     self.connect()
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 24, in inner
eitan-application_1  |     return func(*args, **kwargs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
eitan-application_1  |     self.connection = self.get_new_connection(conn_params)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 24, in inner
eitan-application_1  |     return func(*args, **kwargs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 233, in get_new_connection
eitan-application_1  |     return Database.connect(**conn_params)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
eitan-application_1  |     return Connection(*args, **kwargs)
eitan-application_1  |   File "/usr/local/lib/python3.7/site-packages/MySQLdb/connections.py", line 179, in __init__
eitan-application_1  |     super(Connection, self).__init__(*args, **kwargs2)
eitan-application_1  | django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'mysql_db' (-2)")
eitan_mysql_db_1 exited with code 2

MySql 容器日志

docker-compose logs -f mysql_db

Attaching to eitan_mysql_db_1
mysql_db_1           | 2020-03-03 10:10:10+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
mysql_db_1           | 2020-03-03 10:10:11+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_db_1           | 2020-03-03 10:10:11+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
mysql_db_1           | 2020-03-03 10:10:11 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_db_1           | 2020-03-03 10:10:11 0 [Note] mysqld (mysqld 5.6.47) starting as process 1 ...
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Plugin 'FEDERATED' is disabled.
mysql_db_1           | mysqld: Table 'mysql.plugin' doesn't exist
mysql_db_1           | 2020-03-03 10:10:11 1 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: The InnoDB memory heap is disabled
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Memory barrier is not used
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Using Linux native AIO
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Using CPU crc32 instructions
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Initializing buffer pool, size = 128.0M
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Completed initialization of buffer pool
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_db_1           | InnoDB: No valid checkpoint found.
mysql_db_1           | InnoDB: If you are attempting downgrade from MySQL 5.7.9 or later,
mysql_db_1           | InnoDB: please refer to http://dev.mysql.com/doc/refman/5.6/en/upgrading-downgrading.html
mysql_db_1           | InnoDB: If this error appears when you are creating an InnoDB database,
mysql_db_1           | InnoDB: the problem may be that during an earlier attempt you managed
mysql_db_1           | InnoDB: to create the InnoDB data files, but log file creation failed.
mysql_db_1           | InnoDB: If that is the case, please refer to
mysql_db_1           | InnoDB: http://dev.mysql.com/doc/refman/5.6/en/error-creating-innodb.html
mysql_db_1           | 2020-03-03 10:10:11 1 [ERROR] Plugin 'InnoDB' init function returned error.
mysql_db_1           | 2020-03-03 10:10:11 1 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
mysql_db_1           | 2020-03-03 10:10:11 1 [ERROR] Unknown/unsupported storage engine: InnoDB
mysql_db_1           | 2020-03-03 10:10:11 1 [ERROR] Aborting
mysql_db_1           | 
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Binlog end
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'partition'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_DELETED'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_METRICS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMPMEM'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMP_RESET'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_CMP'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_LOCKS'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'INNODB_TRX'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'BLACKHOLE'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'ARCHIVE'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'MRG_MYISAM'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'MyISAM'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'MEMORY'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'CSV'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'sha256_password'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'mysql_old_password'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'mysql_native_password'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] Shutting down plugin 'binlog'
mysql_db_1           | 2020-03-03 10:10:11 1 [Note] mysqld: Shutdown complete
mysql_db_1           | 
mysql_db_1           | 2020-03-03 10:10:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
mysql_db_1           | 2020-03-03 10:10:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_db_1           | 2020-03-03 10:10:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.6.47-1debian9 started.
mysql_db_1           | 2020-03-03 10:10:14 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
mysql_db_1           | 2020-03-03 10:10:14 0 [Note] mysqld (mysqld 5.6.47) starting as process 1 ...
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Plugin 'FEDERATED' is disabled.
mysql_db_1           | mysqld: Table 'mysql.plugin' doesn't exist
mysql_db_1           | 2020-03-03 10:10:14 1 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: The InnoDB memory heap is disabled
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Memory barrier is not used
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Compressed tables use zlib 1.2.11
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Using Linux native AIO
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Using CPU crc32 instructions
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Initializing buffer pool, size = 128.0M
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Completed initialization of buffer pool
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] InnoDB: Highest supported file format is Barracuda.
mysql_db_1           | InnoDB: No valid checkpoint found.
mysql_db_1           | InnoDB: If you are attempting downgrade from MySQL 5.7.9 or later,
mysql_db_1           | InnoDB: please refer to http://dev.mysql.com/doc/refman/5.6/en/upgrading-downgrading.html
mysql_db_1           | InnoDB: If this error appears when you are creating an InnoDB database,
mysql_db_1           | InnoDB: the problem may be that during an earlier attempt you managed
mysql_db_1           | InnoDB: to create the InnoDB data files, but log file creation failed.
mysql_db_1           | InnoDB: If that is the case, please refer to
mysql_db_1           | InnoDB: http://dev.mysql.com/doc/refman/5.6/en/error-creating-innodb.html
mysql_db_1           | 2020-03-03 10:10:14 1 [ERROR] Plugin 'InnoDB' init function returned error.
mysql_db_1           | 2020-03-03 10:10:14 1 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
mysql_db_1           | 2020-03-03 10:10:14 1 [ERROR] Unknown/unsupported storage engine: InnoDB
mysql_db_1           | 2020-03-03 10:10:14 1 [ERROR] Aborting
mysql_db_1           | 
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Binlog end
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'partition'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_DELETED'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_METRICS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMPMEM'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMP_RESET'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_CMP'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_LOCKS'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'INNODB_TRX'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'BLACKHOLE'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'ARCHIVE'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'MRG_MYISAM'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'MyISAM'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'MEMORY'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'CSV'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'sha256_password'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'mysql_old_password'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'mysql_native_password'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] Shutting down plugin 'binlog'
mysql_db_1           | 2020-03-03 10:10:14 1 [Note] mysqld: Shutdown complete
mysql_db_1           | 
mysql_db_1           | 2020-03-03 10:10:17+00:00 [Note] 
3个回答

在您的 docker-compose.yaml 文件中,您正在端口 8080 上公开数据库,并尝试通过 setting.py 文件中的端口 3306 访问它。

enkodr
2020-03-03

您的设置有几个问题

  1. 该错误是 MySQL 连接错误,因为服务未在端口 3306 上侦听,而您将其绑定到端口 8080,因此您需要将 DATABASES 字典更改为以下内容

  2. 您的 python 命令顺序错误。您需要在 migrate 之前运行 makemigrations 。前者创建迁移文件,后者将它们应用于数据库

Steve Mapes
2020-03-03

settings.py 中, HOST 参数名称不正确。

运行 docker ps

检查镜像 MySql:5.6 的 NAME 并复制它。

然后在您的 setting.py 文件中,使用新建 MySql 容器的 NAME 更新数据库的 HOST 参数。

DATABASES = {
     'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'eitan_database',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '' c# name of newly created `mysql:5.6` image container name
        'PORT': 3306,
    }
}

此外,从 docker-compose 文件中的服务 mysql_db 中删除端口。

ports:
      - "8080:3306"

现在运行

docker-compose up

Jeet Patel
2020-03-11