Access denied for User 'root'@'localhost' (using password: YES )

22,391

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.

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

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

Also flush privileges immediately after using FLUSH PRIVILEGES;

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.

Share:
22,391
Ramesh J
Author by

Ramesh J

Jr.Ruby On rails Developer.. Jr.Android Developer.. & Jr.Java Developer

Updated on October 24, 2020

Comments

  • Ramesh J
    Ramesh J over 3 years

    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.

    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","");
    

    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."%".

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

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