gpt4 book ai didi

java.sql.SQLException : Access denied for user '' @'localhost' (using password: NO)

转载 作者:行者123 更新时间:2023-12-01 16:28:43 27 4
gpt4 key购买 nike

因此,当我尝试从 Java 代码连接到 MySQL 数据库时,出现错误 java.sql.SQLException: Access returned for user ''@'localhost' (using password: NO) 。我能找到的所有内容根本没有解决问题,而且代码非常基本。之前在项目中用过,发现很多教程都是这样的。首先是相关代码:

//Inside class that manages SQLConnection
public SQLTransceiver(String url){
this.connectionURL = url;
this.reconnect();
}

public void reconnect(){
try {
Connection connection = DriverManager.getConnection(this.connectionURL); //Where the error occurs
this.currentConnection = connection;
} catch (SQLException e) {
e.printStackTrace();
}
}

//Inside main class I call this for testing purpose
String s = "jdbc:mysql://localhost:3306/tableName" +
"?user=user" +
"&password=thePassword" +
"&serverTimezone=MET";

DataTransreciever transreciever = new SQLTransceiver(s);

注意:我可以完美地登录 MySQL 工作台中的数据库并执行从创建/更改表到插入等所有操作。

整个 Stacktrace 看起来像这样:

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
at main.data.SQLTransceiver.<init>(SQLTransceiver.java:20)
at main.ui.toDoPane.ToDoPane.<init>(ToDoPane.java:208)
at main.ui.Window.start(Window.java:40)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

那么我做了什么来解决这个问题:这让我想知道为什么它没有说明哪个用户被拒绝,注意到它声明“用户''@'localhost'被拒绝。所以我确实输入了错误的密码,并且出现了这个错误:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
at main.data.SQLTransceiver.<init>(SQLTransceiver.java:26)
at main.Main.main(Main.java:29)

所以看起来它在某个时候确实检查了用户和密码。如果用户和密码正确,它只会给出第一个错误。

因此,我进行了一些查找,并多次得到了针对此问题所述的解决方案(尽管通常它确实指出“使用密码:是”而不是“否”):

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

它指出我应该运行以下语句:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;

问题是我无法运行它。当我尝试在 MySQL Workbench 中运行它时,出现以下错误:

0   5   01:12:32    GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION    Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'password' WITH GRANT OPTION' at line 1  0.000 sec

此外,我尝试完全删除 MySQL 并重新安装它。这没有帮助。

免责声明:出于隐私原因,密码和用户已更改。就错误而言,使用连接字符串和属性类调用 DriverManager 并没有什么区别,我确实尝试过这样做。抱歉,我的英语比较基础,可能还很糟糕。我正在使用带有 InnoDB 引擎的 MySQL Server 8.0.20 Community。 J/Connector 和 MySQL Notifier 适用于同一版本。我使用从 https://dev.mysql.com/downloads/mysql/ 下载的 Windows Installer 安装了所有内容(包括工作台)我使用的是 Windows 10。

当我删除“serverTimezone”参数时,出现以下错误:

java.sql.SQLException: The server time zone value 'Mitteleurop�ische Sommerzeit' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
at main.data.SQLTransceiver.<init>(SQLTransceiver.java:26)
at main.Main.main(Main.java:28)

我不知道这是否有帮助,但这是可能的。

提前非常感谢:)

最佳答案

所以至少解决问题的是再次重新安装 MySQL Server。

不同之处在于,这次我确保为每个组件(服务器、Connector/J、Notifier 和 Workbench)选择"new"密码加密(“caching_sha2_password”)。

尽管我不明白这会如何导致这样的错误,或者这是否是它修复的错误。

感谢所有竭尽全力提供帮助的人:D

关于java.sql.SQLException : Access denied for user '' @'localhost' (using password: NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62096031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com