skip to Main Content

I’m new on Springboot and docker.
I’m trying to create three different microservices and load them into docker.

Two of them start correctly while the last one returns the following error:

Caused by: com.mysql.cj.exceptions.CJException: Access denied for user 'db_user'@'%' to database 'PaymentDB' 

I can’t find the error. The configuration of the three microservices are exactly the same (sometimes I copied and paste the similar code and changed only the name for example from Catalog to Payment. Can You suggest me something?

This is the application.properties:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${PM_DATASOURCE_HOST}:${DATASOURCE_PORT}/${PM_DATASOURCE_NAME}?autoReconnect=true
spring.datasource.username=${DATASOURCE_USER}
spring.datasource.password=${DATASOURCE_PASSWORD}
spring.kafka.bootstrap-servers=kafka:9092
kafkaTopic=payment-topic

This is the .env :

DB_DATABASE_PAYMENT=PaymentDB
DB_HOST_PAYMENT=paymentmysqldb

DB_DATABASE_USER=UserDB
DB_HOST_USER=usermysqldb

DB_DATABASE_CATALOG=CatalogDB
DB_HOST_CATALOG=catalogmysqldb


DB_USER=db_user
DB_PASSWORD=alx2022
DB_ROOT_PASSWORD=user
DB_PORT=3306

This is the docker-compose.yml


version: '3.4'

x-common-variables: &common-variables
  DATASOURCE_USER: ${DB_USER}
  DATASOURCE_PASSWORD: ${DB_PASSWORD}
  DATASOURCE_PORT: ${DB_PORT}

services:
  paymentmysqldb:
    container_name: paymentmysqldb
    image: mysql
    ports:
      - "3313:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_PAYMENT}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - paymentstorage:/var/lib/mysql


  usermysqldb:
    container_name: usermysqldb
    image: mysql
    ports:
      - "3311:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_USER}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - userstorage:/var/lib/mysql

  catalogmysqldb:
    container_name: catalogmysqldb
    image: mysql
    ports:
      - "3312:3306"
    environment:
      - MYSQL_DATABASE=${DB_DATABASE_CATALOG}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
    volumes:
      - catalogstorage:/var/lib/mysql

  paymentmanager:
    container_name: paymentmanager
    image: arausa/paymentimage
    build:
      context: .
      dockerfile: MicroServices/PaymentManager/Dockerfile
    depends_on:
      - paymentmysqldb
    ports:
      - "3333:8080"
    restart: always
    environment:
      <<: *common-variables
      PM_DATASOURCE_HOST: ${DB_HOST_PAYMENT}
      PM_DATASOURCE_NAME: ${DB_DATABASE_PAYMENT}

  usermanager:
    container_name: usermanager
    image: arausa/userimage
    build:
      context: .
      dockerfile: MicroServices/UserManager/Dockerfile
    depends_on:
      - usermysqldb
    ports:
      - "1111:8080"
    restart: always
    environment:
      <<: *common-variables
      UM_DATASOURCE_HOST: ${DB_HOST_USER}
      UM_DATASOURCE_NAME: ${DB_DATABASE_USER}

  catalogmanager:
    container_name: catalogmanager
    image: arausa/catalogimage
    build:
      context: .
      dockerfile: MicroServices/CatalogManager/Dockerfile
    depends_on:
      - catalogmysqldb
    ports:
      - "2222:8080"
    restart: always
    environment:
      <<: *common-variables
      CM_DATASOURCE_HOST: ${DB_HOST_CATALOG}
      CM_DATASOURCE_NAME: ${DB_DATABASE_CATALOG}



#kafka usa zookeeper tiene traccia dei broker, topologia della network e info per la sincronizzazione
  zookeeper:
    image: wurstmeister/zookeeper

#identifica il broker kafka
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092:9092"  #porta di default per il broker kafka
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181  #serve a dire dove sta girando zookeeper

volumes:
  userstorage:
  catalogstorage:
  paymentstorage:

This is the pom.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">

<modelVersion>4.0.0</modelVersion>


<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.6.4</version>

<relativePath/>

<!-- lookup parent from repository -->


</parent>

<groupId>com.example</groupId>

<artifactId>PaymentManager</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>PaymentManager</name>

<description>PaymentManager</description>


<properties>

<java.version>11</java.version>

</properties>


<dependencies>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-validation</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>


<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>


<dependency>

<groupId>org.springframework.kafka</groupId>

<artifactId>spring-kafka</artifactId>

</dependency>


<dependency>

<groupId>com.google.code.gson</groupId>

<artifactId>gson</artifactId>

<version>2.8.5</version>

</dependency>

</dependencies>


<build>


<plugins>


<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>


<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.12.4</version>


<configuration>

<skipTests>true</skipTests>

</configuration>

</plugin>

</plugins>

</build>

</project>

This is the docker file :

FROM maven:3-openjdk-17-slim as builder
WORKDIR /project
COPY MicroServices/PaymentManager/ .
RUN mvn package

FROM openjdk:17-alpine
WORKDIR /app
COPY --from=builder /project/target/PaymentManager-0.0.1-SNAPSHOT.jar ./PaymentManager.jar
CMD java -jar PaymentManager.jar

2

Answers


  1. Chosen as BEST ANSWER

    I solved in a strange way.

    I modified the word "Payment" with the word "pay" everywhere and it magically works. Even if I don't understand the reason


  2. Seems like your application.properties has some error.

    spring.datasource.username=${DATASOURCE_USER}
    spring.datasource.password=${DATASOURCE_PASSWORD}
    

    these env variables aren’t defined or exported from anywhere.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search