用户 'root'@'localhost' 的访问被拒绝 [英] Access denied for user 'root'@'localhost'

查看:155
本文介绍了用户 'root'@'localhost' 的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中获取记录.但我正面临这个访问被拒绝的问题.我尝试了 Stack Overflow 上提到的其他解决方案,例如向用户授予特权..但没有奏效.

I am trying to fetch records from database. but I am facing this access denied issue. I tried the other solutions mentioned on Stack Overflow like granting privilege to the user.. but none worked.

访问数据库的代码:

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<head><title>Servlet JDBC</title></head>");
  out.println("<body>");
  out.println("<h1>Servlet JDBC</h1>");
  out.println("</body></html>");  
  // connecting to database
  Connection con = null;  
  Statement stmt = null;
  ResultSet rs = null;
  try {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees","root","root");
      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * FROM employee");
      // displaying records
      while(rs.next()){
      out.print(rs.getObject(1).toString());
      out.print("			");
      out.print(rs.getObject(2).toString());
      out.print("<br>");
  }
  } catch (SQLException e) {
      throw new ServletException("Servlet Could not display records.", e);
  } catch (ClassNotFoundException e) {
      throw new ServletException("JDBC Driver not found.", e);
  } finally {
      try {
          if(rs != null) {
              rs.close();
              rs = null;
          }
          if(stmt != null) {
              stmt.close();
              stmt = null;
          }
          if(con != null) {
              con.close();
              con = null;
          }
      } catch (SQLException e) {}
  }
    out.close();
  }

错误的堆栈跟踪:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:927)
    com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4686)
    com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1304)
    com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2483)
    com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
    com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
    com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    java.sql.DriverManager.getConnection(Unknown Source)
    java.sql.DriverManager.getConnection(Unknown Source)
    WebAppAss.DatabaseAccess.service(DatabaseAccess.java:36)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

在这种情况下可能是什么问题.我尝试创建一个新数据库,但也没有用.

What could be the problem in this case. I tried creating a new database but that didn't work too.

推荐答案

问题是在 information.schema 表中授予 root 的权限.'root'@'%' 没有任何权限.. 因为我使用 127.0.0.1 作为我的连接地址,所以它给出了访问被拒绝的错误.. % 是 IP 地址的通配符.因此 mysql 将 root@127.0.0.1 视为任何其他 ip 地址,而不是 localhost.所以只要授予它权限就解决了我的问题.. 尝试使用一些 Mysql 客户端,如 SQLYog 等.授予权限很容易,也很容易与用户一起查看权限.

The problem was the permissions granted to root in the information.schema table. 'root'@'%' didnt have any permissions.. And as I was using 127.0.0.1 as my connection address, so it was giving the access denied error.. % is the wildcard for an ip address. so mysql considers root@127.0.0.1 as any other ip address but not localhost. so just granting it permission solved my problem.. Try using some Mysql client like SQLYog etc.. it is easy to grant the privileges and also to view the privileges with the user.

这篇关于用户 'root'@'localhost' 的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆