当前位置: 首页 > 知识库问答 >
问题:

java.sql.sqlexception:拒绝用户''@'localhost'的访问(使用密码:NO)

华心思
2023-03-14

因此,当我尝试从Java代码连接到MySQL数据库时,我收到错误Java.sql.SQLException:用户''@'localhost'(使用密码: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 workbench中的DB中,并在那里做所有的事情,从创建/更改表到插入等等。

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)

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

所以我做了一些查找,得到了这个问题的解决方案(尽管它经常在“no”后面加上“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,然后重新安装它。这没用。

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)

提前非常感谢:)

共有1个答案

云德辉
2023-03-14

因此,至少修复了这个问题,就是重新安装MySQL Server。

不同的是,这次我确保为每个组件(服务器、连接器/J、通知器和工作台)选择“新的”密码加密(“caching_sha2_password”)。

总之,我不明白这怎么会导致这样的错误,或者如果这是错误,它修复了它。

感谢所有在那里尽最大努力提供帮助的人

 类似资料: