java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

匿名 (未验证) 提交于 2019-12-03 02:18:01

问题:

The following code:

Class.forName("com.mysql.jdbc.Driver"); Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost","root","root"); 

Throws this exception on getConnection():

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)     at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)     at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)     at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:813)     at com.mysql.jdbc.JDBC4Connection.(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:399)     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     at db.Database.(Database.java:91)     at db.Main.main(Main.java:10) 

How is this caused and how can I solve it?

EDIT:

    public static void main(String[] args) throws ClassNotFoundException, ServletException, SQLException      {          try         {             Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");              Statement   s = (Statement) conn.createStatement();             int result = s.executeUpdate("CREATE DATABASE databasename");         }           catch ( Exception e)         {             e.printStackTrace();         } } 

Produces :

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)     at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)     at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)     at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:813)     at com.mysql.jdbc.JDBC4Connection.(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:399)     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)     at java.sql.DriverManager.getConnection(Unknown Source)     at java.sql.DriverManager.getConnection(Unknown Source)     at db.Main.main(Main.java:19) 

回答1:

As you are creating a database from scratch, you could use:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");  PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename"); int result = ps.executeUpdate(); 

Here is an identical scenario.



回答2:

This can help you:

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

Execute it with command line or some GUI tool.

Don't forget to replace %password% with real password.



回答3:

I had a similar problem, but the differemce was: I didn't executed my JavaApp from localhost, but from a remote PC. So I got something like java.sql.SQLException: Access denied for user 'root'@'a.remote.ip.adress' (using password: YES) To solve this, you can simply login to phpMyAdmin, go to Users, click add user and enter the host from which you want to execute your JavaApp (or choose Any Host)



回答4:

you can use this

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";      static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DB_NAME";      static final String USER = "root";    static final String PASS = "YOUR_ROOT_PASSWORD";     Connection conn = DriverManager.getConnection(DB_URL,USER,PASS); 

you have to give the right root password .



回答5:

You should specify the db you are connecting to:

jdbc:mysql://localhost:3306/mydb 


回答6:

I was hitting the same issue. Added mysql service port number(3307), resolved the issue.

conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/?" + "user=root&password=password"); 


回答7:

Try it like this....

public static Connection getConnection() throws SQLException{      String driver = "com.mysql.jdbc.Driver";     String url    = "jdbc:mysql://localhost:3306/test";     String username = "root";     String password = "vicky";            // Change it to your Password     System.setProperty(driver,"");      return DriverManager.getConnection(url,username,password); } 


回答8:

Although this may not be the cause of your issue, you'll get the same error if there are two MySQL services running on the same port. You can check on windows by looking at the list of services in the services tab of Task Manager.



回答9:

This Exception is also caused due to version mismatch of mysql db and your pom.xml/jar.

Make sure your pom.xml/jar version is higher than your mysql db version. Because higher versions are compatible with lower version's, but the same is not true for the inverse.

Solution's for java project

  • Replace the Jar with suitable version.

Solution for maven based

  • Change the dependency Version in pom.xml

Solution for spring boot - Override the spring boot dependency by adding 5.1.5.Final



回答10:

I had the same issue "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)". Problem was "WRONG PASSWORD". Copy and paste the query as-it-is in the shell to check whether it gives the desired output or not. Small errors consumes more time.



回答11:

I had to change my SQL setup (in my host, Dreamhost in my case) to allow the username to access the database from other hosts, not just Dreamhost since I'm running a program from the IDE on my computer. I did that for now by adding % to the list of Allowable Hosts. It now works!



回答12:

I encountered this error. The problem was that I had the wrong database name.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!