Error: database is uninitialized and password option is not specified

Nor*_*ack 2 mysql bash docker

I'm new to docker. I've been following this tutorial: https://medium.com/coderscorner/connecting-to-mysql-through-docker-997aa2c090cc . I've set up the root password but once I tried to access the mysql command, it throws out this database is uninitialized error. Also, when I do docker-compose up command to pull the needed modules, it gives out an django.db.utils.InternalError: (1049, "Unknown database 'bitpal'"). The command I placed was:

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

I reckon I've searched for answers here but I couldn't be sure of what's wrong.

docker-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
Run Code Online (Sandbox Code Playgroud)

My directory looks like this.

`**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`
Run Code Online (Sandbox Code Playgroud)

All the project's .sh files are stored under root/bitpal/bin. Do I place wait-for-it.sh there instead or place it in bitpal and nginx folders?

tru*_*512 5

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

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

你可以这样做:

$ docker logs -f <container name>
Run Code Online (Sandbox Code Playgroud)

container name你的情况在哪里mysql。当您看到 db 已初始化且 DB 已启动时,只需从日志中分离 (ctrl+c) 并继续。

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

考虑您的撰写文件的重要说明

此撰写文件将无法工作,因为bitpal/等其他服务worker没有等待 DB 服务初始化。

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

在同一个目录中创建一个docker-entrypoint.sh文件并像这样编写它们:

#!/bin/bash
set -e
sh -c './wait-for-it.sh mysql:3306 -t 30'
exec "$@"
Run Code Online (Sandbox Code Playgroud)

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

entrypoint: ["./docker-entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)

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

在编辑中,我将添加直接的目录树,以便您可以更清楚地看到这些文件应该如何放置。

这是唯一有效的方法之一,因为depends_on它不会像官方文档中明确说明的那样等待 db 服务被初始化。

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

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
Run Code Online (Sandbox Code Playgroud)

你的撰写文件应该是这样的:

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

  # some services

  # Django web server
  bitpal:
    # ...
    entrypoint: ["./docker-entrypoint.sh"]
    # further declarations
Run Code Online (Sandbox Code Playgroud)