31

The Container keeps restarting. I tried

  • docker-compose down -v
  • docker volume rm

The container was working fine earlier.

Logs

2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.

2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'

2021-03-27 13:16:08+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.23-1debian10 started.

2021-03-27 13:16:08+00:00 [ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_USER and MYSQL_PASSWORD are for configuring a regular user and cannot be used for the root user

Remove MYSQL_USER="root" and use one of the following to control the root user password:

- MYSQL_ROOT_PASSWORD

- MYSQL_ALLOW_EMPTY_PASSWORD

- MYSQL_RANDOM_ROOT_PASSWORD

Docker-compose.yml

 mysql:
    image: mysql:8.0
    ports:
      - 3306:3306
    expose:
      - "3306"
    cap_add:
      - SYS_NICE # CAP_SYS_NICE
    volumes:
      - ./cache/mysql:/var/lib/mysql
      - ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_PASSWORD=root
      - MYSQL_USER=root
      - MYSQL_DATABASE=mydb
    restart: unless-stopped
Shimron Duan
  • 391
  • 1
  • 3
  • 9

5 Answers5

75

Simply remove the MYSQL_USER and it will work fine because the root user gets created automatically.

PS. This seems to be a problem with a newer docker version because this used to work before and not throw an error.

Martin Zeltin
  • 2,496
  • 3
  • 18
  • 36
14

User root is reserved and already created with mysql when it's up.

MYSQL_USER must be a different name, not root.

  • Thanks and this does indeed allow me to get _mysql_1 running in Laravel Sail, but there is no sign of my user on the DB. .env contains: DB_USERNAME=lti_laravel_user and docker-compose.yml: MYSQL_USER: '${DB_USERNAME}'. Have already tried this suggestion: https://stackoverflow.com/questions/66077795/laravel-sail-database-user-not-created to no avail. Any ideas? – theotherdy Mar 27 '21 at 20:02
  • 1
    @theotherdy at first, you can run "docker-compose config" to check all the env variables are loaded to docker-compose correctly. For the user in mysql, let's try connect to mysql db with that username and password. And create another question if needed. –  Mar 27 '21 at 20:13
  • Thanks @user15494648 that does indeed show: MYSQL_USER: lti_laravel_user but there was no user in the DB with that name when using HeidiSQL and connecting root. I just created the user manually in the end. Would be nice to understand what's going wrong though as will be annoying to have to redo this every time I need to rebuild. – theotherdy Mar 29 '21 at 19:42
  • This turned out to be my issue. My .env file was accidentally set to use the root user, which is naughty. When Docker tried to build the image, it attempted to create the database user I requested in the .env file: root. MySql didn't like that and threw errors. Changed the database user to something rational and all worked fine. – Tanoro Jul 15 '21 at 02:32
9

I faced the exact same problem and here is how I fixed it.

Go to your docker-compose.yml file and make the following changes:

  • "MYSQL_USER: root" to "MYSQL_ROOT_USER: root" then delete the previous one.

  • "MYSQL_PASSWORD: YourPasseord" to "MYSQL_ROOT_PASSWORD: YourPasseord" then delete the previous one.

    Example:Here is my configuration...

database_server:

image: mysql:8.0
       container_name: mysql
       restart: always
       environment:
         MYSQL_DATABASE: DB_epraca
         MYSQL_ROOT_USER: root
         MYSQL_ROOT_PASSWORD: Password
         MYSQL_ROOT_HOST: localhost
5

There was recent change how the official mysql docker which caused this issue. For further details you can check this PR on github.

For a quick solution you should remove MYSQL_USER=root and your docker-compose.yaml file should look something like this

mysql:
    image: mysql:8.0
    ports:
      - 3306:3306
    expose:
      - "3306"
    cap_add:
      - SYS_NICE # CAP_SYS_NICE
    volumes:
      - ./cache/mysql:/var/lib/mysql
      - ./conf-mysql.cnf:/etc/mysql/conf.d/mysql.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=mydb
    restart: unless-stopped
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
3

Only update your the .env file:

DB_USERNAME=sail
DB_PASSWORD=password
martio
  • 121
  • 1
  • 5