Mysql docker container keeps restarting
Asked Answered
O

6

33

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
Oceanography answered 27/3, 2021 at 13:29 Comment(3)
try change MYSQL_USER=root to MYSQL_ROOT_USER=rootSergei
I already tried that it didn't workOceanography
Your error message is clearly points to MYSQL_PASSWORD and MYSQL_USER which should not be usedSergei
F
83

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.

Fanelli answered 1/4, 2021 at 19:8 Comment(1)
but how can create a mysql separate user/password for specific database?Sedgewick
G
16

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

MYSQL_USER must be a different name, not root.

Gennie answered 27/3, 2021 at 19:24 Comment(4)
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: #66078295 to no avail. Any ideas?Tortosa
@Tortosa 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.Gennie
Thanks @Gennie 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.Tortosa
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.Blurb
C
12

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
Clotilde answered 13/4, 2021 at 0:11 Comment(1)
THANKS A LOT!!! This worked for me. And its true.. this is related to some newer versions of docker.Garmaise
R
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
Rowan answered 7/5, 2021 at 12:55 Comment(0)
O
3

Only update your the .env file:

DB_USERNAME=sail
DB_PASSWORD=password
Outweigh answered 17/4, 2021 at 10:57 Comment(0)
P
2

remove MYSQL_USER=root as root user is created by default and when you use MYSQL_USER=root it tries to create a new normal user with name root which cause conflict. do follow the image in this way:

  mysql:
    image: mysql:latest
    environment:
      MYSQL_DATABASE: "paradise"
      MYSQL_USER: "paradise"
      MYSQL_PASSWORD: "bajbciu2dbwc"
      MYSQL_ROOT_PASSWORD: "bajbciu2dbwc"
    ports:
      - "6033:3306"
    volumes:
      - mysql_data:/var/lib/mysql  # Map MySQL data directory into the container
    env_file:
      - .env
    restart: on-failure:5
    healthcheck:
      interval: 5s
      timeout: 5s
      retries: 5

setting MYSQL_ROOT_PASSWORD:"password_of_your_choice" will set password for root user and setting MYSQL_USER: "paradise" , MYSQL_PASSWORD: "bajbciu2dbwc" will create a new user for you.

Palma answered 9/12, 2023 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.