docker, MYSQL_ROOT_PASSWORD do not work

2019-07-12 02:25发布

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.

2条回答
仙女界的扛把子
2楼-- · 2019-07-12 02:39

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.

查看更多
Melony?
3楼-- · 2019-07-12 02:54

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
查看更多
登录 后发表回答