JDBC / MYSQL:java.sql.SQLException:用户“root”@“localhost”的访问被拒绝(使用密码:否)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8147627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:42:20  来源:igfitidea点击:

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

mysqljdbc

提问by user994165

With this code

有了这个代码

String url = "jdbc:mysql://localhost/mysql";
Connection con = DriverManager.getConnection(url, "root", "");

I get the following exception.

我得到以下异常。

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MyTest1.main(MyTest1.java:29)

I logged into the database using the command tool with root and no password. I was able to telnet to the database and I get this message

我使用命令工具以 root 身份登录数据库,没有密码。我能够 telnet 到数据库并收到此消息

N
 5.5.17-log
           7{*Zj%u??§|tR}=Vz'L,6imysql_native_password"

采纳答案by user994165

It turned out to be some problem with my MYSQL installation. I uninstalled the AMP package I was using and installed XAMPP instead and it works.

结果是我的 MYSQL 安装有问题。我卸载了我正在使用的 AMP 包并安装了 XAMPP,它可以工作。

回答by Nallamachu

Even I also faced the same problem. But, in my application, I did one mistake like I forgot to give password. You can try to manually login to your MySQL and check what is the username and password you are using to log in. That same details you can use in the URL. Better to give database names also. As like below For MySQL:

甚至我也面临同样的问题。但是,在我的应用程序中,我犯了一个错误,例如忘记提供密码。您可以尝试手动登录到您的 MySQL 并检查您用于登录的用户名和密码是什么。您可以在 URL 中使用相同的详细信息。最好也给出数据库名称。如下对于 MySQL:

String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");

In the above, you forgot to give the port number of MySQL. You can check once your MySQL has a port number or not. By default, MySQL port number is 3306. Better to create and use your own database.

在上面,您忘记提供 MySQL 的端口号。您可以检查您的 MySQL 是否有端口号。默认情况下,MySQL 端口号为 3306。最好创建和使用您自己的数据库。

If the above one is not working try for GRANT all privileges to user root on your user database. To grant all privileges below is the query,

如果上述方法不起作用,请尝试将所有权限授予用户数据库上的 root 用户。授予以下所有权限是查询,

GRANT ALL PRIVILEGES ON mysql.* TO 'root'@'localhost' WITH GRANT OPTION;

or else

要不然

GRANT ALL PRIVILEGES ON mysql.* TO 'root'@'localhost' IDENTIFIED BY 'root';

回答by Gustav Bertram

'root'@'localhost'is different from 'root'@'%'. It may still be a permissions issue.

'root'@'localhost'不同于'root'@'%'. 它可能仍然是一个权限问题。

I suggest that you log in through the console, create a 'test'@'%' user with a password, and give it all permissions. You can learn how to do that here.

我建议您通过控制台登录,创建一个带有密码的 'test'@'%' 用户,并赋予其所有权限。你可以在这里学习如何做到这一点