Hi,
I am working on an application that needs a mongoDB with replicaSet. I would like to recreate a single-node cluster with replicaSet locally with docker compose, also adding mongo-express to more easily access data
I can connect to mongoDB via mongo-express and compass until I activate replicationSet. As soon as I enter the mongo machine and run the rs.initiate(…) command then mongo-express continues to work, but compass refuses me the connection with error
connect ECONNREFUSED 127.0.0.1:27017
I would like to launch
- mongoDB on port 37017
- mongo-express on port 8082
Below is my docker-compose.yml
services:
mongo-replica-1:
image: mongo:latest
container_name: orb-replica-mongodb-1
ports:
- 37017:27017
networks:
- MONGO
volumes:
- ./mongodb:/data/db
- ./configdb:/data/configdb
command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
mongo-express:
image: mongo-express:latest
container_name: orb-replica-mongo-express
environment:
- ME_CONFIG_MONGODB_URL=mongodb://mongo-replica-1:27017/?authSource=admin
ports:
- 8082:8081
networks:
- MONGO
depends_on:
- mongo-replica-1
networks:
MONGO:
name: MONGO
This is the rs.status()
rs0 [direct: primary] test> rs.status()
{
set: 'rs0',
date: ISODate('2024-07-03T08:54:02.738Z'),
myState: 1,
term: Long('2'),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long('2000'),
majorityVoteCount: 1,
writeMajorityCount: 1,
votingMembersCount: 1,
writableVotingMembersCount: 1,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
lastCommittedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
appliedOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
durableOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
lastAppliedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
lastDurableWallTime: ISODate('2024-07-03T08:53:59.332Z')
},
lastStableRecoveryTimestamp: Timestamp({ t: 1719996789, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate('2024-07-03T07:46:07.709Z'),
electionTerm: Long('2'),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long('-1') },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1719992763, i: 23 }), t: Long('1') },
numVotesNeeded: 1,
priorityAtElection: 1,
electionTimeoutMillis: Long('10000'),
newTermStartDate: ISODate('2024-07-03T07:46:07.712Z'),
wMajorityWriteAvailabilityDate: ISODate('2024-07-03T07:46:07.714Z')
},
members: [
{
_id: 0,
name: 'mongo-replica-1:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 4076,
optime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
optimeDate: ISODate('2024-07-03T08:53:59.000Z'),
lastAppliedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
lastDurableWallTime: ISODate('2024-07-03T08:53:59.332Z'),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1719992767, i: 1 }),
electionDate: ISODate('2024-07-03T07:46:07.000Z'),
configVersion: 1,
configTerm: 2,
self: true,
lastHeartbeatMessage: ''
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1719996839, i: 1 }),
signature: {
hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
keyId: Long('0')
}
},
operationTime: Timestamp({ t: 1719996839, i: 1 })
}
Any idea? I also added in hosts file the following rule
127.0.0.1 mongo-replica-1
Thanks
Daniele