错误:数据库尚未初始化,并且未指定密码选项 [英] Error: database is uninitialized and password option is not specified

查看:915
本文介绍了错误:数据库尚未初始化,并且未指定密码选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新手。我一直在关注本教程: https://medium.com/ coderscorner /通过docker-997aa2c090cc连接到mysql 。我已经设置了root密码,但是一旦尝试访问mysql命令,它就会抛出该数据库未初始化错误。另外,当我执行 docker-compose up 命令提取所需的模块时,它会给出 django.db.utils.InternalError:(1049,未知数据库'bitpal')。我放置的命令是:



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



我想我已经在这里搜索了答案,但是我不确定是什么问题。



docker-compose.yml

 版本: 2 
服务:
#Redis
mysql:
图像:mysql:5.7
重新启动:始终
主机名:mysql
container_name:mysql
环境:
-MYSQL_USER = root
-MYSQL_ROOT_PASSWORD =密码
-MYSQL_DB = bitpal
端口:
- 3306:3306


# Redis
redis:
图像:redis:latest
重新启动:始终
主机名:redis
container_name:redis
端口:
- 6379 :6379


#Django Web服务器
bitpal:
图片:python:3.5
重新启动:始终
主机名:bitpal
container_name:bitpal
working_dir:/ bitpal
命令:./bin/start_dev.sh
卷:
-./bitpal:/bitpal
-./etc/config:/ etc / config
-./log:/log
端口:
- 80:80
链接:
-mysql
-redis
depend_on:
-mysql
环境:
#数据库
-DB_NAME = bitpal
-DB_USER = root
-DB_PASSWORD =密码
-DB_HOST = mysql
-DB_PORT = 3306


#芹菜工人
工人:
图像:python:3.5
重新启动:始终
container_name:工人
命令:bash -c ./bin/install.sh&& ./bin/celery_worker.sh
working_dir:/ bitpal
卷:
-./bitpal:/bitpal
-./etc/config:/etc/config
-./log:/log
链接:
-mysql
-redis
取决于:
-redis


#Bitshares websocket监听器
websocket_listener:
图像:python:3.5
重新启动:始终
container_name:websocket_listener
命令:bash -c ./bin/install。 sh&& ./bin/websocket_listener.sh
working_dir:/ bitpal
卷:
-./bitpal:/bitpal
-./etc/config:/etc/config
-./log:/log
链接:
-mysql
-redis
取决于:
-redis


#Nginx
nginx:
图像:nginx:1.12.1
container_name:nginx
端口:
- 8000:80
数量:
-./bitpal:/home/bitpal/bitpal/bitpal
-./nginx:/etc/nginx/conf.d
取决于:
-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 文件都存储ed在root / bitpal / bin下。我应该在其中放置 wait-for-it.sh 还是将其放置在bitpal和nginx文件夹中?

解决方案

您正在关注的本教程不完整。它并没有告诉您,如果要使用它,则必须等到初始化数据库之后。



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



您可以执行以下操作:

  $ docker logs -f<容器名称> 

其中您的容器名称 mysql 。当您看到db已初始化并且已启动DB时,只需从日志中分离(ctrl + c)并继续。



您的DB现在可以使用了。 / p>

考虑您的撰写文件的重要提示



此撰写文件无法正常工作因为其他服务,例如 bitpal / worker 不在等待数据库服务初始化。



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



在同一目录中创建一个 docker-entrypoint.sh 文件,并按以下方式编写它们:

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

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

 入口点:[ ./docker-entrypoint.sh] 

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



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



这是唯一有效的方法之一,因为 depends_on 并不等待db服务初始化,正如官方文档中明确指出的那样。



使用文件locat编辑离子说明

  root 
-bitpal
+ *某些服务文件*
+ wait-for-it.sh
+ docker-entrypoint.sh
-some_service_requiring_db
+ *一些服务文件*
+ wait-for-it.sh
+ docker-entrypoint.sh
-docker-compose.yml

和您的撰写文件应该是这样的:

 版本:'2'
服务:
#MySQL服务定义
mysql:
#像你有

#一些服务

#Django Web服务器
bitpal:
#...
入口点:[ ./docker-entrypoint.sh]
#进一步的声明


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

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`

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?

解决方案

This tutorial you were following is incomplete. It didn't tell you that you must wait until the db is initialized if you want to use it.

Just after running the database container via run command, you should check the logs of this container and wait until the DB initialization process is complete

You can do it with:

$ docker logs -f <container name>

Where container name in your case is mysql. When you see that db is initialized and DB is started, just detach (ctrl+c) from the logs and continue on.

Your DB is ready to use now.

important note considering your compose file

This compose file is not going to work because the other services like bitpal/worker are not waiting for the DB service to initialize.

Initially download a wait-for-it.sh script, that'd allow other servies to wait for your database when using compose file to setup your application. The script, made by vishnubob, is available here, then copy it to all the catalogs where your services requiring database are.

In the same catalogs create a docker-entrypoint.sh files and write them like this:

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

Then, in your compose file add entries in every service that require DB (and where you places wait-for-it.sh script) that will execute the waiting script:

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

Then, your services will wait for the DB until it's initialized and ready to accept connections.

In the edits I'll add straight forward catalog tree so that you can more clearly see how these files should be placed.

This is one of the only efficient methods because depends_on is not waiting for the db service to be initialized as it's clearly stated in the official docs.

edit with files location explanation

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

And your compose file should be like:

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

  # some services

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

这篇关于错误:数据库尚未初始化,并且未指定密码选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆