Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

The MariaDB or MySQL container ignores the root password you set

ErrorERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Short answer

The official MariaDB and MySQL images apply their password variables only while initialising an empty data directory. If the volume already has a database, they do nothing. A container with no root password option does not start, and clients that connect before initialisation ends are refused.

  • Applies to mariadb Docker image 11, mysql Docker image 8.4, Docker Compose 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the container log from the start. It says whether the data directory was initialised this time and shows any generated root password.

    docker compose logs db | head -n 40
  2. 2Match what you see, then jump to its fix

    1. 1The log shows no initialisation steps, and the password you set earlier still applies.The data volume already contained a databaseFix
    2. 2The container exits right after start with a message about the missing password option, or the log prints a generated password.No root password option was given, or a random one was generatedFix
    3. 3Can't connect through socket, or the connection drops, in the first seconds of a first start.The client connected before initialisation finishedFix

What does the error look like?

  • ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Cause 1 of 3

The data volume already contained a database

1Check

The variables have no effect on a data directory that already holds a database, except for the auto-upgrade option.

docker volume ls
docker compose exec db env | grep -E 'MARIADB_|MYSQL_'

2Fix

With disposable data, delete the volume and start again. Otherwise log in with the old password and change it with SQL.

# disposable data: deletes the database
docker compose down -v && docker compose up -d

# keep the data
docker compose exec db mariadb -uroot -p -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';"

3Verify

Log in with the new password.

docker compose exec db mariadb -uroot -p -e 'select 1'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

No root password option was given, or a random one was generated

1Check

One of a root password, an empty-password flag or a random-password flag is required. With the random flag the password appears in the logs.

docker compose logs db | grep -i -E 'password|uninitialized'

2Fix

Set a root password in the environment (for MySQL the variable is MYSQL_ROOT_PASSWORD), or read the generated one from the log.

environment:
  MARIADB_ROOT_PASSWORD: change-me
  MARIADB_DATABASE: app
  MARIADB_USER: app
  MARIADB_PASSWORD: change-me-too

3Verify

The container stays running and accepts the password.

docker compose up -d && docker compose exec db mariadb -uroot -pchange-me -e 'select 1'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The client connected before initialisation finished

1Check

The server does not accept incoming connections until initialisation completes, which can take longer on the first start.

docker compose logs db | tail -n 15

2Fix

Wait until the log says the server is ready, and make clients retry or wait on a healthcheck (the MariaDB image ships a healthcheck.sh script for this).

depends_on:
  db:
    condition: service_healthy

3Verify

The service reports healthy before the client starts.

docker compose ps

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Treat these variables as first-start settings and change passwords with SQL later.
  • Use a healthcheck and depends_on with service_healthy for every client.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

PHP throws "could not find driver" when it opens a PDO connection

Fatal error: Uncaught PDOException: could not find driver in /var/www/app/db.php:12

PDO is only a layer; each database needs its own driver extension, such as pdo_mysql or pdo_pgsql, and the DSN prefix must name a driver that is loaded.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.