【问题标题】:java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)java.sql.SQLException:用户'root@localhost'@'localhost'的访问被拒绝(使用密码:是)
【发布时间】:2014-09-10 09:02:08
【问题描述】:

我正在使用 Eclipse 和 MySQL db 连接 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 的密码是否正确?
  • 我会试试String uname="root";

标签: java mysql jdbc


【解决方案1】:

而不是使用:

 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 -&gt; username 不符合 6 个字符的限制。所以我认为通过添加额外的字符,然后在单独的编辑中删除它们将是绕过规则的聪明方法。好吧,事实并非如此。对此感到抱歉。
【解决方案2】:

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

错误:java.sql.SQLException:拒绝访问用户 'xxxxx'@'101.123.163.141'(使用密码: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。
【解决方案3】:

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

sudo mysql

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

mysql&gt; FLUSH PRIVILEGES;

【讨论】:

    【解决方案4】:

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

    我必须去phpmyadmin-&gt;users-&gt;root-&gt;Edit Privileges.

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

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

    在“Login Information-&gt;Host”字段上,我输入值 Any host。

    问题解决了。

    【讨论】:

      【解决方案5】:

      试试这个...

      String password="Hsun123"; 
      

      而不是

      String password=""; 
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          【解决方案7】:

          对于那些正在遭受这个问题的人

          java.sql.SQLException: 用户'root'@'localhost'的访问被拒绝

          (使用密码: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);  
              }  
          }  
          

          【讨论】:

            【解决方案8】:

            我对数据库 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 项目中添加这些连接器!感谢您发布此信息!
            【解决方案9】:

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

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

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

            【讨论】:

              猜你喜欢
              • 2012-08-08
              • 1970-01-01
              • 2015-05-11
              • 2020-03-18
              • 2012-08-11
              • 2021-08-05
              • 2018-04-24
              相关资源
              最近更新 更多