46

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

9 Answers 9

41

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

0
26

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
0
17

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
1
  • Hi Mostafa, I couldn't access the generated password using `docker logs <container-id>, how can I access the password? Commented Jul 30, 2019 at 7:05
6

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.

2
  • 1
    great... and how do you do that? docker run -e MYSQL_ROOT_PASSWORD=root doesn't work Commented Feb 8, 2019 at 19:04
  • It does work. Look at the official documentation. If you run printenv in your container you should see the env variables set. hub.docker.com/_/mysql
    – DevOps Dan
    Commented Feb 13, 2019 at 16:20
5

we can use

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

you can give any name and any password

1
4

just in case someone come here like me

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

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

1

You can also add a healthcheck to make sure your db is available before your php app starts

db:
image: mysql
container_name: ${MYSQL_SERVER_NAME}
ports:
    - "${MYSQL_LOCALPORT}:${MYSQL_PORT}"
environment:
    - "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}"
    - "MYSQL_USER=${MYSQL_USER}"
    - "MYSQL_PASSWORD=${MYSQL_PASSWORD}"
healthcheck:
        test: ["CMD", "mysqladmin" ,"ping", "-h", "${MYSQL_SERVER_NAME}"]
        timeout: 10s
        retries: 10
restart: always

And add this in your app :

    depends_on: 
    db:
        condition: service_healthy
0

Like mentioned, assign an actual value to the MYSQL_ROOT_PASSWORD item in your environment section. For a reference to an example I've used before, see here:

https://github.com/alexmacarthur/local-docker-db/blob/master/mysql/docker-compose.yml#L12

If you don't want to store the password in your docker-compose.yml file, you can also pass a .env file that contains the value, and then have that file ignored in your repo. An example of how that might look is here (using DyanmoDB, but it's the same concept):

https://github.com/alexmacarthur/local-docker-db/blob/master/dynamo/docker-compose.yml#L17

0

I just specify the password like down below and it works magically. I don't know should I use exist user or not?
MYSQL_USER: root MYSQL_PASSWORD: 1 MYSQL_ROOT_PASSWORD: 1

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 22, 2022 at 1:41
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Nick Vu
    Commented Mar 24, 2022 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.