Home > Database > Mysql Tutorial > Why Am I Getting MySQL Error 1698 (Access Denied) and How Can I Fix It?

Why Am I Getting MySQL Error 1698 (Access Denied) and How Can I Fix It?

Barbara Streisand
Release: 2024-12-25 13:12:12
Original
469 people have browsed it

Why Am I Getting MySQL Error 1698 (Access Denied) and How Can I Fix It?

Handling Error 1698 During MySQL Login

Users may encounter ERROR 1698 (28000): Access denied for user 'root'@'localhost' when trying to log into the MySQL database with the root user. This error is often encountered on systems like Ubuntu, where MySQL utilizes the Unix auth_socket plugin for authentication by default.

Background: The auth_socket plugin relies on system user credentials for authentication. By querying the mysql.user table, you can verify that the root user is registered with this plugin:

SELECT User, Host, plugin FROM mysql.user;
Copy after login

Possible Solutions:

Option 1: Configure root User to Use mysql_native_password Plugin
This method involves setting the root user to use the more traditional mysql_native_password plugin:

UPDATE user SET plugin='mysql_native_password' WHERE User='root';
FLUSH PRIVILEGES;
sudo service mysql restart
Copy after login

Option 2: Create a New Database User (Recommended)
An alternative approach is to create a new database user using your system username. This provides a more manageable and secure option:

CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY 'YOUR_PASSWD';
GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
FLUSH PRIVILEGES;
sudo service mysql restart
Copy after login

Note that with Option 2, you will connect to MySQL using your system username:

mysql -u YOUR_SYSTEM_USER
Copy after login

Additional Considerations:

  • In MySQL versions 8.x.x, the auth_socket plugin may have been replaced by caching_sha2_password. Consult MySQL documentation for the appropriate authentication plugin and login method.
  • In Debian 9 (Stretch), the auth_socket plugin is renamed to unix_socket. Adjust the SQL command accordingly.

The above is the detailed content of Why Am I Getting MySQL Error 1698 (Access Denied) and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template