17

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.

Silence
  • 303
  • 1
  • 2
  • 10
  • 1
    I've tried this locally with a similar setup (different version of MySQL, though - I'm using 5.5) and I can't login to MySQL without a password. I have to add the `-p` switch and need to provide the password. – nwinkler Oct 20 '16 at 09:06
  • Tested with provided compose (without the volume) and seems to work fine. – Jonnix Oct 20 '16 at 09:10

7 Answers7

26

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
  • D'oh! I never would have thought to delete the data volume! I kept changing the value in `docker-compose.yaml` to no effect. Deleting the associated data volume did the trick! Thank you! – Ben Johnson Aug 09 '22 at 15:02
  • I found your answer (deleting the volume) after 1 hour of googling. – jartaud Oct 17 '22 at 07:55
  • This is the one worked for me. I mounted a local directory and had to delete it manually – y7ig May 16 '23 at 10:43
15

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

Ashok
  • 3,190
  • 15
  • 31
11

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.

gpapaz
  • 889
  • 14
  • 23
  • thank you, I posted a fragment, also have a nginx, and a php-fpm, – Silence Oct 21 '16 at 02:16
  • and environment in nginx doesn't work too, maybe my system has some problem, I trying to reinstall it. – Silence Oct 21 '16 at 02:18
  • Go one service at a time. Make MySQL work and then proceed with the nginx and then with the PHP. I doubt your system needs reinstallation. Also, try to get some logs and you can ask about other problems here, to find solutions. – gpapaz Oct 21 '16 at 04:48
  • I installed another centos in the vmware, and docker works very well, I don't know where the problem is, but it has been solved. thank you. – Silence Oct 22 '16 at 06:01
  • i had a similar case with MYSQL_ROOT_PASSWORD= but this does not work for me. any help please – mootez Oct 21 '17 at 20:46
2

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.

  • Not only is the mod of the directory important, but so it it's owner. I start docker with sudo, so I needed to have the mount point belonging to root. – Norman Pellet Nov 27 '20 at 08:28
2

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.

user3359139
  • 430
  • 4
  • 17
0

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
Hiroki Matsumoto
  • 357
  • 4
  • 10
0

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.

bunnyhu
  • 21
  • 3