spring boot project can not access mysql db that is created by docker

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]


Answer

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.

Leave a Reply

Your email address will not be published. Required fields are marked *