docker, MYSQL_ROOT_PASSWORD do not work

2019-07-12 02:34发布

问题:

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.

回答1:

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.



回答2:

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