0

I am developing a Spring boot Project without docker and I try to connect a MYSQL 8 db. I created db by docker. But Spring boot project can not access the mysql. There is an exception that "Access denied for user 'root'@'172.21.0.1' (using password: YES)".

However I can access that by using mysql Workbench and I can access mysql terminal.

I try to solve by running that these commands on mysql terminal:

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

How Can I proceed and solve this problem?

docker-compose.yml:

version: '3.1'

services:
  mysql:
    image: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 020688
      MYSQL_DATABASE: ticketdb
      MYSQL_PASSWORD: 020688
      MYSQL_USER: root
    ports:
      - '3306:3306' 

ticket-service.yml

spring:
  application:
    name: ticket-service
  datasource:
    url: jdbc:mysql://localhost:3306/ticketdb
    username: root
    password: 020688
  jpa:
    database: mysql
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
      ddl-auto: update 

Exeption:

Caused by: java.sql.SQLException: Access denied for user 'root'@'172.21.0.1' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.18.jar:8.0.18]
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.18.jar:8.0.18]


Burak Dağlı
  • 512
  • 2
  • 10
  • 33
  • i'm not sure but do you have a mysql container already running ? Maybe that is the problem. Chek out here https://stackoverflow.com/a/59839180/10909386 – isa_toltar Mar 12 '21 at 06:37
  • Yes, that is running and I can access that in terminal and mysql workbenc GUI. And Exception is about priviliges. If it is about connection failure to host , maybe it can be about if running or not. – Burak Dağlı Mar 12 '21 at 06:45

2 Answers2

2

You are trying to connect as the root user from another host I assume (as this is what it looks like). Per default, mysql does not allow this (afaik).

Maybe have a look at this post, might be solving your issue:

mysql-root-access-from-all-hosts

phaen
  • 349
  • 2
  • 12
  • 1
    Hi, I readed that post and I added a new user 'root'@'172.21.0.1' with all priviliges. And 'root@'%' with all priviliges had existed. Again there is a same error – Burak Dağlı Mar 12 '21 at 08:45
2

In your SQL GRANT, you've created a user with the host 127.21.0.1, but your app seems to be running on 172.21.0.1

It might be a misstype in the question though.

Faeeria
  • 786
  • 2
  • 13