Locked out of a fresh MariaDB install as root on Mac or Linux
ERROR 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.
Fix it fast
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 mariadb2Match what you see, then jump to its fix
- 1
Access 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
The same error with -p, and you never set a password for rootroot has no usable password yetFix - 3
Password login fails and the socket login is not available eitherThe password was set and then lostFix
- 1
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:
mariadb3Verify
The prompt opens without asking for a password.
mariadb -e "SELECT CURRENT_USER()"Confirmed by mariadb.com
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 -pConfirmed by mariadb.com
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 -p2Fix
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 -pConfirmed by mariadb.com
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:
- 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.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
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 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 fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
An Aria table fails with 'Read page with wrong checksum' (error 1030, code 176)
ERROR 1030 (HY000): Got error 176 "Read page with wrong checksum" from storage engine Aria
Code 176 means MariaDB's Aria engine read a data or index page whose checksum does not match, so that table file is damaged.
3 causes, each with a fixDatabases
The client cannot open a TCP connection to MariaDB or MySQL (error 2003)
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Error 2003 means the client tried a TCP connection to the host and port and nobody answered.
4 causes, each with a fix