skip to Main Content

I am new to containerization and I am trying to learn docker to create an e-commerce microservices Spring Boot application. I’ve set up a docker-compose file that defines a multi-container application that includes services for Mysql, MongoDB, Mongo Express, and MailDev. However, I am facing issues when attempting to connect mysql to SpringBoot. I recieve these errors stating that that database I called ‘ecommerce" cannot be found despite being defined in the environment variables of my docker-compose.yml file

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.SQLGrammarException: Unable to open JDBC Connection for DDL execution [Unknown database 'ecommerce'] [n/a]

Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.SQLGrammarException: Unable to open JDBC Connection for DDL execution [Unknown database 'ecommerce'] [n/a]

Caused by: org.hibernate.exception.SQLGrammarException: Unable to open JDBC Connection for DDL execution [Unknown database 'ecommerce'] [n/a]

Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'ecommerce'

Here is the full docker-compose.yml:

services:
  mysql:
    container_name: ms_mysql
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: ecommerce
    ports:
      - 3307:3306
    volumes:
      - mysql:/var/lib/mysql
    networks:
      - microservices-net
    restart: unless-stopped

  mongodb:
    container_name: ms_mongo_db
    image: mongo
    ports:
      - 27017:27017
    volumes:
      - mongo:/data
    environment:
      MONGO_INITDB_ROOT_USERNAME: earl
      MONGO_INITDB_ROOT_PASSWORD: root

  mongo-express:
    container_name: ms_mongo_express
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: earl
      ME_CONFIG_MONGODB_ADMINPASSWORD: root
      ME_CONFIG_MONGODB_SERVER: mongodb

  mail-dev:
    container_name: ms_mail-dev
    image: maildev/maildev
    ports:
      - 1080:1080
      - 1025:1025


networks:
  microservices-net:
    driver: bridge

volumes:
  mysql:
  mongo:

product-service.yml in my configuration server for the product service:

spring:
  datasource:
    url: jdbc:mysql://localhost:3307/ecommerce
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver 
  jpa:
    hibernate:
      ddl-auto: create-drop
      format_sql: true
    show-sql: true 
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect

server:
  port: 8050

I also created a data.sql file to define the tables placed in the same package of the docker-compose.yml(although I am not sure if this is the correct use) :

CREATE DATABASE IF NOT EXISTS ECOMMERCE;

USER ECOMMERCE;

CREATE TABLE category(
    id INT NOT NULL AUTO_INCREMENT,
    description VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    PRIMARY KEY(id)
);

CREATE TABLE product(
    id integer NOT NULL AUTO_INCREMENT,
    description VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    available_quantity INT,
    price DECIMAL(38,2)
    PRIMARY KEY(id)
)

Structure of the project

2

Answers


  1. Chosen as BEST ANSWER

    I decided to use MySQL Workbench to first create the ecommerce database and it worked. I suppose it was not enough to define the database inside the docker-compose.yml file and I had to manual create it myself through the MySQL Workbench.


  2. You need to wait for your DB to be up and running before starting your service.

    Can you provide your steps when starting you service

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