Server is running on port 5000 successfully, but unable to connect to the mongoDB atlas database
mymanager-server | /usr/src/app/node_modules/mongodb/lib/cmap/connect.js:375
mymanager-server | return new error_1.MongoNetworkError(err);
mymanager-server | ^
mymanager-server |
mymanager-server | MongoNetworkError: getaddrinfo EAI_AGAIN mymanager-shard-00-01.qrauq.mongodb.net
mymanager-server | at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/cmap/connect.js:375:20)
mymanager-server | at TLSSocket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/cmap/connect.js:295:22)
mymanager-server | at Object.onceWrapper (node:events:628:26)
mymanager-server | at TLSSocket.emit (node:events:513:28)
mymanager-server | at TLSSocket.emit (node:domain:489:12)
mymanager-server | at emitErrorNT (node:internal/streams/destroy:157:8)
mymanager-server | at emitErrorCloseNT (node:internal/streams/destroy:122:3)
mymanager-server | at processTicksAndRejections (node:internal/process/task_queues:83:21) {
mymanager-server | [Symbol(errorLabels)]: Set(0) {}
mymanager-server | }
docker-compose.yml
version: "3"
services:
mymanager_b:
build: ./mymanager_b
container_name: mymanager-server
command: yarn prod
restart: unless-stopped
ports:
- "5000:5000"
networks:
- mm-network
networks:
mm-network:
driver: bridge
docker-compose-dev.yml
version: "3"
services:
mymanager_b:
command: yarn dev
volumes:
- ./mymanager_b:/usr/src/app
- /usr/src/app/node_modules
And I have a separate .env file for the MONGO_URI
I expect it to run mongoDB as a normal dev environment
Related
Hi I try to create a project usingin spring webflux and mongodb, but when a Try to connect in mongodb I get a follow error:
com.mongodb.MongoSocketOpenException: Exception opening socket
app_1 | at com.mongodb.connection.netty.NettyStream$OpenChannelFutureListener.operationComplete(NettyStream.java:520) ~[mongodb-driver-core-4.4.2.jar!/:na]
app_1 | at com.mongodb.connection.netty.NettyStream$OpenChannelFutureListener.operationComplete(NettyStream.java:488) ~[mongodb-driver-core-4.4.2.jar!/:na]
app_1 | at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:609) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:117) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
app_1 | Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/127.0.0.1:27017
app_1 | Caused by: java.net.ConnectException: Connection refused
app_1 | at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
app_1 | at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
app_1 | at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946) ~[na:na]
app_1 | at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496) ~[netty-transport-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.74.Final.jar!/:4.1.74.Final]
app_1 | at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
I try to connect in database with another client and I havse success, probably the problem is in project config.
somo observations:
I using mac m1
I using docker
follow my configurations:
docker-compose:
version: '3.8'
services:
app:
build: .
command: bash start-dev.sh
ports:
- "8080:8080"
- "5005:5005"
environment:
DB_USER: reactive-calendar
DB_PASSWORD: reactive-calendar
DB_PORT: 27017
DB: reactive-calendar
GRADLE_USER_HOME: /box
SERVER_PORT: 8080
SPRING_PROFILES_ACTIVE: dev
volumes:
- .:/reactive-calendar
- box:/box
depends_on:
- db
db:
image: mongo:5.0.6
environment:
MONGO_INITDB_ROOT_USERNAME: reactive-calendar
MONGO_INITDB_ROOT_PASSWORD: reactive-calendar
MONGO_INITDB_DATABASE: reactive-calendar
ports:
- "27017:27017"
volumes:
- ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
box:
image: busybox
volumes:
- /box
volumes:
db:
box:
I set up a Minikube-Cluster with the MongoDB Community Kubernetes Operator.
To view the content i want to set up a mongo-express Instance which connects to the Mongo-Cluster/ReplicaSet. But if I apply the Deployment the Container always fails with:
mongo-express_1 | Waiting for localhost:27017...
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 |
mongo-express_1 |
mongo-express_1 | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1 | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1 | basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
mongo-express_1 | Database connected
mongo-express_1 | Admin Database connected
mongo-express_1 | Error [MongoError]: Authentication failed.
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at Socket.emit (events.js:314:20)
mongo-express_1 | at addChunk (_stream_readable.js:297:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:272:9)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:213:10)
mongo-express_1 | at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
mongo-express_1 | operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1611234788 },
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'Authentication failed.',
mongo-express_1 | code: 18,
mongo-express_1 | codeName: 'AuthenticationFailed',
mongo-express_1 | '$clusterTime': {
mongo-express_1 | clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1611234788 },
mongo-express_1 | signature: { hash: [Binary], keyId: [Long] }
mongo-express_1 | }
mongo-express_1 | }
mongo-express_1 | unable to list databases
mongo-express_1 | Error [MongoError]: command listDatabases requires authentication
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at Socket.emit (events.js:314:20)
mongo-express_1 | at addChunk (_stream_readable.js:297:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:272:9)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:213:10)
mongo-express_1 | at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
mongo-express_1 | operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1611234788 },
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'command listDatabases requires authentication',
mongo-express_1 | code: 13,
mongo-express_1 | codeName: 'Unauthorized',
mongo-express_1 | '$clusterTime': {
mongo-express_1 | clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1611234788 },
mongo-express_1 | signature: { hash: [Binary], keyId: [Long] }
mongo-express_1 | }
mongo-express_1 | }
For the ease of testing I used docker-compose to start mango-express and created a Port-Forwarding to access the cluster.
Versions:
MongoDB Cluster: tried 4.2.6 and 4.4.3
mongo-express: 0.54.0
Here the Deployment/Service I use for MongoDB Cluster creation:
---
apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
name: example-mongodb
spec:
members: 3
type: ReplicaSet
version: "4.4.3"
security:
authentication:
modes: ["SCRAM"]
users:
- name: mongoadmin
db: admin
passwordSecretRef: # a reference to the secret that will be used to generate the user's password
name: mongoadmin-password
roles:
- name: root
db: admin
scramCredentialsSecretName: my-scram
# the user credentials will be generated from this secret
# once the credentials are generated, this secret is no longer required
---
apiVersion: v1
kind: Secret
metadata:
name: mongoadmin-password
type: Opaque
stringData:
password: mongoadmin
And here the compose-file i use for mongo-express:
version: '3.8'
services:
mongo-express:
image: mongo-express
restart: on-failure
ports:
- 8081:8081
network_mode: host
environment:
ME_CONFIG_MONGODB_SERVER: localhost
ME_CONFIG_MONGODB_ADMINUSERNAME: mongoadmin
ME_CONFIG_MONGODB_ADMINPASSWORD: mongoadmin
The thing is, that if I setup up a mongo (4.4.3) container with mongo-express via docker-compose everything works fine...
Does anybody have a clue whats going on?
I have build the docker image from the alpha version of mongo-express
https://github.com/mongo-express/mongo-express/tree/v1.0.0-alpha.1
Here is the image uncl3mar1k/mongo-express:v1.0.0-alpha.1
It seems to work well with authentication, but I noticed that it has an issue with pagening. (if you have a lot of collections and try to go through pages, you will notice that data about collections did not changes)
Update: there is a newer version v1.0.0-alpha.3 looks like all issues was fixed here. Please take a look to new image: staslb/mongo-express:v1.0.0-alpha.3
When starting my app, I get the following error:
error: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkTimeoutError: connection timed out
backend_1 | at connectionFailureError (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:342:14)
backend_1 | at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:310:16)
backend_1 | at Object.onceWrapper (events.js:421:28)
backend_1 | at Socket.emit (events.js:315:20)
backend_1 | at Socket._onTimeout (net.js:483:8)
backend_1 | at listOnTimeout (internal/timers.js:554:17)
backend_1 | at processTimers (internal/timers.js:497:7)]
backend_1 | at Pool.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
backend_1 | at Pool.emit (events.js:315:20)
backend_1 | at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:562:14
backend_1 | at /usr/src/app/node_modules/mongodb/lib/core/connection/pool.js:995:11
backend_1 | at /usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
backend_1 | at callback (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:280:5)
backend_1 | at Socket.<anonymous> (/usr/src/app/node_modules/mongodb/lib/core/connection/connect.js:310:7)
backend_1 | at Object.onceWrapper (events.js:421:28)
backend_1 | at Socket.emit (events.js:315:20)
backend_1 | at Socket._onTimeout (net.js:483:8)
backend_1 | at listOnTimeout (internal/timers.js:554:17)
backend_1 | at processTimers (internal/timers.js:497:7),
However, I can connect to the MongoDB service via MongoDBCompass and the Database connection tool in WebStorm, using the same connection string. "mongodb://localhost:27017/fmis"
I'm struggling to find the root cause to even try and solve it myself.
Update: as asked in the comments, I am running docker
I solved this by including the following in my connection configuration:
mongoose.connect(
db,
{
...
useUnifiedTopology: true //THIS SETTING
})
...
)
I used SpringBoot rest api and MongoDB. I have three different DataBases such as player-db, game-db and score-db. for each services I have DockerFiles.
when I run docker-compose up I've Getting an Error:
The error in docker-shell:
game-docker_1 | 2020-06-12 09:12:03.795 INFO 1 --- [localhost:27017] org.mon
godb.driver.cluster : Exception in monitor thread while connecting
to server localhost:27017
game-docker_1 |
game-docker_1 | com.mongodb.MongoSocketOpenException: Exception opening socke
t
game-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:70) ~[mongo-java-driver-3.11.2.jar!/:na]
game-docker_1 | at com.mongodb.internal.connection.InternalStreamConnect
ion.open(InternalStreamConnection.java:128) ~[mongo-java-driver-3.11.2.jar!/:na]
game-docker_1 | at com.mongodb.internal.connection.DefaultServerMonitor$
ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.1
1.2.jar!/:na]
game-docker_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]
game-docker_1 | Caused by: java.net.ConnectException: Connection refused (Con
nection refused)
game-docker_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
~[na:1.8.0_252]
game-docker_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPl
ainSocketImpl.java:350) ~[na:1.8.0_252]
game-docker_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(Abs
tractPlainSocketImpl.java:206) ~[na:1.8.0_252]
game-docker_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlai
nSocketImpl.java:188) ~[na:1.8.0_252]
game-docker_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java
:392) ~[na:1.8.0_252]
game-docker_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_2
52]
game-docker_1 | at com.mongodb.internal.connection.SocketStreamHelper.in
itialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.11.2.jar!/:na]
game-docker_1 | at com.mongodb.internal.connection.SocketStream.initiali
zeSocket(SocketStream.java:79) ~[mongo-java-driver-3.11.2.jar!/:na]
game-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:65) ~[mongo-java-driver-3.11.2.jar!/:na]
game-docker_1 | ... 3 common frames omitted
game-docker_1 |
player-docker_1 | 2020-06-12 09:12:03.810 INFO 1 --- [localhost:27017] org.mon
godb.driver.cluster : Exception in monitor thread while connecting
to server localhost:27017
player-docker_1 |
player-docker_1 | com.mongodb.MongoSocketOpenException: Exception opening socke
t
player-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:70) ~[mongo-java-driver-3.11.2.jar!/:na]
player-docker_1 | at com.mongodb.internal.connection.InternalStreamConnect
ion.open(InternalStreamConnection.java:128) ~[mongo-java-driver-3.11.2.jar!/:na]
player-docker_1 | at com.mongodb.internal.connection.DefaultServerMonitor$
ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.1
1.2.jar!/:na]
player-docker_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]
player-docker_1 | Caused by: java.net.ConnectException: Connection refused (Con
nection refused)
player-docker_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
~[na:1.8.0_252]
player-docker_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPl
ainSocketImpl.java:350) ~[na:1.8.0_252]
player-docker_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(Abs
tractPlainSocketImpl.java:206) ~[na:1.8.0_252]
player-docker_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlai
nSocketImpl.java:188) ~[na:1.8.0_252]
player-docker_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java
:392) ~[na:1.8.0_252]
player-docker_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_2
52]
player-docker_1 | at com.mongodb.internal.connection.SocketStreamHelper.in
itialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.11.2.jar!/:na]
player-docker_1 | at com.mongodb.internal.connection.SocketStream.initiali
zeSocket(SocketStream.java:79) ~[mongo-java-driver-3.11.2.jar!/:na]
player-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:65) ~[mongo-java-driver-3.11.2.jar!/:na]
player-docker_1 | ... 3 common frames omitted
player-docker_1 |
score-docker_1 | 2020-06-12 09:12:03.893 INFO 1 --- [localhost:27017] org.mon
godb.driver.cluster : Exception in monitor thread while connecting
to server localhost:27017
score-docker_1 |
score-docker_1 | com.mongodb.MongoSocketOpenException: Exception opening socke
t
score-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:70) ~[mongo-java-driver-3.11.2.jar!/:na]
score-docker_1 | at com.mongodb.internal.connection.InternalStreamConnect
ion.open(InternalStreamConnection.java:128) ~[mongo-java-driver-3.11.2.jar!/:na]
score-docker_1 | at com.mongodb.internal.connection.DefaultServerMonitor$
ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.1
1.2.jar!/:na]
score-docker_1 | at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]
score-docker_1 | Caused by: java.net.ConnectException: Connection refused (Con
nection refused)
score-docker_1 | at java.net.PlainSocketImpl.socketConnect(Native Method)
~[na:1.8.0_252]
score-docker_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPl
ainSocketImpl.java:350) ~[na:1.8.0_252]
score-docker_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(Abs
tractPlainSocketImpl.java:206) ~[na:1.8.0_252]
score-docker_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlai
nSocketImpl.java:188) ~[na:1.8.0_252]
score-docker_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java
:392) ~[na:1.8.0_252]
score-docker_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_2
52]
score-docker_1 | at com.mongodb.internal.connection.SocketStreamHelper.in
itialize(SocketStreamHelper.java:64) ~[mongo-java-driver-3.11.2.jar!/:na]
score-docker_1 | at com.mongodb.internal.connection.SocketStream.initiali
zeSocket(SocketStream.java:79) ~[mongo-java-driver-3.11.2.jar!/:na]
score-docker_1 | at com.mongodb.internal.connection.SocketStream.open(Soc
ketStream.java:65) ~[mongo-java-driver-3.11.2.jar!/:na]
score-docker_1 | ... 3 common frames omitted
score-docker_1 |
game-docker_1 | 2020-06-12 09:12:06.823 INFO 1 --- [ main] o.s.s.c
oncurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTas
kExecutor'
player-docker_1 | 2020-06-12 09:12:06.895 INFO 1 --- [ main] o.s.s.c
oncurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTas
kExecutor'
The docker-compose.yml:
version: "3"
services:
player-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/player/DockerFile
restart: always
ports:
- 8080:8080
depends_on:
- db
game-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/game/DockerFile
restart: always
ports:
- 8081:8080
depends_on:
- db1
score-docker:
build:
context: ./
dockerfile: ./src/main/java/spring/multiple/mongo/project/score/Dockerfile
restart: always
ports:
- 8082:8080
depends_on:
- db2
db:
image: mongo
volumes:
- mongodata:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: player-db
db1:
image: mongo
volumes:
- mongodata:/data/db1
ports:
- 27018:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: game-db
db2:
image: mongo
volumes:
- mongodata:/data/db2
ports:
- 27019:27017
restart: always
environment:
MONGO_INITDB_ROOT_DATABASE: score-db
volumes:
mongodata:
I do not know how to solve this problem.
I am beginner in Docker.
The problem here is with the network. Kindly understand that each container is a kind of a separate VM. Here, You are trying to connect to localhost. This will not work as each of the containers will resolve to it's own Loop back.
You can solve this in multiple ways:
Since all the containers are connected to the same docker network you can use container names to resolve respective ones. So here you can use db:27017 to connect to MongoDB.(Recommended)
Since you have port bonded all the containers you can use of the host machine instead of localhost. So if your host machine's private IP is 10.0.0.1 then use 10.0.0.1:27017 to connect to MongoDB.
Use host network, in this case, port binding instruction will be useless as it will be ignored and every service gets bonded automatically to their own port and you can use localhost to connect each of them (Not recommended)
References:
Networking overview
Use bridge networks
Networking in Compose
The system I use is centos7 and I had disabled selinux
docker-compose.yaml:
version: "2"
services:
web-service:
build:
context: .
dockerfile: springboot.dockerfile
container_name: smweb-container
ports:
- "8088:8888"
# networks:
# - net-spring-db
depends_on:
- db-service
db-service:
build:
context: .
dockerfile: sql.dockerfile
container_name: mysql-container
restart: always
ports:
- "3306:3306"
networks:
- net-spring-db
## environment:
## MYSQL_ROOT_PASSWORD: 123
environment:
MYSQL_DATABASE: rbac
MYSQL_USER: h2t
MYSQL_PASSWORD: 123456
networks:
net-spring-db:
driver: bridge
application.properties:
server.port=8888
## user container name to replace ip
spring.datasource.url=jdbc:mysql://mysql-container:3306/rbac?characterEncoding=utf8&useSSL=false
spring.datasource.username=h2t
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis-plus.mapper-locations=classpath*:/mapper/*.xml
mybatis-plus.type-aliases-package=com.h2t.study.pojo
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
When I use docker-compose up
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
smweb-container |
smweb-container | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
smweb-container | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
smweb-container | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
smweb-container | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
smweb-container | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
smweb-container | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:983) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2252) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
smweb-container | at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
smweb-container | at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
smweb-container | at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
smweb-container | at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) ~[mysql-connector-java-5.1.37.jar!/:5.1.37]
smweb-container | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.2.0.jar!/:na]
smweb-container | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.2.0.jar!/:na]
I can use docker exec -it mysql-container bash command to enter mysql container and I can also use mysql -uh2t -p123456 to connect mysql.At the same time,I check the table,it was initialized,but SpringBoot App still can't connect to MySQL.
Can you help me,thanks in advance.