Not connecting in the mongodb compass - getaddrinfo ENOTFOUND db-mongo-node17
gulskr opened this issue · 6 comments
When i am trying run using the docker-compose.yml version 5.0.24 and I am doing all this in a ec2 server AWS
version: "3.0" # Update the version to "3.0" as per your requirement
volumes:
db-mongo-keys26:
db-mongo-data-26:
db-mongo-data-27:
db-mongo-data-28:
networks:
db-cluster26:
driver: bridge
ipam:
driver: default
config:
- subnet: 162.10.21.0/24
services:
db-mongo-keys26:
image: depop/openssl-bats
volumes:
- db-mongo-keys26:/mongo-conf
command: 'bash -c "openssl rand -base64 741 > /mongo-conf/mongodb-keyfile; chmod 600 /mongo-conf/mongodb-keyfile; chown 999 /mongo-conf/mongodb-keyfile"'
# Node1
db-mongo-node17:
container_name: db-mongo-node17
image: mongo:5.0.24 # Update the MongoDB version to "5.0.24"
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-26:/data/db
env_file:
./mongod.env
ports:
- 27017:27017 # Change the host port to 27017
command: 'mongod --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
networks:
db-cluster26:
# Node2
db-mongo-node18:
container_name: db-mongo-node18
image: mongo:5.0.24
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-27:/data/db
env_file:
./mongod.env
ports:
- 27018:27017 # Change the host port to 27018
command: 'mongod --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
networks:
db-cluster26:
# Node3
db-mongo-node19:
container_name: db-mongo-node19
image: mongo:5.0.24
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-28:/data/db
env_file:
./mongod.env
ports:
- 27019:27017 # Change the host port to 27019
command: 'mongod --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
networks:
db-cluster26:
This is to initiate the replica set
rs.initiate({
_id: "db-ppd",
members: [
{_id: 0, host: "db-mongo-node17:27017"},
{_id: 1, host: "db-mongo-node18:27017"},
{_id: 2, host: "db-mongo-node19:27017"}
]
})
And i have created a root user and trying to connect into my mongodb compass like this and this doesn't work it give an error - getaddrinfo ENOTFOUND db-mongo-node17 mongodb://root:*****@host_address:27017,host_address:27018,host_address:27018/?replicaSet=db-ppd
But when i try like this - it show me the db and work fine
mongodb://root:root@host_address:27017/?directConnection=true
I have no idea why this doesn't work when i don't try with the direct connection
If you need the rs.status , if it helps here it is
db-ppd:PRIMARY> rs.status()
{
"set" : "db-ppd",
"date" : ISODate("2024-02-26T12:55:41.236Z"),
"myState" : 1,
"term" : NumberLong(8),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"lastCommittedWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"appliedOpTime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"durableOpTime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"lastAppliedWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastDurableWallTime" : ISODate("2024-02-26T12:55:38.624Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1708952118, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2024-02-26T11:41:28.430Z"),
"electionTerm" : NumberLong(8),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1708947657, 1),
"t" : NumberLong(6)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2024-02-26T11:41:28.443Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2024-02-26T11:41:28.752Z")
},
"members" : [
{
"_id" : 0,
"name" : "db-mongo-node17:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4463,
"optime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2024-02-26T12:55:38Z"),
"optimeDurableDate" : ISODate("2024-02-26T12:55:38Z"),
"lastAppliedWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastDurableWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastHeartbeat" : ISODate("2024-02-26T12:55:40.928Z"),
"lastHeartbeatRecv" : ISODate("2024-02-26T12:55:39.567Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "db-mongo-node19:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 8
},
{
"_id" : 1,
"name" : "db-mongo-node18:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4462,
"optime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2024-02-26T12:55:38Z"),
"optimeDurableDate" : ISODate("2024-02-26T12:55:38Z"),
"lastAppliedWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastDurableWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastHeartbeat" : ISODate("2024-02-26T12:55:40.929Z"),
"lastHeartbeatRecv" : ISODate("2024-02-26T12:55:39.956Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "db-mongo-node19:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 8
},
{
"_id" : 2,
"name" : "db-mongo-node19:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4465,
"optime" : {
"ts" : Timestamp(1708952138, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2024-02-26T12:55:38Z"),
"lastAppliedWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"lastDurableWallTime" : ISODate("2024-02-26T12:55:38.624Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1708947688, 1),
"electionDate" : ISODate("2024-02-26T11:41:28Z"),
"configVersion" : 1,
"configTerm" : 8,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1708952138, 1),
"signature" : {
"hash" : BinData(0,"zl2L6hBW70Cr5140qnwfseqG0fA="),
"keyId" : NumberLong("7339807892901462020")
}
},
"operationTime" : Timestamp(1708952138, 1)
}
If anyone can help, it will be very much appreciated
Connecting to the replicaset works from within the network. It seems to be an issue when connecting from outside.
There are a few ways you could get around this problem.
- Implement a reverse proxy using nginx or similar.
- Add explicit ip addresses, change the ports mapping in the container and add the hosts to your hostfile,.
The following worked for me
compose
version: "3.0" # Update the version to "3.0" as per your requirement
volumes:
db-mongo-keys26:
db-mongo-data-26:
db-mongo-data-27:
db-mongo-data-28:
networks:
db-cluster26:
driver: bridge
ipam:
driver: default
config:
- subnet: 162.10.21.0/24
services:
db-mongo-keys26:
image: depop/openssl-bats
volumes:
- db-mongo-keys26:/mongo-conf
command: 'bash -c "openssl rand -base64 741 > /mongo-conf/mongodb-keyfile; chmod 600 /mongo-conf/mongodb-keyfile; chown 999 /mongo-conf/mongodb-keyfile"'
# Node1
db-mongo-node17:
container_name: db-mongo-node17
image: mongo:5.0.24 # Update the MongoDB version to "5.0.24"
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-26:/data/db
env_file:
./mongod.env
ports:
- 27017:27017 # Change the host port to 27017
command: 'mongod --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
# Node2
db-mongo-node18:
container_name: db-mongo-node18
image: mongo:5.0.24
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-27:/data/db
env_file:
./mongod.env
ports:
- 27018:27018 # Change the host port to 27018
command: 'mongod --port 27018 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
# Node3
db-mongo-node19:
container_name: db-mongo-node19
image: mongo:5.0.24
volumes:
- db-mongo-keys26:/opt/keyfile
- db-mongo-data-28:/data/db
env_file:
./mongod.env
ports:
- 27019:27019 # Change the host port to 27019
command: 'mongod --port 27019 --auth --keyFile /opt/keyfile/mongodb-keyfile --replSet db-ppd'
depends_on:
- db-mongo-keys26
Command:
rs.initiate({
_id: "db-ppd",
members: [
{_id: 0, host: "db-mongo-node17:27017"},
{_id: 1, host: "db-mongo-node18:27018"},
{_id: 2, host: "db-mongo-node19:27019"}
]
})
Host file on host
127.0.0.1 db-mongo-node17
127.0.0.1 db-mongo-node18
127.0.0.1 db-mongo-node19
Connection string:
mongodb://root:example@db-mongo-node17:27017,db-mongo-node18:27018,db-mongo-node19:27019/?replicaSet=db-ppd
@LaurentGoderre are you working on your local system or some clous because the same thing i am trying on my aws ec2, it is giving error WARNING: Some networks were defined but are not used by any service: db-cluster26
Creating network "ec2-user_default" with the default driver
Creating volume "ec2-user_db-mongo-keys26" with default driver
Creating volume "ec2-user_db-mongo-data-26" with default driver
Creating volume "ec2-user_db-mongo-data-27" with default driver
Creating volume "ec2-user_db-mongo-data-28" with default driver
Creating ec2-user_db-mongo-keys26_1 ... done
Creating db-mongo-node19 ...
Creating db-mongo-node17 ... error
Creating db-mongo-node18 ...
Creating db-mongo-node19 ... done
de17 (07973999b06c76bc04444a43530a0aff03a9dbc623e9ac18dd2019e7f5748f63): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: Creating db-mongo-node18 ... done
ERROR: for db-mongo-node17 Cannot start service db-mongo-node17: driver failed programming external connectivity on endpoint db-mongo-node17 (07973999b06c76bc04444a43530a0aff03a9dbc623e9ac18dd2019e7f5748f63): Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use
ERROR: Encountered errors while bringing up the project.
[ec2-user@ip-12-1-27-125 ~]$ docker exec -it db-mongo-node18 bash
root@5252644ac2d7:/# mongo
MongoDB shell version v5.0.24
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
If you have any reference to install it with full setup with docker-compose or something, It will be very helpful on Amazon linux2, 5.0.24
This was on a local setup. Maybe you can use all non-default ports to avoid port in use error (27018, 27019, 27020)