Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Locked out of a fresh MariaDB install as root on Mac or Linux

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

Short answer

A fresh MariaDB root account is not a normal password account. It logs in through the unix_socket plugin, or with a password that was set to an invalid value. So a typed password fails. Log in as the matching operating system user, set a real password from inside, or reset it with the server started without grant tables.

  • Applies to MariaDB 10.11, MariaDB 11.4, macOS, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Try to connect without a password and without -u root, as the same operating system user that installed the server. On Linux, also try the root-owned socket login.

    mariadb
    sudo mariadb
  2. 2Match what you see, then jump to its fix

    1. 1Access denied for root@localhost even with the right-looking password, because no password is checked at allThe account uses unix_socket, so the OS user must matchFix
    2. 2The same error with -p, and you never set a password for rootroot has no usable password yetFix
    3. 3Password login fails and the socket login is not available eitherThe password was set and then lostFix

What does the error look like?

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

Cause 1 of 3

The account uses unix_socket, so the OS user must match

1Check

mariadb-install-db creates root@localhost to log in with unix_socket first, which only lets a process owned by the OS root user in without a password. It also creates a second account, named after the user that ran the install, with the same socket login.

id -un
sudo mariadb -e "SELECT user, host FROM mysql.user"

2Fix

Connect the way the plugin expects: sudo mariadb for the root account, or plain mariadb as the OS user that installed the server. Do not pass -p.

sudo mariadb
# or, as the user that ran the install:
mariadb

3Verify

The prompt opens without asking for a password.

mariadb -e "SELECT CURRENT_USER()"

Confirmed by mariadb.com

Did this fix it?

Cause 2 of 3

root has no usable password yet

1Check

The install sets an invalid password on the root account, so password login cannot work until you set a real one. Once you are inside through the socket login, switch root to password authentication and set the password.

sudo mariadb
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

2Fix

Change root to mysql_native_password and set a password. Scripts that relied on passwordless root will stop working, so keep the password in the [client] group of a private option file.

ALTER USER root@localhost IDENTIFIED VIA mysql_native_password;
SET PASSWORD = PASSWORD('choose-a-strong-password');

3Verify

Connect with the new password.

mariadb -u root -p

Confirmed by mariadb.com

Did this fix it?

Cause 3 of 3

The password was set and then lost

1Check

Confirm you cannot get in by either route above before you reset anything.

mariadb -u root -p

2Fix

Stop the server, start it by hand with --skip-grant-tables, reload the privilege tables, set a new password, then stop it and start it normally. The option gives every user full access, so do this only while nobody else can connect.

mariadbd --skip-grant-tables
# in a second terminal:
mariadb -u root
FLUSH PRIVILEGES;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('choose-a-strong-password');

3Verify

After a normal restart, log in with the new password.

mariadb -u root -p

Confirmed by mariadb.com

Did this fix it?

How do I stop it coming back?

  • Use sudo mariadb for admin work and create a named admin user for applications instead of using root.
  • Keep the root password in a private option file with mode 600, not in shell history.
  • Never leave the server running with --skip-grant-tables after a reset.

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

mysql_secure_installation cannot set the root password on Ubuntu

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

On Ubuntu the packaged MySQL root account uses auth_socket: you enter MySQL with sudo mysql, and no password is stored, so the tool has no password to check.

2 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.