错误:数据库未初始化且未指定密码选项

时间:2018-05-09 07:52:04

标签: mysql bash docker

我是码头工人的新手。我一直在关注本教程:https://medium.com/coderscorner/connecting-to-mysql-through-docker-997aa2c090cc。我已经设置了root密码但是一旦我尝试访问mysql命令,就会抛出这个数据库是未初始化的错误。此外,当我执行docker-compose up命令来提取所需的模块时,它会发出 django.db.utils.InternalError:(1049,"未知数据库' bitpal'& #34)即可。我发出的命令是:

docker run --name=mysql -e MYSQL_USER=root MYSQL_ROOT_PASSWORD=password -d mysql

我估计我在这里搜索了答案,但我无法确定错误。

搬运工-compose.yml

version: '2'
services:
  # Redis
  mysql:
    image: mysql:5.7
    restart: always
    hostname: mysql
    container_name: mysql
    environment:
      - MYSQL_USER=root
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DB=bitpal
    ports:
      - "3306:3306"


  # Redis
  redis:
    image: redis:latest
    restart: always
    hostname: redis
    container_name: redis
    ports:
      - "6379:6379"


  # Django web server
  bitpal:
    image: python:3.5
    restart: always
    hostname: bitpal
    container_name: bitpal
    working_dir: /bitpal
    command: ./bin/start_dev.sh
    volumes:
      - ./bitpal:/bitpal
      - ./etc/config:/etc/config
      - ./log:/log
    ports:
      - "80:80"
    links:
      - mysql
      - redis
    depends_on:
      - mysql
    environment:
      # Database
      - DB_NAME=bitpal
      - DB_USER=root
      - DB_PASSWORD=password
      - DB_HOST=mysql
      - DB_PORT=3306


  # Celery worker
  worker:
    image: python:3.5
    restart: always
    container_name: worker
    command: bash -c "./bin/install.sh && ./bin/celery_worker.sh"
    working_dir: /bitpal
    volumes:
      - ./bitpal:/bitpal
      - ./etc/config:/etc/config
      - ./log:/log
    links:
      - mysql
      - redis
    depends_on:
      - redis


  # Bitshares websocket listener
  websocket_listener:
    image: python:3.5
    restart: always
    container_name: websocket_listener
    command: bash -c "./bin/install.sh && ./bin/websocket_listener.sh"
    working_dir: /bitpal
    volumes:
      - ./bitpal:/bitpal
      - ./etc/config:/etc/config
      - ./log:/log
    links:
      - mysql
      - redis
    depends_on:
      - redis


  # Nginx
  nginx:
    image: nginx:1.12.1
    container_name: nginx
    ports:
      - "8000:80"
    volumes:
      - ./bitpal:/home/bitpal/bitpal/bitpal
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - bitpal

我的目录看起来像这样。

`**ROOT**
     `root: .gitignore, docker-compose.yml, docker-compose-production.yml... 
     /bitpal /etc /log /nginx /public_html`

**ROOT/bitpal**
      `.gitignore, Dockerfile, Makefile, manage.py... /bin /bitpal /media 
      /static /tests`

所有项目的.sh文件都存储在root / bitpal / bin下。我是否将wait-for-it.sh放在那里或将其放在bitpal和nginx文件夹中?

1 个答案:

答案 0 :(得分:1)

您关注的本教程不完整。它没有告诉你,如果要使用它,你必须等到db初始化。

在通过run命令运行数据库容器之后,您应该检查此容器的日志并等待数据库初始化  流程已完成

你可以用:

$ docker logs -f <container name>

您案件中的container namemysql。当您看到db已初始化并且DB已启动时,只需从日志中分离(ctrl + c)并继续。

您的数据库现在可以使用了。

考虑到撰写文件

重要提示

此撰写文件不起作用,因为bitpal / worker等其他服务不等待数据库服务初始化。

最初下载wait-for-it.sh脚本,在使用撰写文件设置应用程序时,允许其他服务等待您的数据库。由vishnubob制作的脚本可用here,然后将其复制到需要数据库的服务所在的所有目录中。

在相同的目录中创建一个docker-entrypoint.sh文件并按如下方式编写:

#!/bin/bash
set -e
sh -c './wait-for-it.sh mysql:3306 -t 30'
exec "$@"

然后,在您的撰写文件中,在每个需要DB的服务(以及您放置wait-for-it.sh脚本的位置)中添加将执行等待脚本的条目:

entrypoint: ["./docker-entrypoint.sh"]

然后,您的服务将等待数据库,直到它被初始化并准备好接受连接。

在编辑中,我会添加直接目录树,以便您可以更清楚地了解这些文件的放置方式。

这是唯一有效的方法之一,因为depends_on并未等待数据库服务初始化,因为它已在官方文档中明确说明。

使用文件位置说明进行编辑

root 
   - bitpal
      + *some service files*
      + wait-for-it.sh
      + docker-entrypoint.sh
   - some_service_requiring_db
      + *some service files*
      + wait-for-it.sh
      + docker-entrypoint.sh
   - docker-compose.yml

你的撰写文件应该是:

version: '2'
services:
  # MySQL service definition
  mysql:
    # like you have

  # some services

  # Django web server
  bitpal:
    # ...
    entrypoint: ["./docker-entrypoint.sh"]
    # further declarations