mongodb and express error: Could not connect to database using connectionString - java

This was working last 3 months ago and now i check its error. The error show that Could not connect to database using connectionString
Already try this Problem with connecting mongo express service in docker
Docker-compose.yaml
version: '3.8'
networks:
default:
name: product-poc-project-net
external: true
services:
loggermicroservice:
image: loggermicroservice
container_name: loggermicroservice
build:
context: ./
dockerfile: Dockerfile
ports:
- 8087:80
depends_on:
- mongodb
#----------------------------------------------------------------
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
ports:
- 27017:27017
environment:
MONGO_INITDB_DATABASE: LoggerActivity
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
volumes:
- ./database-data:/data/db
#----------------------------------------------------------------
mongo-express:
image: mongo-express:latest
container_name: mongo-express
restart: unless-stopped
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_URL: mongodb://root:root#mongodb:27017/
application.properties
spring.data.mongodb.host=mongodb
spring.data.mongodb.port=27017
spring.data.mongodb.database=LoggerActivity
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=root
Error 1
Could not connect to database using connectionString: mongodb://root:root#mongodb:27017/"
mongo-express | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongodb
Error 2
Caused by: java.net.UnknownHostException: mongodb: Temporary failure in name resolution

fix in mongo-express side by removing ME_CONFIG_MONGODB_PORT: 27017
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_URL: mongodb://root:root#mongodb:27017/"

Related

Docker-compose: There is not are not services registered in Eureka Server

I am trying to run all microservices from a docker-compose file and I am facing issues when running the containers due to the issue that have between the Eureka discovery server and api-gateway with the other services. There is a way that can make the discover-server (Eureka) communicate with the other services? Many thanks in advance.
version: "3"
services:
discovery-server:
image: renosbardis/discovery-service:latest
container_name: discovery-server
ports:
- "8761:8761"
environment:
- SPRING_PROFILES_ACTIVE=docker
networks:
- test-network
api-gateway:
image: renosbardis/api-gateway:latest
container_name: api-gateway
ports:
- "8888:8888"
environment:
- SPRING_PROFILES_ACTIVE=docker
- LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_SECURITY= TRACE
depends_on:
- discovery-server
networks:
- test-network
accounts-service:
image: renosbardis/accounts-service:latest
container_name: accounts-service
ports:
- "8081:8081"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: rabbitmq
ports:
- "5672:5672"
- "15672:15672"
environment:
AMQP_URL: 'amqp://rabbitmq?connection_attempts=5&retry_delay=5'
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
customers-service:
image: renosbardis/customer-service:latest
container_name: customers-service
ports:
- "8083:8083"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
transactions-service:
image: renosbardis/transaction-service:latest
container_name: transactions-service
ports:
- "8084:8084"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
notification-service:
image: renosbardis/notification-service:latest
container_name: notification-service
ports:
- "8085:8085"
environment:
- SPRING_PROFILES_ACTIVE=docker
depends_on:
- discovery-server
- api-gateway
networks:
- test-network
networks:
test-network:
driver: bridge
There is nothing wrong with the docker-compose configuration file, the first thing you need to check is that the api-gateway can access the discovery-server properly
You can go into the containers and ping or telnet to test whether the network between the containers is connected.
use docker exec -it name /bin/bash
The startup log shows that your api-gateway configuration is incorrect, as it should be
discover-server:8761//eureka no localhost
api-gateway | 2022-12-28 02:25:27.549 INFO 1 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=I/O error on POST request for "http://localhost:8761/eureka/apps/API-GATEWAY": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused) stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8761/eureka/apps/API-GATEWAY": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
edit 1
I pulled your api-gateway image and started
And executed
docker cp api-gateway:/app/resources/application.properties ./
Here is the configuration that went wrong
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
You just have to change it to
eureka.client.serviceUrl.defaultZone=http://discovery-server:8761/eureka
edit 2
Add the following configuration to api-gatway configuration file to see if it works
eureka.client.fetch-registry=true
eureka.client.registry-with-eureka=true
edit 3
I find that most of your problems are configuration file errors in the mirror
This section uses customer-service mirroring as an example
Its port is set to 0
hostname is localhost
defaultZone is localhost:8761/eureka
This is the configuration file of customer-service that I modified
# Server port
server.port = 8083
eureka.instance.hostname = customer-service
spring.application.name = customer-service
# Memory Database for development Environment
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:customerdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.hibernate.dialect=org.hibernate.dialect.H2Dialect
eureka.client.service-url.defaultZone=http://discovery-server:8761/eureka
eureka.client.fetch-registry= true
eureka.client.register-with-eureka= true
This is my visit 0.0.0.0:8888/api/v1/customer/2 the returned json all working properly
{
"customerID": 2,
"name": "John",
"surname": "Doe",
"balance": 50
}

Docker Compose java.lang.ClassCastException: class org.hibernate.dialect.MySQL8Dialect cannot be cast to class java.sql.Driver

I have a problem about fixing the issue in docker-compose.yml file.
I have a problem about connection between service and database.
Here is my issue shown below.
Failed to create instance of driver class org.hibernate.dialect.MySQL8Dialect, trying j
dbcUrl resolution
java.lang.ClassCastException: class org.hibernate.dialect.MySQL8Dialect cannot be cast to class java.sql.Driver (org.hibernate.dialect.MySQL8Dialect is in unnamed module of loader org.
springframework.boot.loader.LaunchedURLClassLoader #2096442d; java.sql.Driver is in module java.sql of loader 'platform')
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
I've searched on resources on Internet but they all cannot help me fix my issue.
Here is my relevant codes of docker-compose.yml shown below.
services:
database:
container_name: mysql-database
image: 'mysql:latest'
ports:
- "3366:3306"
restart: always
environment:
#MYSQL_DATABASE: "springbootuser"
MYSQL_USER: "springmicroserviceuser"
MYSQL_PASSWORD: "111111"
MYSQL_ROOT_PASSWORD: "111111"
volumes:
- db-data:/var/lib/mysql
networks:
backend:
aliases:
- "database"
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10
userservice:
image: user-service
build:
context: ./user-service
dockerfile: Dockerfile
environment:
SERVER_PORT: "9000"
CONFIGSERVER_URI: "http://configserver:9191"
CONFIGSERVER_PORT: "9191"
EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
EUREKASERVER_PORT: "8761"
SPRING_DATASOURCE_URL: "jdbc:mysql://database:3366/springbootuser?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey"
SPRING_DATASOURCE_DRIVER_CLASS_NAME: "org.hibernate.dialect.MySQL8Dialect"
SPRING_DATASOURCE_USERNAME: "springmicroserviceuser"
SPRING_DATASOURCE_PASSWORD: "111111"
SPRING_JPA_HIBERNATE_DDL_AUTO: "update"
depends_on:
database:
condition: service_healthy
configserver:
condition: service_started
ports:
- "9000:9000"
networks:
- backend
networks:
backend:
driver: bridge
volumes:
db-data:
Here is my project link : Link
Please check the value of SPRING_DATASOURCE_URL. Since you have configured a network, the host has to be mysql-database
SPRING_DATASOURCE_URL: "jdbc:mysql://mysql-database:3366/springbootuser?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey"

docker-compose fails on host mashine

I have a few microservices runins on spring boot, each of them have they own postgres database. When I run docker-compose up on development windows mashin it works correctly. But when I deploy it on linux VPS host, spring can not connect to database. Individually (without environment variables) the images are launched on VPS.
Here is my docker-compose.yml
version: "3.9"
services:
gateway:
image: gateway:latest
container_name: gateway
ports:
- "8080:8080"
depends_on:
- postgres
environment:
- DATASOURCE_URL=jdbc:postgresql://postgres:5432/gateway_db
- DATASOURCE_USERNAME=postgres
- DATASOURCE_PASSWORD=postgres
- JPA_HIBERNATE_DDL_AUTO=update
restart: unless-stopped
networks:
- postgres
postgres:
image: postgres:14.4
container_name: gateway_db
environment:
POSTGRES_DB: "gateway_db"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
volumes:
- pgdata:/var/lib/postgresql/data
expose:
- 5432
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres -d postgres" ]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
networks:
- postgres
volumes:
pgdata:
networks:
postgres:
driver: bridge
And this is my application.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.sql.init.mode=always
spring.sql.init.platform=postgres
spring.datasource.url=${DATASOURCE_URL}
spring.datasource.username=${DATASOURCE_USERNAME}
spring.datasource.password=${DATASOURCE_PASSWORD}
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=${JPA_HIBERNATE_DDL_AUTO}
So, here's spring logs from docker container on host machine
I would be very grateful for any help!

Spring Boot SQL with Docker - Could not connect to adress - Connection refused

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?

Docker-compose up : Connection refused

I'm using Docker-compose in my project when i'm trying to inside a project to connect to a host in my VBox "localhost:22000" it causes an Exception connection refused
version: '2'
services:
mongodb:
image: mongo
ports:
- "27017:27017"
command: mongod --smallfiles
rabbitmq:
image: rabbitmq:3.5.3-management
ports:
- "5672:5672"
- "15672:15672"
broker:
image: java:openjdk-8u91-jdk
depends_on:
- mongodb
- rabbitmq
working_dir: /app
volumes:
- ./blockchain-rabbitmq/target/:/app
command: java -jar /app/blockchain-rabbitmq-0.0.1.jar
ports:
- "8484:8484"
links:
- rabbitmq
- mongodb
environment:
SPRING_DATA_MONGODB_URI: mongodb://mongodb/ethereum
RABBIT_HOST: rabbitmq
SPRING_RABBITMQ_HOST: rabbitmq
nfb:
image: java:openjdk-8u91-jdk
depends_on:
- mongodb
- broker
working_dir: /app
volumes:
- ./coordinator/target/:/app
command: java -jar /app/coordinator-0.0.1.jar
ports:
- "8383:8383"
links:
- mongodb
environment:
SPRING_DATA_MONGODB_URI: mongodb://mongodb/ethereum
is there a way to expose some hosts or port ( i got this kind of exception before that when i'm dealing with mongo and rabbit mq and i resolved it buy using links and env vars )

Categories