ERROR 1698 (28000): Access denied for user 'root'@'localhost' usually appears on a fresh Ubuntu or Debian server, right after you install MySQL or MariaDB and try mysql -u root -p. The account exists and nothing is broken. root@localhost simply doesn't use a password. It checks which operating-system user is running the client, and you are not root.
Why 1698 and not 1045
Both errors say "Access denied", but they come from different places. Error 1698 (ER_ACCESS_DENIED_NO_PASSWORD_ERROR) has no "(using password: YES/NO)" suffix (error reference). MySQL uses it when the authentication plugin reports that a password was never part of the exchange (sql_authentication.cc). The auth_socket plugin does exactly that (auth_socket.cc). If you see "(using password: YES)", you have a different problem, covered in our guide to error 1045.
$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
auth_socket accepts only Unix socket connections. It reads the client's OS user from the socket with SO_PEERCRED and lets you in if that name matches the MySQL user name, or the name stored in the account's authentication string (socket authentication). Whatever you type at the password prompt is ignored. That is why "I typed the right password" does not help.
On Ubuntu, the mysql-server package sets up 'root'@'localhost' this way. Ubuntu's own docs say to log in with sudo mysql, and that no password is needed because root authenticates with auth_socket (Ubuntu Server docs). MariaDB 10.4 and later do something similar: the root@localhost account created by mariadb-install-db tries unix_socket first, then a password that is deliberately set to an invalid value (MariaDB authentication).
Step 1: get in and look
$ sudo mysql
mysql> SELECT user, host, plugin FROM mysql.user WHERE user = 'root';
+------+-----------+-------------+
| user | host | plugin |
+------+-----------+-------------+
| root | localhost | auth_socket |
+------+-----------+-------------+
If plugin says auth_socket (MySQL) or unix_socket (MariaDB), you have found the cause. Being root on the OS is the password.
127.0.0.1 is not localhost here. For MySQL clients on Unix, the host name localhost means a socket file connection, while 127.0.0.1 means TCP (transport protocols). auth_socket refuses anything that is not a socket connection (auth_socket.cc). So sudo mysql -h 127.0.0.1 -u root fails, and so does every application, GUI or container that connects as root over TCP. If the socket itself is the problem, see our article on "Can't connect through socket".
Fixes, from best to worst
- Use
sudo mysqlfor administration and leave root alone. Socket login has no password to leak, brute-force or rotate. In our experience it is the safest default for a single server. - Create a separate admin account with a password for tools that cannot use the socket: GUI clients, remote administration over an SSH tunnel, monitoring.
- Create an application account. If an application is trying to log in as root, the real fix is a dedicated account with only the privileges it needs, limited to the hosts it connects from.
- Switch root to a password only if you must. In MySQL 8.4 the default plugin is
caching_sha2_password(caching_sha2_password). Once you change it,sudo mysqlwithout a password stops working, and any scripts that relied on it will break.
-- keep root on auth_socket, add a separate admin account with a password
CREATE USER 'dba'@'localhost' IDENTIFIED BY 'long-random-password';
GRANT ALL PRIVILEGES ON *.* TO 'dba'@'localhost' WITH GRANT OPTION;
-- an application account: never root, only what it needs
CREATE USER 'app'@'10.0.%' IDENTIFIED BY 'another-long-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'10.0.%';
-- if root really must use a password (MySQL 8.4 default plugin)
ALTER USER 'root'@'localhost'
IDENTIFIED WITH caching_sha2_password BY 'long-random-password';
-- a socket-only account that two OS users can use: ops and deploy
CREATE USER 'ops'@'localhost' IDENTIFIED WITH auth_socket AS 'deploy';
The AS clause of auth_socket is useful for deploy users: the account above accepts both the OS user ops and the OS user deploy over the socket, without a password (socket authentication).
MariaDB: keep both methods
On MariaDB you do not have to choose. The account can keep unix_socket and get a real password as a second method. To authenticate with a password, you must first set one with SET PASSWORD, because the initial value is invalid (MariaDB authentication).
-- MariaDB 10.4+: root@localhost is created like this
CREATE USER root@localhost
IDENTIFIED VIA unix_socket OR mysql_native_password USING 'invalid';
-- keep the socket login and give the password method a real password
SET PASSWORD FOR root@localhost = PASSWORD('long-random-password');
What changes on large systems
On one server, 1698 is a five-minute surprise. On a fleet it usually means each server was set up a little differently.
- The same "root" behaves differently everywhere. A distribution package uses socket authentication for root. The official Docker image sets a root password from
MYSQL_ROOT_PASSWORD(Docker image docs). A server installed from upstream packages and configured by hand may do neither. Scripts written on one type of host fail on another. Audit plugin usage per server, and decide on one model. - Automation should not depend on root's password. Run configuration management on the host as OS root through the socket, or give it its own admin account. In our experience, turning root into a password account on every server so that one playbook works creates a shared secret that is hard to rotate.
- Keep secrets out of command lines. Cron jobs and backup scripts often pass
-p'secret', which other local users may be able to see in the process list. Use socket authentication for local jobs, or a login path stored withmysql_config_editor, which is obfuscated (not encrypted) in~/.mylogin.cnf(mysql_config_editor). - Replicas and clones copy accounts. Statements like
CREATE USERandALTER USERreplicate, and physical backups carry themysqlschema with them, so a root changed on the primary usually changes everywhere. Make account changes in code and in one place, not by hand on whichever server failed. - Treat a sudden wave of 1698 as an incident signal. After an upgrade or rebuild, a burst of 1698 errors in the error log usually means a tool lost its old root login. In our experience, a steady trickle instead points to an application or GUI that still tries root over TCP.
-- which accounts use which plugin, on every server in the fleet
SELECT plugin, COUNT(*) AS accounts,
GROUP_CONCAT(CONCAT(user, '@', host) ORDER BY user SEPARATOR ', ') AS who
FROM mysql.user
GROUP BY plugin;
# store credentials for scripts in an obfuscated ~/.mylogin.cnf
mysql_config_editor set --login-path=backup --user=backup_user --password
mysql --login-path=backup -e 'SELECT 1'
Key takeaways
- Error 1698 for root@localhost almost always means socket authentication (
auth_socketorunix_socket), not a wrong password. - Log in with
sudo mysqland checkSELECT user, host, plugin FROM mysql.user. - Socket authentication only works over the Unix socket:
localhostworks,127.0.0.1does not. - Prefer a separate admin account and dedicated application accounts over changing root.
- On MariaDB, add a password as a second method and keep socket login.
- Across a fleet, audit plugins per server and keep credentials out of command lines.