1

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.

Luca
  • 1,159
  • 2
  • 10
  • 18
  • Can you check this solution http://stackoverflow.com/a/11922393/227775 – dasrohith Apr 28 '17 at 06:33
  • @dasrohith I already tried but it didn't solve my issue – Luca Apr 28 '17 at 06:38
  • have you tried with ip or locahost? – dasrohith Apr 28 '17 at 06:41
  • @dasrohith yes, I tried localhost, 127.0.0.1, my local ip and that IP that the exception shows (that I don't recognize). Still no luck – Luca Apr 28 '17 at 06:42
  • Execute FLUSH PRIVILEGES; after the GRANT ALL PRIVILEGES – dasrohith Apr 28 '17 at 06:44
  • Thanks, but it didn't work – Luca Apr 28 '17 at 06:46
  • Have you checked this SO answer. Please have a look into this http://stackoverflow.com/a/32288517/227775 – dasrohith Apr 28 '17 at 06:47
  • @dasrohith I tried id. I had many root users (all the IPs and wildcard I tried). I removed them ad left with only root@127.0.0.1, localhost, % wildcard and 172.17.0.1 (the IP of the exception). I also ran a FLUSH PRIVILEGES; Still no changes.. Of course as soon as it will work I'll remove the wildcard and I'll not use root. I don't know if you saw my edit but I can connect using Python – Luca Apr 28 '17 at 07:02
  • This solved my problem https://medium.com/tech-learn-share/docker-mysql-access-denied-for-user-172-17-0-1-using-password-yes-c5eadad582d3 – salerokada Sep 16 '21 at 12:11

4 Answers4

2

I had been facing the same problem. I found one solution to this problem.

CREATE USER 'root'@'172.17.0.1' IDENTIFIED BY 'root';


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


docker exec -it a5572eb07b8fe8e216d9addc63c1b8f50473638393c39d79e44e2ca7ffd6781e mysql -uroot -p (to get the docker-mysql terminal)[![This picture is for docker-mysql CLI][1]][1]
flush privileges;

Create the user for the IP address for which you are using getting this error using above command on the docker-mysql terminal.

Ajay Kumar
  • 586
  • 5
  • 21
  • I tried to execute your second line and got the following message `Error Code: 1410. You are not allowed to create a user with GRANT` Strange as I didn't think I was creating a user with GRANT? Any ideas? – codeskin Aug 04 '23 at 16:23
1

thoughts:

  1. if you checks roots privileges, you will see, this user is bound to localhost.
  2. i have a query-user to exec queries without host (%)

so to solve this problem: 1. give root more access 2. use another user for access (not root) 3. important:use newest mysql jar driver package.

drdrej
  • 904
  • 8
  • 14
0

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.

Luca
  • 1,159
  • 2
  • 10
  • 18
0

The same error comming on connect with DB in local Docker, if Docker self has problems to connect to with his own cloud account (Feuerwall, login problem etc. ).

Iakov Senatov
  • 151
  • 1
  • 5