Fix MySQL ERROR 1698: Access denied for user 'root'@'localhost'
Ubuntu mysql -u root fails with ERROR 1698 because root@localhost uses auth_socket, not a password. Log in with sudo mysql and create a separate admin user.
ERROR 1698 (28000): Access denied for user 'root'@'localhost'On Ubuntu's packaged MySQL, root@localhost uses the auth_socket plugin instead of a password, so mysql -u root from any shell that is not the OS root user fails with this error. Get in with sudo mysql and create a separate account to work with.
The error number matters. 1045 ends with (using password: YES/NO) and means the password check failed. 1698 (ER_ACCESS_DENIED_NO_PASSWORD_ERROR) has no such tail, and no password you type will change the result. 1045 is covered in Fix MySQL ERROR 1045 Access denied for user (using password: YES).
[1] Check which OS user you are. From a normal user shell, root is refused with or without a password.
$ id -unappuser$ mysql -u rootERROR 1698 (28000): Access denied for user 'root'@'localhost'$ mysql -u root -p'<password>'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1698 (28000): Access denied for user 'root'@'localhost'[2] Log in with sudo mysql. As the OS root user, your name matches the MySQL root account and you get in without a password.
$ sudo mysql -e "SELECT USER(), CURRENT_USER();"USER() CURRENT_USER()root@localhost root@localhost[3] Confirm root's authentication plugin. If plugin is auth_socket, this is your case.
# mysql -e "SELECT user,host,plugin FROM mysql.user WHERE user='root';"user host pluginroot localhost auth_socketThe MySQL manual says auth_socket only accepts clients that connect through the Unix socket file, and only when the OS user name obtained via SO_PEERCRED matches the MySQL user name (Socket Pluggable Authentication).
# mysql -u root -h 127.0.0.1 -e "SELECT 1;"ERROR 1698 (28000): Access denied for user 'root'@'localhost'[4] Create a separate admin account that uses a password. Leave root on auth_socket and give people and tools their own account. Grant only what is needed; for an application, grant its database instead of ALL PRIVILEGES ON *.*.
# mysql -e "CREATE USER 'admin'@'localhost' IDENTIFIED BY '<password>'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;"$ mysql -u admin -p'<password>' -e 'SELECT CURRENT_USER();'mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER()admin@localhostThe normal user shell now gets in with a password. Because this account uses password authentication, the check is the same over the socket or TCP.
[5] If root itself must use a password, switch its plugin. After this, the passwordless sudo mysql route stops working.
# mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<password>';"# mysql -u root -e "SELECT 1;"ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)$ mysql -u root -p'<password>' -e 'SELECT CURRENT_USER();'mysql: [Warning] Using a password on the command line interface can be insecure.CURRENT_USER()root@localhostFrom now on even the OS root shell needs the password, and leaving it out gives 1045 instead. Whether package maintenance tasks expect passwordless root access was not confirmed. Ubuntu's package keeps a separate /etc/mysql/debian.cnf (readable by root only); check which account it uses before you switch.
# mysql -u root -p'<password>' -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;"mysql: [Warning] Using a password on the command line interface can be insecure.# mysql -u root -e "SELECT user,host,plugin FROM mysql.user WHERE user='root';"user host pluginroot localhost auth_socket[6] Keep passwords off the command line in scripts. The manual warns that a password given as -p<password> can be visible to other users through ps on some systems (End-User Guidelines for Password Security). Type -p alone and enter it at the prompt, or put it in the [client] section of ~/.my.cnf and chmod 600 that file.
If you now get in but hit Too many connections, see MySQL ERROR 1040 Too many connections. If even sudo mysql fails with ERROR 2002 … through socket, the problem is the server or the socket path, before authentication — see ERROR 2002: Can't connect to local MySQL server through socket.
Analyze your MySQL slow log log in the browser ↗ — nothing is uploaded.