用户’root’@’localhost’拒绝访问

我试图从数据库中获取记录。 但我面临这个访问被拒绝的问题。 我尝试了Stack Overflow上提到的其他解决方案,比如向用户授予权限..但没有一个工作。

访问数据库的代码:

public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println("Servlet JDBC"); out.println(""); out.println("

Servlet JDBC

"); out.println(""); // 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("\t\t\t"); out.print(rs.getObject(2).toString()); out.print("
"); } } 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.(ConnectionImpl.java:834) com.mysql.jdbc.JDBC4Connection.(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) 

在这种情况下可能会出现什么问题。 我尝试创建一个新的数据库,但这也不起作用。

在控制台中启动mysql客户端并执行此查询: select Host, User from mysql.user; 。 你必须有这样一行:

 + ---------------- + ------------------ +  
 | 主持人| 用户|  
 + ---------------- + ------------------ +  
 |  localhost | 根|
 + ---------------- + ------------------ +  

Host中使用“localhost”并在User使用 “root”的行。 如果你没有这就是问题的原因(如果你在User中有“root”的其他行没关系)

如果您没有这样的行,请添加一个新用户:

 CREATE USER 'appUser'@'localhost' IDENTIFIED BY 'appPassword'; 

如果需要,可以通过“root”更改“appUser”,但我强烈建议您使用其他用户。 然后通过在mysql客户端中执行此操作为新用户添加权限:

 GRANT ALL PRIVILEGES ON employees.* TO 'appUser'@'localhost'; 

(再次,如果你愿意,可以通过’root’更改’appUser’)

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

尝试一下:

 mysql --no-defaults --force --user=root --host=localhost --database=mysql 

更改新密码:

 UPDATE user SET Password=PASSWORD('NEWPASSWORD') where USER='root'; FLUSH PRIVILEGES;