0

With this code

String url = "jdbc:mysql://localhost/mysql";
Connection con = DriverManager.getConnection(url, "root", "");

I get the following exception.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(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:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MyTest1.main(MyTest1.java:29)

I logged into the database using the command tool with root and no password. I was able to telnet to the database and I get this message

N
 5.5.17-log
           7{*Zj%u☻ǧ|tR}=Vz'L,6imysql_native_password"

3 Answers 3

1

Even I also faced the same problem. But, in my application, I did one mistake like I forgot to give password. You can try to manually login to your MySQL and check what is the username and password you are using to log in. That same details you can use in the URL. Better to give database names also. As like below For MySQL:

String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");

In the above, you forgot to give the port number of MySQL. You can check once your MySQL has a port number or not. By default, MySQL port number is 3306. Better to create and use your own database.

If the above one is not working try for GRANT all privileges to user root on your user database. To grant all privileges below is the query,

GRANT ALL PRIVILEGES ON mysql.* TO 'root'@'localhost' WITH GRANT OPTION;

or else

GRANT ALL PRIVILEGES ON mysql.* TO 'root'@'localhost' IDENTIFIED BY 'root';
Sign up to request clarification or add additional context in comments.

Comments

0

'root'@'localhost' is different from 'root'@'%'. It may still be a permissions issue.

I suggest that you log in through the console, create a 'test'@'%' user with a password, and give it all permissions. You can learn how to do that here.

Comments

0

It turned out to be some problem with my MYSQL installation. I uninstalled the AMP package I was using and installed XAMPP instead and it works.

1 Comment

What is that AMP package? How could I uninstall it? I'm using xampp for php, also I'd installed mysql server on my localhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.