Skip to content

ERROR 1698: Access Denied for User 'root'@'localhost' on Ubuntu

On Ubuntu and Debian, MySQL's root account logs in with auth_socket, so sudo mysql works while your app, Workbench and phpMyAdmin get ERROR 1698. Why no password will ever work over TCP, and the fix that leaves root alone.

mysql-error-1698 access-denied auth-socket mysql-auth linux mysql mysql-errors
Gopi Gorantala
Reading Progress

On This Page

sudo mysql drops you straight into a prompt. It doesn't even ask for a password. The merchant onboarding service on the same Ubuntu box, using the same user name, gets refused. And mysql -u root -p from your normal shell, with the password you set during the install (or think you set), prints ERROR 1698 (28000). Same server, same account, opposite results. The password has nothing to do with either of them.

What the app got, and what sudo got

The CLI version, from a login shell that isn't root:

$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

1698 is the server error ER_ACCESS_DENIED_NO_PASSWORD_ERROR, SQLSTATE 28000, message Access denied for user '%s'@'%s'. Now look at what isn't there. Every ERROR 1045 ends in (using password: YES) or (using password: NO). 1698 has no tail at all, and that missing tail is most of the diagnosis. (If reading the number, SQLSTATE and message apart is new, this post covers it.)

The same refusal through other clients:

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
mysqli::real_connect(): (HY000/1698): Access denied for user 'root'@'localhost'
pymysql.err.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
java.sql.SQLException: Access denied for user 'root'@'localhost'

PDO under Laravel, phpMyAdmin, PyMySQL, Connector/J. I built these from the message format, not a live server (the lab explains why). Connector/J leaves the number out of the text, so log getErrorCode(). It fails at connect time, from the first minute after apt install mysql-server.

1698 or 1045? Check the plugin before the password

The first time I hit this I reset root's password. Twice. Nothing changed: the account has no password to check. Ubuntu's mysql-server package creates 'root'@'localhost' with the auth_socket plugin. An authentication plugin is the piece of the server that decides how an account proves who it is, and this one ignores passwords completely: it asks the Linux kernel which OS user is on the other end of the socket.

So check the plugin first. On the database host:

sudo mysql -e "SELECT user, host, plugin FROM mysql.user WHERE user = 'root';"
+------+-----------+-------------+
| user | host      | plugin      |
+------+-----------+-------------+
| root | localhost | auth_socket |
+------+-----------+-------------+

That's the shape you'll get, not a capture from my own run. If the plugin column says auth_socket, you're in the right place. On MariaDB it says unix_socket, and SHOW CREATE USER root@localhost usually shows a second method next to it.

Then the quick discriminators:

  • sudo mysql works and mysql -u root -p doesn't: OS user root versus OS user you. Socket auth.
  • mysql -u root -p -h 127.0.0.1 fails too: that's TCP, and socket auth never accepts TCP.
  • You get 1045 with a using password tail instead: a password plugin did check a password, and it was wrong. That's the 1045 article.
  • Host '…' is not allowed to connect: no account matched your host at all, see ERROR 1130.
  • Can't connect to local MySQL server through socket: you never reached the server, see ERROR 2002.

Still unsure? The comparison post walks how far each connection error got.

Watch the kernel answer the question MySQL asks

I couldn't run a MySQL server for this one (no Docker daemon, no route to the package mirrors), and I won't invent a transcript. So I rebuilt the one check that matters: the SO_PEERCRED lookup. SO_PEERCRED is a Linux socket option that returns the process ID, user ID and group ID of whoever is on the other end of a Unix socket. The script listens on a Unix socket and on TCP port 3307, asks the kernel who connected, and applies auth_socket's rule. It isn't MySQL. It's the question MySQL asks.

Python 3, standard library only, run it as any user:

# Mimics what MySQL's auth_socket plugin checks. Not MySQL. A teaching lab.
import os, pwd, socket, struct, threading, time
SOCK = "/tmp/lab-peercred.sock"

def peer(conn):
    raw = conn.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, struct.calcsize("3i"))
    return struct.unpack("3i", raw)

def handle(conn, proto, mysql_user):
    pid, uid, gid = peer(conn)  # the kernel, not the client, supplies these
    try: osname = pwd.getpwuid(uid).pw_name
    except KeyError: osname = "?"
    if proto != "socket":
        verdict = "REFUSE (not a Unix socket connection)"
    elif osname != mysql_user:
        verdict = f"REFUSE (OS user {osname!r} != MySQL user {mysql_user!r})"
    else:
        verdict = "ACCEPT"
    print(f"[{proto:6}] SO_PEERCRED pid={pid} uid={uid} gid={gid} os_user={osname!r} mysql_user={mysql_user!r} -> {verdict}")
    conn.close()

def serve(srv, proto, n):
    for _ in range(n):
        c, _ = srv.accept(); user = c.recv(64).decode().strip(); handle(c, proto, user)

if os.path.exists(SOCK): os.unlink(SOCK)
u = socket.socket(socket.AF_UNIX); u.bind(SOCK); u.listen(); os.chmod(SOCK, 0o777)
t = socket.socket(); t.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1); t.bind(("127.0.0.1", 3307)); t.listen()
th1 = threading.Thread(target=serve, args=(u, "socket", 2)); th2 = threading.Thread(target=serve, args=(t, "tcp", 1))
th1.start(); th2.start()

def client(kind, user):
    s = socket.socket(socket.AF_UNIX) if kind == "socket" else socket.socket()
    s.connect(SOCK if kind == "socket" else ("127.0.0.1", 3307)); s.sendall(user.encode()); time.sleep(0.2); s.close()

me = pwd.getpwuid(os.getuid()).pw_name
client("socket", me)       # like: sudo mysql  (OS user root, MySQL user root)
client("socket", "root")   # like: mysql -u root -p  from a normal login shell
client("tcp", me)          # like: JDBC / Workbench / mysql -h 127.0.0.1
th1.join(); th2.join(); os.unlink(SOCK)

Run as an ordinary user on an Ubuntu 22.04 VM (Python 3.10.12, kernel 6.8). The sandbox gave me a long random user name; I left it as printed:

[socket] SO_PEERCRED pid=12 uid=1463 gid=1463 os_user='rcw-01lg4rfrfr7vhuos46o9zb97' mysql_user='rcw-01lg4rfrfr7vhuos46o9zb97' -> ACCEPT
[socket] SO_PEERCRED pid=12 uid=1463 gid=1463 os_user='rcw-01lg4rfrfr7vhuos46o9zb97' mysql_user='root' -> REFUSE (OS user 'rcw-01lg4rfrfr7vhuos46o9zb97' != MySQL user 'root')
[tcp   ] SO_PEERCRED pid=0 uid=-1 gid=-1 os_user='?' mysql_user='rcw-01lg4rfrfr7vhuos46o9zb97' -> REFUSE (not a Unix socket connection)

And the first line again, this time as root in a container (Python 3.13.18, kernel 6.18), which is the sudo mysql case:

[socket] SO_PEERCRED pid=253 uid=0 gid=0 os_user='root' mysql_user='root' -> ACCEPT

Line two is you typing mysql -u root -p: you asked to be MySQL root, the kernel says you're Linux user 1463, and your password never enters the decision. Line three is every TCP client, from the app to Workbench to the SSH tunnel. Look at what the kernel hands back for TCP. pid=0 uid=-1. Nobody to recognise.

To see the real thing, use a VM you can throw away, never a shared box:

sudo apt-get install -y mysql-server      # 8.4.x on Ubuntu 26.04, 8.0.x on 24.04
sudo mysql -e "SELECT CURRENT_USER(), @@version;"
mysql -u root -p                          # any password, or none: ERROR 1698
mysql -u root -p -h 127.0.0.1             # same error, over TCP this time

You've reproduced it when you get 1698 with no using password tail. If you get 1045 instead, root on that box is a password account. That's what the official mysql Docker image creates, so a 1698 inside a container means someone changed root's plugin by hand. Tear down by deleting the VM; the script removes its own socket file.

The one-paragraph version

Root on a stock Ubuntu MySQL isn't a password account. It's an account that trusts the Linux user root, connecting through the local Unix socket file, and nobody else. Your app connects over TCP as Linux user www-data or onboarding or you. It fails twice over, and no password can rescue it.

Where ERROR 1698 (28000) comes from

Two things happen before that error is raised, and only the second one is unusual.

First, account matching, same as any login (user@host accounts has the long version): your user name plus the host you came from picks a row in mysql.user. A socket connection is localhost. A TCP connection from 127.0.0.1 on a default Ubuntu config also gets resolved back to localhost, which is why the error still names 'root'@'localhost' for your JDBC URL. Either way the row it lands on is root's.

Second, the server hands the login to whatever plugin that row names. For auth_socket, the MySQL 8.4 manual is short and blunt about the rules. The plugin reads SO_PEERCRED, so it only works where the OS supports it, Linux included. It compares the OS user name with the MySQL user name. If they differ, it checks whether the OS name matches the value stored in authentication_string, which you set with IDENTIFIED WITH auth_socket AS 'name'. And it refuses TCP connections outright, even when the names would have matched. There's no client-side half to this plugin. Your driver has nothing to send.

So why 1698 and not 1045? That part I had backwards for years. I read 1698 as "you didn't send a password". It means the opposite of a password problem: the password never mattered. In the server source, login_failed_error() in sql/auth/sql_authentication.cc picks the error. When the plugin reports that passwords don't apply to it, the server raises ER_ACCESS_DENIED_NO_PASSWORD_ERROR and drops the using password part. Every other plugin gets 1045 with YES or NO. auth_socket never takes a password, so it always lands in the first branch. The error number is the plugin telling you to stop typing.

Think of it as a door with a fingerprint reader. You can wave a keycard at it all night.

The dead end that cost me the most was the install itself. On Ubuntu 20.04 and 22.04, mysql_secure_installation used to ask for a root password and then loop on SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. (Launchpad bug 1980466). The fixed script skips that step and tells you auth_socket is in use. Either way, the password you remember typing during setup was never stored. I spent a good while trying passwords that didn't exist anywhere.

Ubuntu isn't being difficult, by the way. A root with no stored password can't be guessed over the network or leak from a config file. Good default. The mistake is pointing an application at it.

The change that actually works

Since the account checks the OS user, you have two honest moves: be that OS user, or use a different MySQL account. Which one depends on who's connecting.

Who's connecting What to do
You, doing admin work on the box sudo mysql, and leave root alone
An application its own account, scoped to its schema
Workbench, DBeaver or phpMyAdmin, including over an SSH tunnel a separate admin account with a password
A cron job or backup script on the same host a socket account mapped to that job's OS user
Something that insists on root with a password the last-resort change below, with its cost

For the app, run this as sudo mysql:

CREATE USER 'onboarding_svc'@'localhost' IDENTIFIED BY 'a-generated-secret';
GRANT SELECT, INSERT, UPDATE, DELETE ON onboarding.* TO 'onboarding_svc'@'localhost';

If the app runs on another machine, the host part should be that machine's address or subnet, not '%' (1130 explains why). New accounts get caching_sha2_password by default on 8.0 and later. An old driver may choke on it (plugin errors), and Connector/J over plain TCP will ask for public key retrieval. No FLUSH PRIVILEGES needed; CREATE USER and GRANT apply immediately.

For GUI tools, a human admin account:

CREATE USER 'dbadmin'@'localhost' IDENTIFIED BY 'another-generated-secret';
GRANT ALL PRIVILEGES ON *.* TO 'dbadmin'@'localhost' WITH GRANT OPTION;

Workbench's "Standard TCP/IP over SSH" lands on 127.0.0.1 on the server, so @'localhost' covers it and bind-address can stay on 127.0.0.1.

For a job on the box, my favourite option, because there's no password anywhere:

CREATE USER 'backup'@'localhost' IDENTIFIED WITH auth_socket;
GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, LOCK TABLES ON *.* TO 'backup'@'localhost';

Linux user backup runs mysqldump over the socket and gets in. Nothing else does. Check the grant list against your mysqldump flags and version.

The last resort is giving root a password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'a-long-generated-secret';

It costs you exactly what made root safe: it's guessable now, and the password will end up in someone's .my.cnf. On MariaDB you can keep both doors, which is the syntax MariaDB itself uses when it creates root: ALTER USER root@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('…');. Check /etc/mysql/debian.cnf first, since Debian's maintenance scripts may log in through that account.

What the forum threads recommend instead, and why I don't:

  • ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '…'. The most upvoted old answer. On MySQL 8.4, which is what Ubuntu 26.04 installs, that plugin is disabled by default, so unless someone switched it back on the statement fails with ERROR 1524 Plugin 'mysql_native_password' is not loaded. In 9.0 it's gone.
  • UPDATE mysql.user SET plugin = …. Editing grant tables by hand, then FLUSH PRIVILEGES. Use ALTER USER.
  • CREATE USER 'root'@'%', GRANT ALL, and bind-address = 0.0.0.0. Now there's a password-protected root reachable from the network. That's strictly worse than where you started.

How to not end up here again

No application should log in as root, on any database. Make the accounts part of provisioning, so a fresh box never has a window where somebody "temporarily" points the app at root:

-- accounts.sql, run by Ansible or cloud-init as: sudo mysql < accounts.sql
CREATE USER IF NOT EXISTS 'onboarding_svc'@'localhost' IDENTIFIED BY 'from-your-secret-store';
GRANT SELECT, INSERT, UPDATE, DELETE ON onboarding.* TO 'onboarding_svc'@'localhost';
CREATE USER IF NOT EXISTS 'backup'@'localhost' IDENTIFIED WITH auth_socket;

Running it through sudo mysql is the point: root gets used exactly as designed, and nothing downstream ever needs root's credentials.

Which versions behave how

  • Ubuntu 26.04 ships mysql-server 8.4 (8.4.11 in the archive when I checked), root on auth_socket, and mysql_native_password disabled.
  • Ubuntu 24.04 and 22.04 ship 8.0 with the same root setup. Upstream 8.0 is end of life, which is a separate reason to plan the move.
  • Debian installs MariaDB when you ask for default-mysql-server. Since MariaDB 10.4, root and mysql@localhost are created with unix_socket plus a password method set to 'invalid', so you see the same 1698.
  • The official mysql Docker image gives root a password. Anything else, don't assume: the plugin column settles it in one query.

What I checked for this one, in place of a long war story: the auth_socket page in the 8.4 manual, the branch in sql_authentication.cc that chooses 1698 over 1045, the 26.04 package version, the old mysql_secure_installation loop on Launchpad, and the SO_PEERCRED behaviour on two kernels. No MySQL server ran here.

The cheap test: a CI lint that fails the build when any datasource config says username: root, and a provisioning check that runs sudo mysql -N -e "SELECT user, plugin FROM mysql.user" and asserts your app account exists. Before upgrading, look for anything still on mysql_native_password.

What to remember

  • No using password in the message means the plugin doesn't take passwords. Stop resetting it.
  • auth_socket trusts the Linux user on the local socket. TCP clients, including your app on the same machine, can never pass it.
  • Leave root alone. Give the app its own account and your GUI its own admin account.
  • The mysql_native_password answers from 2018 fail with ERROR 1524 on Ubuntu 26.04.

Things people search for about 1698

How do I fix ERROR 1698 (28000): Access denied for user 'root'@'localhost'?

Use sudo mysql for admin work and a separate password account for everything else. Root on Ubuntu and Debian uses auth_socket or unix_socket, which only accepts Linux root over the local socket.

Why does sudo mysql work but mysql -u root -p doesn't?

sudo makes the client run as Linux user root, and the plugin compares that OS user with the MySQL user. From your own shell the kernel reports your user, the names don't match, and the password you typed is never consulted.

How do I log in to phpMyAdmin or MySQL Workbench as root on Ubuntu?

Not as that root: both connect over TCP, and auth_socket refuses TCP. Create 'dbadmin'@'localhost' with a password and ALL PRIVILEGES … WITH GRANT OPTION, and log in as that, SSH tunnel included.

Does MariaDB give the same ERROR 1698?

Yes. MariaDB 10.4+ creates root with unix_socket, and a failed socket check gives the same 1698. MariaDB lets one account accept either method, so IDENTIFIED VIA unix_socket OR mysql_native_password adds a password without breaking sudo mysql.

mysql-error-1698access-deniedauth-socketmysql-authlinuxmysqlmysql-errors

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments