Burak Dağlı
Burak Dağlı

Reputation: 512

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]


Upvotes: 0

Views: 1762

Answers (2)

Faeeria
Faeeria

Reputation: 826

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.

Upvotes: 2

phaen
phaen

Reputation: 388

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

Upvotes: 3

Related Questions