How-to

Fix "Access Denied for User 'root'@'localhost'"

On a fresh Ubuntu or Debian install, this error rarely means a wrong password. It usually means root is set to log in through the system account, not a password.

What does the error tell me?

The database server received the login and rejected it. The number matters. ERROR 1698 (28000) appears when root is configured for socket authentication and you tried a password, or tried as the wrong system user. ERROR 1045 (28000) with "(using password: YES)" means a password check ran and failed. Both say Access denied for user 'root'@'localhost', so read the number first.

Why does a fresh install refuse root?

Because the packages on Ubuntu and Debian set up root to log in through the operating system account rather than a database password. MariaDB on Debian 13, Debian 12 and Ubuntu 24.04 uses the unix_socket plugin for root; MySQL 8.0 on Ubuntu uses auth_socket. The rule is simple: if you are the Linux root user, you may log in as the database root user, with no password. If you are anyone else, you may not. The fix for most people is one word:

sudo mariadb          # or: sudo mysql

This is more secure than a password: there is no database root password to leak or guess, and only people with sudo on the machine can reach full database control.

How do I check which method root uses?

-- MariaDB
SELECT user, host, JSON_VALUE(priv, '$.plugin') AS plugin FROM mysql.global_priv WHERE user = 'root';
-- MySQL 8
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

Run these after sudo mariadb or sudo mysql. unix_socket or auth_socket confirms socket login; mysql_native_password or caching_sha2_password means a password is expected.

My application needs to connect; should it use root?

No. Create a dedicated account with rights on its own database only. That is better security and avoids the socket problem entirely, because the app user has a normal password:

CREATE DATABASE shop CHARACTER SET utf8mb4;
CREATE USER 'shopapp'@'localhost' IDENTIFIED BY 'long-random-secret';
GRANT ALL PRIVILEGES ON shop.* TO 'shopapp'@'localhost';

Generate the secret with openssl rand -base64 30 and store it in the app's config with mode 600. Fine-grained grants are covered in MariaDB users and grants.

Can I give root a password as well?

On MariaDB you can allow both methods, so sudo mariadb keeps working and tools that insist on a password also connect:

ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('another-long-secret');

On MySQL 8, switching root to a password replaces socket login: ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'another-long-secret';. Keep socket login unless a tool truly needs root with a password.

How do I reset a forgotten root password?

Restart the server without the privilege system, set a new password, then restart normally. Do this in a quiet window, since the database is open to any local connection while grants are skipped:

sudo systemctl stop mariadb
sudo mysqld_safe --skip-grant-tables --skip-networking &
sleep 5; mariadb -u root
-- inside the client:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED VIA unix_socket;
EXIT;
sudo mysqladmin shutdown
sudo systemctl start mariadb

FLUSH PRIVILEGES must come first; without it, ALTER USER fails while grants are skipped. --skip-networking keeps the open door limited to local sockets. For MySQL the service is mysql on Ubuntu and mysqld on Rocky Linux and AlmaLinux.

What if the error names a host other than localhost?

Then the client connected over TCP, and MySQL matched the account by the connecting address. 'root'@'127.0.0.1' and 'root'@'localhost' are different accounts: localhost means the Unix socket, 127.0.0.1 means TCP. Errors naming a remote address mean that account does not exist for that host, which is usually a good thing for root. Remote access belongs to application accounts with a specific host, never to root with %.

Quick reference

SymptomFix
ERROR 1698 as a normal userUse sudo mariadb or sudo mysql
ERROR 1045, password forgottenReset with skip-grant-tables
Application cannot log in as rootCreate a dedicated app account
Works on socket, fails on 127.0.0.1The TCP account differs; grant the right host

Keep Reading

MariaDB users and grants

Create MariaDB accounts with the least privilege they need, choose host patterns, add roles, require TLS and check grants.

Open

MySQL servers

Give InnoDB a buffer pool that holds your working set, NVMe for the redo log and no hypervisor between query and disk.

Open

Database servers

PostgreSQL, MongoDB, ClickHouse and other engines on machines picked by working set, write rate and core count.

Open

The sudo command

How sudo decides who may run what, the flags you use daily, safe sudoers rules, logging and the habits that keep root powers contained.

Open

FAQ

Quick Answers

Root uses socket authentication by default. Log in with sudo mysql or sudo mariadb without a password.

1698 usually means socket authentication rejected you; 1045 means a password check failed.

No. Create a dedicated user with rights only on the app's database.

Start the server with --skip-grant-tables and --skip-networking, run FLUSH PRIVILEGES, then ALTER USER and restart normally.

No. localhost means the Unix socket; 127.0.0.1 is a TCP connection matched to a different account entry.