How to fix error "SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'"
Martijn | Developer
If you're reading this, you might have encountered the following issue in your Laravel application when using WSL 2 with Laravel Valet.
SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'
Tip
Didn't install WSL 2 yet? Make sure to check out this article first.
In MySQL, plugins determine how users log in. Sometimes, the setup switches the plugin to auth_socket
.
This plugin lets you log in only if you're the system's root user, without a password.
If you encounter issues even with your root user, it's best to use mysql_native_password
(the password-based method).
Below we’ll show you how to switch it back to fix the error.
Step 1 - Open MySQL
First, open MySQL by running the following command. You might need to enter the password for your WSL user.
sudo mysql
Step 2 - Check the currently used plugin
After that, run the following command to check the plugin that is currently used.
SELECT user, host, plugin FROM mysql.user WHERE user='root';
You will see a table like the one below.
mysql> SELECT user, host, plugin FROM mysql.user WHERE user='root';
+------+-----------+-------------+
| user | host | plugin |
+------+-----------+-------------+
| root | localhost | auth_socket |
+------+-----------+-------------+
1 row in set (0.00 sec)
As you can see, plugin auth_socket
is being used.
Let's change it back to mysql_native_password
.
Step 3 - Change the plugin
You can change the plugin and flush the priviliges running the following commands. Please note that the first command below will remove your current password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;
Tip
If you use a password or if you want to set or update the password, you need to specify it.
Replace 'new_password' with the password that you want to use.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
You will see the following.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
It is normal to see 0 rows affected
.
This is because MySQL updated the existing root user’s authentication method without adding or removing any rows.
Step 4 - Verify plugin change
Now that we have executed the previous commands, let's verify the change by running the same command as in step 2.
SELECT user, host, plugin FROM mysql.user WHERE user='root';
You will see the same table, but with a different plugin.
mysql> SELECT user, host, plugin FROM mysql.user WHERE user='root';
+------+-----------+-----------------------+
| user | host | plugin |
+------+-----------+-----------------------+
| root | localhost | mysql_native_password |
+------+-----------+-----------------------+
1 row in set (0.00 sec)
With the plugin change confirmed, the error should no longer appear.