Silence
Silence

Reputation: 313

docker, MYSQL_ROOT_PASSWORD do not work

docker-compose:

mysql:
image: mysql:5.7.16
container_name: f_mysql
volumes:
  - ./db:/var/lib/mysql
environment:
  MYSQL_ROOT_PASSWORD: sheep
expose:
  - '3306'

and I use docker exec input this container,

and I type echo $MYSQL_ROOT_PASSWORD, then I got sheep,

but the mysql root password still is '',

when I type 'mysql -uroot', I login mysql.

Upvotes: 18

Views: 56252

Answers (7)

markson edwardson
markson edwardson

Reputation: 710

For me the issue was that I'd created the db volume with the random password option set, then disabled that, but hadn't cleared the volume. So no matter what changes I made to the docker-compose file, the old volume with the old login information was still there.

I had to docker volume ls to find the volume then docker volume rm <name> to remove it. After re-upping, everything worked.

Regarding other answers on this page, the format for specifying env variables is correct, you can use either

environment:
  MYSQL_ROOT_PASSWORD: a_password

OR

environment:
  - MYSQL_ROOT_PASSWORD=a_password

Upvotes: 35

bunnyhu
bunnyhu

Reputation: 61

I had a same problem, and after a lot of try I found my solution. When I run first time the docker-composer, I left everything on the original settings like this:

       environment:
        MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD

Then I change the password, say "docker-compose up" but it was still MYSQL_ROOT_PASSWORD. My solution was delete the "mysql" docker image from my disk. After that, the docker download everything again BUT also set my password for the root as well. Maybe this is not the best, but I am also a beginner in Docker.

So in nutshell the simple "docker-compose up" does not enough.

Upvotes: 0

user3359139
user3359139

Reputation: 478

Please note that according to the official docker image: "none of those variables will have any effect if you start the container with a data directory that already contains a database". In fact, in this case, you have already a "mysql.user" table, and you should use the user info set there that there. The same thing happens when you try to restore a full dump.

Upvotes: 6

gpapaz
gpapaz

Reputation: 909

You need to fix your docker-compose file:

environment:
  - MYSQL_ROOT_PASSWORD=sheep

The following is the full docker-compose that achieves what you want:

version: '2'

services:
    mysql:
        image: mysql:5.7.16
        container_name: f_mysql
        volumes:
            - ./db:/var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=sheep
        expose:
            - '3306'

Then with a docker exec -it f_mysql /bin/bash and inside the container mysql -u root -p, using sheep, as the password, will be the only way to connect to the mysql server.

Upvotes: 12

Ashok
Ashok

Reputation: 3611

The image entrypoint script will never make changes to a database which is existing. If you mount an existing data directory into var/lib/mysql then MYSQL_ROOT_PASSWORD will have no effect.

Workaround

Remove all unused volumes: docker volume prune

Remove the volume from your database service: docker volume rm <db_data>

Down containers, remove volumes: docker-compose down --volumes

Upvotes: 19

Jonas Sciangula Street
Jonas Sciangula Street

Reputation: 1893

This happens when your volume from a directory has wrong permission. You can fix this letting docker to create directory itself.

In case you have an existent data, you can compare the new one with the previous one in order to apply correct chmod, because this depends on if docker/your-user is part of root group.

Upvotes: 2

Hiroki Matsumoto
Hiroki Matsumoto

Reputation: 377

This happened when the the mount directory has ea(extended attribute) on Mac.
It is better to delete the directory once and recreate it or check the permission with the xattr command.

$ ls -l ./db
$ xattr ls ./db

Upvotes: 1

Related Questions