Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 780
Silence Avatar asked Oct 20 '16 08:10

Silence


Video Answer


4 Answers

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.

like image 157
user3359139 Avatar answered Sep 17 '22 11:09

user3359139


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

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.

like image 45
markson edwardson Avatar answered Oct 23 '22 04:10

markson edwardson


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

like image 12
Ashok Avatar answered Oct 23 '22 04:10

Ashok


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.

like image 11
gpapaz Avatar answered Oct 23 '22 02:10

gpapaz