Issue
I am trying to write into a mysql database from Java but I get this error:
java.sql.SQLException: Access denied for user 'root'@'172.17.0.1' (using password: YES)
I am connecting to a local mysql instance and to be honest I don't know what's 172.17.0.1 ip.
The small code I wrote:
public static void writeToDB(String value, String unit) throws ClassNotFoundException, SQLException, SQLException
{
// create a mysql database connection
String myDriver = "com.mysql.cj.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/MyDB";
Class.forName(myDriver);
Connection conn = null;
conn = DriverManager.getConnection(myUrl, "root", "MyPass");
// the mysql insert statement
String query = " insert into data (unit, value)"
+ " values (?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = null;
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, unit);
preparedStmt.setString (2, value);
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
I ran
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyPass' WITH GRANT OPTION;
but nothing changed.
My my.cnf is all commented out. My local ip is 192.168.1.46. I tried to use this ip and 127.0.0.1 instead of localhost on the myUrl variable but nothing changed.
I restarted the mysql server, eclipse and pc without success.
Edit: additional info I stopped the server and ran the code. I get the exact same exception. I guess somehow I can't reach the server at all.
I am able to use "localhost" in my Python script. It writes on the same table as the java code.
Solution
I solved the issue by shutting down the Docker server. I was using it for another project and I completely forgot about it. Still not sure why I got the error as yes, I had a Mysql container running, but was not on localhost.
Answered By - Luca
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.