Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构建Docker mysql数据库后抛出错误_Mysql_Django_Docker_Nginx_Docker Compose - Fatal编程技术网

构建Docker mysql数据库后抛出错误

构建Docker mysql数据库后抛出错误,mysql,django,docker,nginx,docker-compose,Mysql,Django,Docker,Nginx,Docker Compose,我试图用Docker+Nginx+MySQL运行一个Django项目,在成功构建之后,通过运行 docker-compose up --build 这是我的设置.py数据库连接 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'smartCut', 'USER': 'antu', 'PASSWORD': 'secure',

我试图用Docker+Nginx+MySQL运行一个Django项目,在成功构建之后,通过运行

docker-compose up --build
这是我的设置.py数据库连接

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'smartCut',
        'USER': 'antu',
        'PASSWORD': 'secure',
        'HOST': 'mysql',
        'PORT': 3306,
    }
}
如果我使用主机:“mysql”

错误 但是如果我使用 如果我使用主机:“localhost”

此外,此项目在管理面板中找不到静态文件

我做错了什么

更新:

$docker ps-a

docker compose.yml

version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure
      - MYSQL_DATABASE=smartCut
    healthcheck:
      test: "exit 0"

volumes:
  data-volume:
$docker日志83572753b4ae


如前所述,您应该在数据库配置中使用
mysql
作为主机和
root
作为
USER

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'smartCut',
        'USER': 'root',
        'PASSWORD': 'secure',
        'HOST': 'mysql',
        'PORT': 3306,
    }
}
mysql
将解析为mysql容器IP,因为来自compose的容器将连接到同一网络

此外,您还使用环境变量-
mysql\u root\u password=secure
为您的
mysql
容器设置了
root
密码,因此
root
必须用作用户

如果您想创建新用户,可以使用
MYSQL\u user,MYSQL\u PASSWORD
环境变量-参考可在中找到。相应地修改docker compose:

version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure #root password
      - MYSQL_DATABASE=smartCut #this database will be created for MYSQL_USER
      - MYSQL_USER=antu 
      - MYSQL_PASSWORD=secure #your user password
    healthcheck:
      test: "exit 0"

volumes:
  data-volume:

这对我来说很有用,我可以用这个docker组件连接到MySQL。您是否更改了此repo中的代码?我尝试运行两个项目,其中一个项目运行在3306 MySQL端口,另一个项目运行在3301端口,但两个都不工作您是否更改了
app/config/settings.py
文件和数据库配置?显示此文件。您需要将收到的实际错误消息作为文本包含在问题中;不要粘贴浏览器窗口的屏幕截图。显示告诉应用程序如何连接到数据库的代码或配置也会很有帮助。
USER
应该是数据库设置中的
root
。并将
mysql
保留为主机。
version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure
      - MYSQL_DATABASE=smartCut
    healthcheck:
      test: "exit 0"

volumes:
  data-volume:
2019-07-21T10:00:06.327977Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-07-21T10:00:06.328151Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
2019-07-21T10:00:06.332333Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2019-07-21T10:00:12.001312Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-07-21T10:00:12.150341Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-07-21T10:00:12.290955Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2019-07-21T10:00:12.817464Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'smartCut',
        'USER': 'root',
        'PASSWORD': 'secure',
        'HOST': 'mysql',
        'PORT': 3306,
    }
}
version: "2.4"
services:
  web:
    container_name: web
    build: ./docker/nginx
    ports:
      - 8888:80
    volumes:
      - ./app:/var/www/html
    working_dir: /etc/nginx
    links:
      - python
  python:
    container_name: python
    build: ./docker/python
    volumes:
      - ./app:/var/www/html
    working_dir: /var/www/html
    depends_on:
      mysql:
        condition: service_healthy
  mysql:
    build: docker/mysql
    container_name: mysql
    ports:
      - 3306:3306
    volumes:
      - data-volume:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=secure #root password
      - MYSQL_DATABASE=smartCut #this database will be created for MYSQL_USER
      - MYSQL_USER=antu 
      - MYSQL_PASSWORD=secure #your user password
    healthcheck:
      test: "exit 0"

volumes:
  data-volume: