I'm stuck with SQLNonTransientConnectionException in Spring Boot application with MariaDB database using docker.
Docker-compose.yaml
version: '3.1'
services:
stats-server:
container_name: stats-server
build:
dockerfile: Dockerfile-statsserver
context: ./stats-server
ports:
- "9090:9090"
depends_on:
- stats-db
stats-db:
container_name: stats-db
image: mariadb
ports:
- "9091:3306"
environment:
- MARIADB_ROOT_PASSWORD=root
- MARIADB_DATABASE=stats
- MARIADB_USER=stats_user
- MARIADB_PASSWORD=stats_password
application.properties
#port
server.port=9090
#MariaDB
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://stats-db:9091/stats
spring.datasource.username=stats_user
spring.datasource.password=stats_password
spring.jpa.hibernate.ddl-auto=update
After docker-compose up command I've got the SQLNonTransientConnectionException error:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=stats-db)(port=9091)(type=primary). Connection refused
Database container runs well.
With Intellij Idea I can connect by this url:
jdbc:mariadb://localhost:9091/stats
my bad, should been use internal container port
spring.datasource.url=jdbc:mariadb://stats-db:3306/stats
Related
I'm trying to connect my containerized spring boot application with another containerized MQTT broker. Both of them are on their own projects as follows:
mqtt docker-compose.yml:
version: '3.9'
services:
mqttbroker:
container_name: mqttbroker
restart: always
volumes:
- ./config:/mosquitto/config
- ./data:/mosquitto/data
- ./log:/mosquitto/log
ports:
- 8883:8883
networks:
- mynetwork
volumes:
config:
data:
log:
mqtt Dockerfile
FROM eclipse-mosquitto
WORKDIR /mosquitto
COPY . .
EXPOSE 8883
And then the spring boot project is like:
spring boot docker-compose.yml
version: '3.8'
services:
myapp:
build: .
container_name: myapp
ports:
- '8082:8082'
stdin_open: true
tty: true
networks:
- mynetwork
In my application.properties I try to connect to the MQTT broker like:
mosquitto.url=tcp://mqttbroker:8883 and I get connection refused. However, if I run the spring boot application locally, I can connect to the docker container with mosquitto.url=tcp://localhost:8883.
I would rather have all the configurations in my docker-compose files to decrease manual codes.
I really appreciate your help in advance!
Yes, You do have to share the network as external.
you cab check the networks you have with this command : docker network ls
You would notice that they each have their own networks.
adding this to spring boot docker-compose file should fix the issue :
networks:
default:
name: mynetwork
external: true
For more information about docker compose networks
I try to implement a SpringBoot Application with a MariaDB Connection running on Docker.
But unfortunately SpringBoot is not able to connect with the DB Container.
This is my docker-compose.yml:
version: "3.4"
services:
db:
image: mariadb:10.6
environment:
MYSQL_ROOT_PASSWORD: 'admin'
MYSQL_DATABASE: 'ghp_board'
MYSQL_USER: 'ghp_board_admin'
MYSQL_PASSWORD: 'ghp_board'
volumes:
- ./data/mariadb:/docker-entrypoint-initdb.d
ports:
- "33006:3306"
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- "8081:8080"
container_name: ghp_board_frontend
volumes:
- ./frontend:/usr/src/app/ghp_board_frontend
depends_on:
- db
backend:
build:
context: ./backend
dockerfile: Dockerfile
depends_on:
- db
ports:
- "8886:8080"
mailhog:
image: mailhog/mailhog:latest
ports:
- "8025:8025"
- "1025:1025"
And this is my application.properties:
spring.datasource.url=jdbc:mariadb://db:3306/ghp_board
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.datasource.username=ghp_board_admin
spring.datasource.password=ghp_board
logging.level.org.hibernate.SQL=debug
spring.flyway.enabled=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
The DB-Container is running. I can connect to it via docker-compose exec db /bin/bash and then mysql -u ghp_board_admin -p
But if I am running docker-compose up --build or gradlew clean build I will get the following error:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=db)(port=3306)(type=primary). db
Can anyone tell me what I did wrong?
I have created a docker-compose.yml to run a postgresql database . While the image is pulled and can run succesfully I am unable to connect to my database through a spring boot project I have . When I try to run my spring boot project after succesfully running my postgresql container with docker-compose up I get the error org.postgresql.util.PSQLException: The connection attempt failed. and a huge text of error logs underneath where I spotted the cause.
Caused by: java.net.UnknownHostException: postgresqldb
which is the name of my db image .
My docker-compose.yml at the root of my spring project
version: '3.1'
services:
postgresqldb:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=booksdb
My application.properties in my spring boot project .
server.port=8081
server.servlet.context-path=/rest
#Postgres
spring.datasource.url=jdbc:postgresql://postgresqldb:5432/booksdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
#JWT
jwt.secret-key=someKey
I would appreciate your help .
Maybe you should add a hostname to the container:
version: '3.1'
services:
postgresqldb:
image: postgres
hostname: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=booksdb
docker uses its own network and resolves hostname internally. You should assign a hostname to the running container that may be found for the rest of the containers.
Problem Description
Spring Boot(app) container cant connect to "mysql" container
Problem Output
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
application.properties
spring.datasource.username = root
spring.datasource.url = jdbc:mysql://mysql:3306/fms?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.password = manager#123
docker-compose.yml
version: '3.7'
services:
mysql:
image: mysql:latest
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- "33061:3306"
networks:
- spring-boot-mysql-network
environment:
MYSQL_DATABASE: fms
MYSQL_ROOT_PASSWORD: manager#123
volumes:
- ./database_storage:/docker-entrypoint-initdb.d
app:
build:
context: .
dockerfile: app.Dockerfile
ports:
- "8091:8080"
networks:
- spring-boot-mysql-network
depends_on:
- mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
restart: always
depends_on:
- mysql
environment:
PMA_HOST: database
PMA_PORT: 3306
ports:
- "9091:80"
networks:
spring-boot-mysql-network:
driver: bridge
I had done a silly mistake.
I was updating my application.property file and docker-compose up
--build.But I never repackaged war file.So I was reading my old war file and thus reading my old property file
Your settings look correct to me. However, you are using the latest docker image of MySQL (which is 8.0.x at the time of writing). So, the exception you got indicates that there is a compatibility issue between your MySQL connector and MySQL server version. Which version of MySQL Connector/J are you using in your Spring Boot application? You need to update the connector or downgrade the MySQL docker image version.
Could you try updating mysql connector version to mysql-connector-java-8.0.11
I have two docker containers: mysql and spring boot service.
Service container is not connecting to mysql container(failing on the deploy) with following exception:
CommunicationsException: Communications link failure
So far I have tried using docker --link, docker networks, running everything using/without docker compose.
I am able to connect to DB container outside of docker.
Connection string: jdbc:mysql://db:3306/somedb
docker-compose.yml
version: '3.7'
services:
app:
build: .
restart: unless-stopped
ports:
- "80:8060"
links:
- db
depends_on:
- db
db:
image: "mysql:latest"
command: --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_DATABASE=somedb
- MYSQL_ROOT_PASSWORD=somepassword
ports:
- "3306:3306"
volumes:
- /Users/someuser/someproject/mysql/data:/var/lib/mysql
I had the same problem earlier and i tried with by changing the mysql image version. Here is my compose yaml file where i am able to connect it with.
version: "3.0"
services:
spring-boot-container:
build: .
ports:
- "8086:8086"
links:
- mysql-standalone
mysql-standalone:
image: mysql:5.6
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: springsecurity
MYSQL_USER: sa
MYSQL_PASSWORD: password
So in my service dockerfile I say:
ADD target/app-1.0-SNAPSHOT.jar /app-service.jar
RUN sh -c 'touch /app-service.jar'
But I forgot to rebuild jar after making changes in service config files
TL;DR I'm retarded