Incident Response

MySQL ERROR 1698 (28000): Access denied for user 'root'@'localhost'

On Ubuntu and Debian-style installs, root@localhost usually has no password at all. It trusts the operating-system user instead. Here is how to tell, how to log in, and how to set up admin and application accounts so you never need to "fix" root.

· 11 min read · AG Data Team

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

  1. Use sudo mysql for 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.
  2. Create a separate admin account with a password for tools that cannot use the socket: GUI clients, remote administration over an SSH tunnel, monitoring.
  3. 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.
  4. 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 mysql without 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.

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

MySQL MariaDB Errors Security
Work with us

Access and accounts in a mess?

We audit MySQL and MariaDB accounts, authentication plugins and privileges across fleets, and set up admin and application access that survives upgrades.

Get in touch More from the blog