Java.sql.SQLException: 用户'root@localhost'@'localhost'(使用密码:YES)被拒绝访问。

7
我将使用Eclipse连接MySQL数据库的Java代码。 代码:
import java.sql.*;
import java.io.*;

public class DbDemo {

    public static void main(String args[]) throws ClassNotFoundException, SQLException {

        String s;       
        String uname="root@localhost";
        String url="jdbc:mysql://localhost:3306/student";

        String password="Hsun123";

        int i;

        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Connection con=DriverManager.getConnection(url,uname,password);

            Statement st=con.createStatement();

            ResultSet rs=st.executeQuery("select * from student_detail");

            if(rs.next()) {

                i=rs.getInt(1);

                s=rs.getString(2);

                System.out.println(i+"/t"+s);
            }           

            rs.close();

            st.close();

            con.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

错误

java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4101)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1300)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
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 database.DbDemo.main(DbDemo.java:13)

我应该怎么做来解决我的问题?

root@localhost的密码正确吗? - Miro Hudak
3
我会尝试使用 String uname="root"; - Joachim Isaksson
10个回答

2

如果您正在使用Java从本地计算机连接远程MySQL服务器,请按照以下步骤操作。

错误: java.sql.SQLException: Access denied for user 'xxxxx'@'101.123.163.141' (using password: YES)

对于远程访问,可能是cPanel或其他应该授予您的本地IP地址的远程访问权限。

在上述错误消息中,“101.123.163.141”是本地计算机IP地址。因此,首先我们必须在cPanel->Remote MySQL®中提供远程访问。然后运行应用程序以进行连接。

Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
    "jdbc:mysql://www.xyz.com/abc","def","ghi");  
//here abc is database name, def is username and ghi        
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from employee");  
    while(rs.next())  
        System.out.println(rs.getInt(1)+" "+rs.getString(2)+"  
                      "+rs.getString(3));  
con.close();    

我希望这能解决你的问题。
谢谢。

谢谢,这对我很有帮助。我认为将百分号符号添加作为主机将允许所有IP。 - Nipun

2

使用以下方式代替:

 String uname="root@localhost";

使用:

String url="jdbc:mysql://localhost:3306/student";
String userName="root"
String password="Hsun123"
...
try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con=DriverManager.getConnection(url,username,password);
...

这应该可以正常工作(前提是您设置了有效的密码)。

有人可以删除我添加的那10个“M”吗?我做了一个4个字符的更改usernamename -> username,但它没有达到6个字符的限制。所以,我想通过添加额外的字符,然后在单独的编辑中将它们删除是绕过规则的聪明方法。但实际上这样行不通。对此我很抱歉。 - Austin A

2
以下查询和执行已经解决了我的问题:

sudo mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

mysql> FLUSH PRIVILEGES;


注意:请勿修改html标签。

1

嗯,我也遇到了同样的问题。我正在使用phpmyadmin。

我不得不进入phpmyadmin->users->root->Edit Privileges

然后在"Database-specific privileges"字段中,我必须为我的数据库提供权限。

在"Change password"字段中,我重新输入了我的密码(我不知道为什么要这样做,可能是一个可能的错误,因为我已经有一个密码)。

在"Login Information->Host"字段中,我填写了任意主机。

问题解决了。


0

试试这个...

String password="Hsun123"; 

而不是

String password=""; 

0

确保您在数据库中拥有具有正确密码的特权(即必要权限)用户帐户存在,或者根本不创建密码


0

选择一个像这样的密码

password = 'Hellobds123@S'

然后运行

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Hellobds123@S';

FLUSH PRIVILEGES;


0

对于那些遭遇此问题的人:

java.sql.SQLException: Access denied for user 'root'@'localhost'

(using password: YES)

解决方法是:

无论你在代码中提供了什么密码

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

不需要提供密码,只需插入""

完整代码如下:

import java.sql.*;  
class TestDB{  
    public static void main(String args[]){  
    try{  
        Class.forName("com.mysql.jdbc.Driver");  
        Connection con=DriverManager.getConnection(         "jdbc:mysql://localhost:3306/Pravin","root","");  
        //here sonoo is database name, root is username and password  
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("select * from student");  
        while(rs.next())  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+" "+rs.getString(3));  
        con.close();  
    }catch(Exception e){
        System.out.println(e);  
    }  
}  

0

我的答案是使用新的数据库连接器myPhpAdmin:

spring.datasource.url= jdbc:mysql://localhost:8889/mydb?serverTimezone=UTC
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.show-sql= false
spring.jpa.hibernate.ddl-auto= update
#spring.jpa.hibernate.ddl-auto= create
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
spring.main.banner-mode= off

这正是我一直在寻找的。通过Spring.*在我的Spring项目中添加这些连接器!感谢您发布这个! - JhWebDev

-1
晚回答,但这个发现可能对其他人有帮助 :) 遇到类似问题,我正在尝试从使用Eclipse开发的Spring Boot应用程序连接数据库。打开Mysql命令提示符或GUI工具,并运行以下查询。
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%your_password%' WITH GRANT OPTION;

同时检查您的TCP/IP服务是否已启用数据库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接