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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24838298/
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-14 15:06:11  来源:igfitidea点击:

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

javamysqljdbc

提问by rajeev mishra

I am connecting Java using Eclipse with MySQL db

我正在使用 Eclipse 连接 Java 和 MySQL db

CODE

代码

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();
        }
    }
}

ERROR

错误

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)

What should I do to resolve my problem?

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

回答by kazi

try this...

尝试这个...

String password="Hsun123"; 

instead of

代替

String password=""; 

回答by Amitesh Kumar

Instead of using :

而不是使用:

 String uname="root@localhost";

Use :

用 :

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);
...

This should work (provided you are setting the valid password)

这应该有效(前提是您设置了有效密码)

回答by Pravin Garodi

For those who are suffering from this problem

对于那些受此问题困扰的人

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

java.sql.SQLException:拒绝用户“root”@“localhost”的访问

(using password: YES)

(使用密码:是)

Solution for that is:

解决方案是:

Whatever password you are providing on this line in your code

无论您在代码中的这一行提供什么密码

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

No need to provide this password just insert ""

无需提供此密码只需插入 ""

Complete Code is:

完整代码为:

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);  
    }  
}  

回答by George Tsamis

Well, same problem here. I'm using phpmyadmin.

嗯,同样的问题在这里。我正在使用 phpmyadmin。

I had to go to phpmyadmin->users->root->Edit Privileges.

我不得不去 phpmyadmin->users->root->Edit Privileges.

Then on the field "Database-specific privileges" I had to provide privileges for my database.

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

On the field "Change password" I retyped my password (I don't know why I had to do that, maybe a possible bug since i already had one).

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

On the field "Login Information->Host" I put value Any host.

在字段“ Login Information->Host”上,我将值设置为任何主机。

Problem is solved.

问题解决了。

回答by henry.w

Make sure you have a privileged (i.e. neccessary rights) user account with correct password exist in your database. or just creat no password at all

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

回答by S Debasish Nayak

If you are connecting a remote mysql server from your local machine using java see the below steps.

如果您使用 java 从本地计算机连接远程 mysql 服务器,请参阅以下步骤。

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

错误:java.sql.SQLException:用户'xxxxx'@'101.123.163.141'的访问被拒绝(使用密码:是)

for remote access may be cPanel or others grant the remote access for your local ip address.

远程访问可能是 cPanel 或其他人授予您本地 IP 地址的远程访问权限。

In the above error message: "101.123.163.141" is the local machine IP. So First we have to give remote access in the cPanel-> Remote MySQL?. Then run your application to connect.

在上面的错误信息中:“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();    

Hope it will resolve your issue.

希望它能解决您的问题。

Thanks

谢谢

回答by Joginder Malik

Late answer, but findings can help others :) Faced similar issue, i was trying to connect database from Spring boot application developed using Eclipse. Open Mysql Command Prompt or GUI tooland run following query.

迟到的答案,但发现可以帮助其他人:) 面对类似的问题,我试图从使用Eclipse开发的 Spring 启动应用程序连接数据库。打开Mysql 命令提示符或 GUI 工具并运行以下查询。

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

Also check your TCP/IPservice for database are enabled or not.

还要检查数据库的TCP/IP服务是否启用。

回答by Rajaneesh

Below queries, execution has fixed my problem:

在查询下面,执行解决了我的问题:

sudo mysql;

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

须藤mysql;

mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; mysql>刷新权限'