Docker-compose: Database is uninitialized

霸气de小男生 提交于 2019-12-10 01:21:43

问题


I have a problem with docker-compose and mysql:

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?


回答1:


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=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



回答2:


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

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.




回答3:


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

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 ONE of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD according to the entrypoint of MySQL image:

  • -z is to verify that the variable is not empty
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



回答4:


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

docker run -e MYSQL_ROOT_PASSWORD=password mysql_image

Just notice that it won't work if you put -e MYSQL_ROOT_PASSWORD=password after mysql_image



来源:https://stackoverflow.com/questions/39678463/docker-compose-database-is-uninitialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!