2

I have created a simple spring boot app to communicate with MYSQL via docker using the same network. Once I run docker-compose up command then following errors have occurred

employee-jdbc-container | java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

employee-jdbc-container | Caused by: com.mysql.cj.exceptions.UnableToConnectException: Public Key Retrieval is not allowed

docker-compose.yml

version: "3"   
services:
  employee-mysql:
             image: employee-jdbc
             container_name: employee-jdbc-container
             ports:
             - "9090:9090"
             networks:
               - employee-mysql2    
             depends_on:
               - mysqldb
  mysqldb:
         image: mysql:8
         container_name: mysqldb
         ports:
         - "3306:3306"
         networks:
           - employee-mysql2
         environment:
           - MYSQL_USER=root
           - MYSQL_ROOT_PASSWORD=root
           - MYSQL_DATABASE=HR
networks:                     
    employee-mysql2: 

application.yml

server:
  port: 9090

spring:
  datasource:
    url: "jdbc:mysql://mysqldb:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&useSSL=false"
    username: root
    password: root
    platform: mysql
    initialization-mode: always
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect

I think the problem in the configuration file but have no idea what is wrong.

Nafaz M N M
  • 1,558
  • 2
  • 27
  • 41
  • 1
    Please try to connect to the database with a different tool, to see if it's working. If the db works, the issue is probably within in the application.yml. – RatzzFatzz Aug 31 '20 at 08:44
  • Are you sure your DB is up, when connection is made? Did you try to repeat ```docker-compose up``` after short period? (it should restart your application while keep DB inact.) – GintsGints Aug 31 '20 at 08:49
  • Your error suggests, you need to update starting options - https://stackoverflow.com/questions/50379839/connection-java-mysql-public-key-retrieval-is-not-allowed – GintsGints Aug 31 '20 at 08:51
  • @GiantsGits yes.DB container is runing – Nafaz M N M Aug 31 '20 at 09:03
  • @RatzzFatzz MYSQL has not been installed in my local machine. if there any other way to check it without installing it in the local machine – Nafaz M N M Aug 31 '20 at 09:07
  • @NafazBenzema make sure mysql-client package is installed and then you can do the following: ```docker exec mysql -uroot -p```. But in general, there different methods, which would also work. (access the containers bash and then mysql, etc.) – RatzzFatzz Aug 31 '20 at 09:14
  • @RatzzFatzz should I have to install it explicitly. doesn't it contain with MYSQL image – Nafaz M N M Aug 31 '20 at 09:24
  • @RatzzFatzz yes I can access MYSQL using bash. it is running properly. but the problem occurs when trying to communicate with spring boot and MySQL – Nafaz M N M Aug 31 '20 at 09:27
  • Great, at least we now know, that it's definitely spring boot / java side – RatzzFatzz Aug 31 '20 at 09:36
  • @NafazBenzema Did you try to update the starting options? If not, try to use this URL: ```"jdbc:mysql://mysqldb:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false``` – RatzzFatzz Aug 31 '20 at 09:42
  • @RatzzFatzz " jdbc:mysql://mysqldb:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false" . it does not give the solution. same errors are occured – Nafaz M N M Aug 31 '20 at 10:13

1 Answers1

2

finally, I have found another way to run both containers without any issue.

first set MySQL environment

docker pull mysql:8

then run the image as a container

docker container run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=<root_password> -e MYSQL_DATABASE=<database_name>
-e MYSQL_USER=<user> -e MYSQL_PASSWORD=<password>  mysql:8(image name with version)

second set spring boot environment

application.yml

server:
  port: 9090

spring:
  datasource:
    url: "jdbc:mysql://mysql-db/<database_name>?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false"
    username: <username>
    password: <password>
    initialization-mode: always
    driver-class-name: com.mysql.cj.jdbc.Driver
    tomcat:
      test-while-idle: true
      validation-query: SELECT 1
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect
    show-sql: true
    hibernate:
      ddl-auto: update
      

Docker file

FROM openjdk:11
COPY ./target/employee-mysql.jar employee-mysql.jar
EXPOSE <port number> (where your application is running)
ENTRYPOINT ["java","-jar","employee-mysql.jar"]

build a docker image for spring boot app

docker image build -t employee-jdbc .

run the employee-jdbc image as a container

docker container run -d --name employee-mysql -p 9090:9090 --link mysql-db:mysql(mysql is the image of mysql-db container) employee-jdbc(employee-jdbc is the image of employee-mysql container)
Nafaz M N M
  • 1,558
  • 2
  • 27
  • 41