Skip to content

Setting MYSQL_USER=root prevents docker-entrypoint-initdb.d scripts from running #641

@janvogt

Description

@janvogt

Description

When configuring the official MariaDB container with MYSQL_USER=root, scripts placed in the /docker-entrypoint-initdb.d/ directory are not executed during container initialization. This prevents database creation and other initialization tasks from completing.

Environment

  • MariaDB container version: latest
  • Docker Compose

Steps to Reproduce

  1. Create a docker-compose.yml file:
mariadb:
  image: mariadb:latest
  volumes:
    - wordpress_database_data:/var/lib/mysql
    - ./create_other_wordpress_db.sql:/docker-entrypoint-initdb.d/create_other_wordpress_db.sql:ro
  environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_USER=root
    - MYSQL_PASSWORD=password
  restart: always

volumes:
  wordpress_database_data:
  1. Create an initialization SQL file (create_other_wordpress_db.sql):
CREATE DATABASE IF NOT EXISTS `wordpress`;
CREATE DATABASE IF NOT EXISTS `other_wordpress`;
  1. Start the container using docker-compose up

Expected Behavior

Both databases (wordpress and other_wordpress) should be created as specified in the initialization script.

Actual Behavior

The initialization script is not executed and the databases are not created. The container logs show:

mariadb-1  | 2025-03-17 10:00:41+00:00 [Note] [Entrypoint]: Creating user root
mariadb-1  | 2025-03-17 10:00:41+00:00 [Note] [Entrypoint]: Securing system users (equivalent to running mysql_secure_installation)
mariadb-1  | --------------
mariadb-1  | CREATE USER 'root'@'%' IDENTIFIED BY 'password'
mariadb-1  | --------------
mariadb-1  | 
mariadb-1  | ERROR 1396 (HY000) at line 29: Operation CREATE USER failed for 'root'@'%'

Workaround

Changing MYSQL_USER=root to another username (e.g., MYSQL_USER=db_user) allows the initialization scripts to run properly.

Root Cause Analysis

The issue appears to be in the docker_setup_db function within docker-entrypoint.sh. When MYSQL_USER is set to "root", the script attempts to create a user that already exists, causing an error and preventing subsequent execution of initialization scripts.

Additional Information

This behavior significantly impacts containerized applications that rely on MariaDB initialization scripts for setup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions