What the message looks like
ERROR 1698 (28000): Access denied for user 'root'@'localhost'Common causes
- The account
'root'@'localhost'usesauth_socketorunix_socket. The password is never checked, only whether the OS user matches. - The client runs as a normal OS user without
sudo, for examplemysql -u root -pfrom a login shell. - A web application, PHP script or phpMyAdmin logs in as root. It runs as
www-dataor similar, so the socket check fails. - A password was set for root, but the authentication plugin was never changed, so the password has no effect.
- A backup or cron script runs as a non-root user and still uses the root database account.
Check
- Which plugin does root use?
sudo mysql -e "SELECT user, host, plugin FROM mysql.user WHERE user='root'" - Does the login work through the socket as OS root?
sudo mysql -e "SELECT CURRENT_USER(), VERSION()" - Which OS user runs the client?
id -un - Server version and flavor (MySQL or MariaDB)
mysql --version
Fix
- For interactive administration nothing needs to change:
sudo mysqllogs in as database root straight away. - For applications do not use root at all. Create a dedicated account:
CREATE USER 'app'@'localhost' IDENTIFIED BY 'strong-password';andGRANT ALL PRIVILEGES ON appdb.* TO 'app'@'localhost'; - If you need a separate admin account with a password, create one with
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;and leave root on the socket. This is safer than giving root a password. - If root really must use a password, MySQL:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';. From MySQL 8.4 the pluginmysql_native_passwordis disabled by default, so usecaching_sha2_passwordthere. MariaDB:ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('new-password'); - Afterwards test with
mysql -u root -pwithout sudo, and update scripts so that they use their own account.
Typical scenario
On a freshly built Ubuntu server mysql-server was installed with apt. While setting up WordPress the admin puts root and a password of their own choosing into wp-config.php, and the site shows a database error. On the console mysql -u root -p gives the same error 1698, while sudo mysql logs in at once without a password. The fix is not a new root password but a dedicated database user for WordPress.
FAQ
Is it a problem to leave root on auth_socket?
No, it is the safer default. There is no root password that could be guessed or leaked, and only the local system root can use the account. Applications should have their own users anyway.
Why does it work with sudo but not without?
With sudo the operating system user is root, which matches the account. Without it the OS user is someone else, the socket plugin refuses the login and the password is not even looked at.
Can phpMyAdmin work as root then?
Not with auth_socket, because the web server runs as www-data and not as system root. A separate admin user with a password for phpMyAdmin makes more sense than rebuilding the root account.
How do I tell whether my server is MySQL or MariaDB?
mysql --version says so, MariaDB shows MariaDB in the output. It matters because the syntax for the plugin differs (IDENTIFIED WITH on MySQL, IDENTIFIED VIA on MariaDB).
How is this different from error 1045?
With 1045 the password or the user is wrong. With 1698 the authentication plugin blocks the login: the account is protected by the socket and only accepts the matching operating system user.