The MariaDB or MySQL container ignores the root password you set
ERROR 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.
Fix it fast
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 402Match what you see, then jump to its fix
- 1
The log shows no initialisation steps, and the password you set earlier still applies.The data volume already contained a databaseFix - 2
The 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
Can't connect through socket, or the connection drops, in the first seconds of a first start.The client connected before initialisation finishedFix
- 1
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
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-too3Verify
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
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 152Fix
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_healthy3Verify
The service reports healthy before the client starts.
docker compose psConfirmed by raw.githubusercontent.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Databases
The client cannot reach the MySQL or MariaDB server through its Unix socket file
ERROR 2002 (HY000): Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2) (MariaDB client wording)
Error 2002 means the client tried the Unix socket file named in the message and found nothing it could talk to.
4 causes, each with a fixDatabases
The MySQL container stops with: the designated data directory is unusable
[ERROR] --initialize specified but the data directory exists. Aborting.
This message closes a failed first start.
3 causes, each with a fixDatabases
MySQL: could not create connection to database server
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
These messages are wrappers.
3 causes, each with a fixDatabases
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 fixDatabases
Spring Boot in a container cannot connect to the MySQL container
Communications link failure
In Compose the app and MySQL are separate containers.
3 causes, each with a fixDatabases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fix