简体   繁体   中英

Locall MySQL DB login Error : Access denied for user 'root'@'localhost' (using password: YES)

I have the weirdest MySQL login problem ever!

The DBMS is installed on my desktop and I'm accessing it locally, not through a network or internet.

When I try to connect to it via cmd:

mysql -u root -proot;

it connects perfectly..

but, when I try to connect through a Java program in NetBeans:

Connection con = null;

String url ="jdbc:mysql://localhost:3306/testdb";
String user ="root";
String password ="root";

try
{          
    con = DriverManager.getConnection(url,user,password);
}
Catch(Exception e)
{ 
     System.out.println(e.toString() );
}

it throws an exception !!

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

I have the mysql-connector-java-5.1.24-bin.jar library added to NetBeans.. It was working before, not sure why it is not working now!!

Also, when I connect to MySQL like this:

mysql -u root -p;

then I enter the password, it gives this error:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

??

Connection con = null;
String dbase = "database";
String dbuser = "root";
String dbpass = "";

    try{

                String ConnString;

                Class.forName("com.mysql.jdbc.Driver").newInstance();
                ConnString = "jdbc:mysql://localhost:3306/"+dbase+"?user="+dbuser+"&password="+dbpass;
                con = DriverManager.getConnection(ConnString);
                //System.out.println("Connection to database successful!");

            }catch(Exception e){

                e.printStackTrace();
            } 

Try this?

so stupid ~~~ I found the problem ... Here's why this is happening and how I solved it:

Reason of problem: I changed the password for root from blank to 'root'

Then, I couldn't connect to MySQL from Java ..

it turns out I have to update the privileges of the database after changing the password.

So I did that by:

GRANT ALL PRIVILEGES ON [database_name].* to '[user]'@'[hostname]' identified by '[password]'

and now it works 100%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM