MySQL Docker-compose:数据库未初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39678463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:01:37  来源:igfitidea点击:

Docker-compose: Database is uninitialized

mysqldockerdocker-compose

提问by Stefano

I have a problem with docker-compose and mysql:

我对 docker-compose 和 mysql 有问题:

docker-compose.yml

docker-compose.yml

version: '2' 
  services:
   db:
    image: mysql
    volumes:
      - "./sito/db/:/var/lib/mysql"
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD:

   app:
    depends_on:
      - db
    image: eboraas/apache-php
    links:
      - db
    ports:
      - "80:80"
    volumes:
      - ./sito/:/var/www/html/

An error occurred when I compose this container:

组合此容器时发生错误:

Recreating phpapp_phpapache_1
Attaching to phpapp_db_1, phpapp_phpapache_1
db_1 | error: database is uninitialized and password option is not specified 
db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
phpapache_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.30.0.3. Set the 'ServerName' directive globally to suppress this message
phpapp_db_1 exited with code 1
db_1 | error: database is uninitialized and password option is not specified 
db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
db_1 | error: database is uninitialized and password option is not specified 
db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
db_1 | error: database is uninitialized and password option is not specified 
db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
db_1 | error: database is uninitialized and password option is not specified

But the database don't have a password. How can I solve it?

但是数据库没有密码。我该如何解决?

回答by mayo

According the documentation, instead of MYSQL_ROOT_PASSWORD:you have to use -and =and also you should use a 'password' The result it will be:

根据文档MYSQL_ROOT_PASSWORD:您不必使用-=并且还应该使用“密码”,结果将是:

- MYSQL_ROOT_PASSWORD=some_password

- MYSQL_ROOT_PASSWORD=some_password

In your example:

在你的例子中:

version: '2' 
  services:
   db:
    image: mysql
    volumes:
      - "./sito/db/:/var/lib/mysql"
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=some_password

回答by Mostafa Hussein

The problem will appear if you don't specify a value for MYSQL_ROOT_PASSWORD(other cases explained below).

如果您没有为MYSQL_ROOT_PASSWORD(其他情况解释如下)指定值,则会出现问题。

You can use one of the following syntax below:

您可以使用以下语法之一:

# syntax 1
environment:
  MYSQL_ROOT_PASSWORD: strongrootpassword
# syntax 2
environment:
 - MYSQL_ROOT_PASSWORD=strongrootpassword

If you wish to use blank/empty password then use the following:

如果您希望使用空白/空密码,请使用以下命令:

MYSQL_ALLOW_EMPTY_PASSWORD=1

If you wish to set a random password then use the following:

如果您希望设置随机密码,请使用以下命令:

MYSQL_RANDOM_ROOT_PASSWORD=1

Then you can get the generated password using

然后您可以使用获取生成的密码

docker logs container_name_or_id 

In the end you need to specify ONEof MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORDand MYSQL_RANDOM_ROOT_PASSWORDaccording to the entrypoint of MySQL image:

最后需要指定ONEof MYSQL_ROOT_PASSWORDMYSQL_ALLOW_EMPTY_PASSWORDMYSQL_RANDOM_ROOT_PASSWORD根据 MySQL 镜像的入口点:

  • -zis to verify that the variable is not empty
  • -z是验证变量不为空
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
    echo >&2 'error: database is uninitialized and password option is not specified '
    echo >&2 '  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
    exit 1
fi

回答by DevOps Dan

It looks like you're setting the MYSQL_ROOT_PASSWORDto nothing. Per the documentation, it is required.

看起来您将 设置MYSQL_ROOT_PASSWORD为空。根据文档,它是必需的。

https://hub.docker.com/_/mysql/

https://hub.docker.com/_/mysql/

MYSQL_ROOT_PASSWORD

This variable is mandatory and specifies the password that will be set for the MySQL root superuser account.

MYSQL_ROOT_PASSWORD

此变量是必需的,它指定将为 MySQL root 超级用户帐户设置的密码。

回答by Jayani Sumudini

we can use

我们可以用

docker container run -d --name mysql -e MYSQL_RANDOM_ROOT_PASSWORD=password mysql

you can give any name and any password

您可以提供任何名称和任何密码

回答by Mohammad Ganji

I just wanted to point out something, if using docker runcommand in terminal and not docker-compose, this would be the command:

我只是想指出一些事情,如果docker run在终端中使用命令而不是 docker-compose,这将是命令:

docker run -e MYSQL_ROOT_PASSWORD=password mysql_image

Just noticethat it won't work if you put -e MYSQL_ROOT_PASSWORD=passwordafter mysql_image

请注意,如果你把它放在-e MYSQL_ROOT_PASSWORD=password后面就行不通了mysql_image