Home  >  Article  >  Database  >  Why am I getting 'Access denied for user ''@'localhost' (using password: NO)' when connecting to MySQL?

Why am I getting 'Access denied for user ''@'localhost' (using password: NO)' when connecting to MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-11-13 15:40:02604browse

Why am I getting

Exception: "Access denied for user ''@'localhost' (using password: NO)"

In this code, an SQLException is thrown when trying to establish a connection to a MySQL database. The error message indicates that the user trying to connect to the database is not authorized.

Specifically, the code in the MainApp class attempts to connect to the database using the DriverManager.getConnection method, providing the URL, username, and password. However, the error message suggests that the user with an empty username is trying to connect using the password "NO," which is incorrect.

Solution:

To resolve this issue, verify the following:

  1. Ensure Correct Credentials: Double-check that you are using the correct username and password to connect to the MySQL database. In the code, it appears that the username is empty, which is likely incorrect.
  2. Check MySQL Workbench: In MySQL Workbench, ensure that the root user has been created and has the necessary privileges to access the database. Grant the user the appropriate permissions, such as "ALL PRIVILEGES."
  3. Run the Correct Class: The error message indicates that the "com.mysql.cj.jdbc.admin.TimezoneDump" class is running instead of the MainApp class. To fix this, right-click on the MainApp.java file in Eclipse and select "Run As" -> "Java Application."

Once you have verified these steps, the code should be able to establish a successful connection to the MySQL database without the "Access denied" error.

The above is the detailed content of Why am I getting 'Access denied for user ''@'localhost' (using password: NO)' when connecting to MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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