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

问题描述:

我想连接我的本地MYSQL数据库,该数据库与XAMP服务器一起安装.我创建了一个新用户,即"NewUser",其密码为"password".我把所有特权都给了这个用户.

I would like to connect with my local MYSQL data base, which is installed along with XAMP server. I created a new User ie, " NewUser " and its password is "password". I given all PRIVILEGES to this user.

我使用"root"用户(该用户无密码)编写连接数据库的代码.其连接.像下面这样.

I write the code to connect with data base by using user "root" (No password for this user). Its connected . Like bellow..

return DriverManager.getConnection("jdbc:MySql://localhost/database_name","root","");

现在,我编写了由另一个用户("NewUser"及其密码"password")连接相同数据库的代码

Now I wrote the code to connect with same data base by another user ie, "NewUser" and its pasword "password"

return DriverManager.getConnection("jdbc:MySql://localhost/database_name","NewUser","password");

,但未连接.控制台中的错误是

but its not connected. The error in console is

java.sql.SQLException: Access denied for user 'NewUser'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.GeneratedConstructorAccessor207.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:214)
    at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
    at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:73)
    at org.eclipse.datatools.enablement.internal.mysql.connection.JDBCMySQLConnectionFactory.createConnection(JDBCMySQLConnectionFactory.java:28)
    at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
    at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
    at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
    at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

我在向该用户提供特权的同时给出了主机类型,即任何主机",即%".

I give the host type while providing PRIVILEGES to this user, as " any host" ie."%".

如果我将其更改为"localhost"或"127.0.0.1",则其工作.

If I change this to " localhost " or "127.0.0.1" its working.

那么我如何将带有"anyhost"特权的数据库用于"NewUser"之类的特定用户.

So How can i use my database with " anyhost " PRIVILEGES to the particular user like "NewUser" .

如果我在这里获得成功,那么我将成功连接到客户端实时数据库.

If I got success here then I successes in connection to client live Data base..

谢谢大家,请让我离开.....

Thanks to all and please let me out from this one.....

由于第一个指定的代码有效,并且也基于报告的跟踪,因此我很确定问题出在数据库上,而不是代码语法上.

Since the first specified code works and also based on the reported trace, i'm pretty sure the problem is on the database, not the code syntax.

基于Mysql版本,请尝试基于

Based on the Mysql version, please try as an alternative to set privileges without specifying the any host (%) as based on Mysql documentation,

简单形式的user_name是user_name @'%'的同义词

The simple form user_name is a synonym for user_name@'%'

在使用 FLUSH PRIVILEGES;

只需确保所有内容正确无误,还可以运行 NewUser的显示权限; 并检查NewUser是否以相应的权限显示在列表中.

Just to make sure everything is correct, also run a SHOW GRANTS FOR NewUser; and check if NewUser appears in the list with the corresponding permissions.