Data is not reflecting in my MongoDB database

Sorry, if my question sound dumb, I am just starting off.

I am trying to sync data from MongoDB to Elasticsearch. I am using Monstache for the same.
When I send data from Postman, It says 201 ok, but the same doesn’t get reflected in my database.

I am using a cluster of 3 nodes(Primary-secondary-secondary), named mongo1, mongo2, mongo3.
when I ping mongo1, mongo2, mongo3, it says:
“Ping request could not find host mongo1. Please check the name and try again.”

when I ping its ip, it says, “Request Timeout”

Here’s my docker-compose file:

version: '3'
services:
  mongo1:
    container_name: mongo1
    image: mongo
    restart: on-failure 
    ports:
      - "27017:27017"
    command: mongod --replSet myReplicaSet --bind_ip localhost,mongo1
    networks:
      - mongo-network
  mongo2:
    container_name: mongo2
    image: mongo
    restart: on-failure
    ports:
      - "27018:27017"
    command: mongod --replSet myReplicaSet --bind_ip localhost,mongo2
    networks:
      - mongo-network
  mongo3:
    container_name: mongo3
    image: mongo
    restart: on-failure
    ports:
      - "27019:27017"
    command: mongod --replSet myReplicaSet --bind_ip localhost,mongo3
    networks:
      - mongo-network
  mongoexpress:
    container_name: mongoExpress
    image: mongo-express
    restart: on-failure
    ports:
      - "8081:8081"
    environment:
      # - ME_CONFIG_MONGODB_SERVER=mongo1,mongo2,mongo3
      # - ME_CONFIG_MONGODB_URL="mongodb://mongo1:27017,mongo2:27018,mongo3:27019"
      - ME_CONFIG_MONGODB_SERVER=mongo1,mongo2,mongo3
      - ME_CONFIG_MONGODB_URL="mongodb://writeUser:secret@mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=myReplicaSet"
      # Add your user credentials for authentication
      - ME_CONFIG_MONGODB_ADMINUSERNAME=writeUser
      - ME_CONFIG_MONGODB_ADMINPASSWORD=secret
    networks:
      - mongo-network
  elasticsearch:
    container_name: elasticsearch
    image: elasticsearch:8.11.1
    restart: on-failure
    ports:
      - "9200:9200"
    networks:
      - mongo-network
  kibana:
    container_name: kibana
    image: kibana:8.11.1
    restart: on-failure
    ports:
      - "5601:5601"
    networks:
      - mongo-network
  monstache:
    container_name: monstache
    image: rwynn/monstache
    command: -f /config/config.toml
    restart: on-failure
    volumes:
      - ./monstache-config:/config
    networks:
      - mongo-network

networks:
  mongo-network:
    external: true

Here’s my Docker logs:
mongo1:

{"t":{"$date":"2023-12-06T20:36:19.581+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-5","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":2321}}

{"t":{"$date":"2023-12-06T20:36:21.561+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"QueryAnalysisConfigurationsRefresher","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":1027}}

{"t":{"$date":"2023-12-06T20:36:24.749+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.6:37102","uuid":{"uuid":{"$uuid":"6b5ea1b9-a59e-4660-94f4-a6cc88fff709"}},"connectionId":162,"connectionCount":13}}

{"t":{"$date":"2023-12-06T20:36:24.756+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn162","msg":"client metadata","attr":{"remote":"172.18.0.6:37102","client":"conn162","doc":{"driver":{"name":"NetworkInterfaceTL-MirrorMaestro","version":"7.0.3"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"22.04"}}}}

{"t":{"$date":"2023-12-06T20:36:24.765+00:00"},"s":"I",  "c":"NETWORK",  "id":6788700, "ctx":"conn162","msg":"Received first command on ingress connection since session start or auth handshake","attr":{"elapsedMillis":9}}

{"t":{"$date":"2023-12-06T20:36:27.629+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-5","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":1252}}

{"t":{"$date":"2023-12-06T20:36:29.659+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-9","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":2100}}

Mongo2:

{"t":{"$date":"2023-12-06T20:37:24.731+00:00"},"s":"I",  "c":"REPL",     "id":3873113, "ctx":"ReplCoordExtern-0","msg":"Cannot select sync source which is not ahead of me","attr":{"syncSourceCandidate":"mongo1:27017","syncSourceCandidateLastAppliedOpTime":{"ts":{"$timestamp":{"t":1701895034,"i":1}},"t":34},"lastOpTimeFetched":{"ts":{"$timestamp":{"t":1701895044,"i":1}},"t":34}}}

{"t":{"$date":"2023-12-06T20:37:26.175+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-9","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":2615}}

{"t":{"$date":"2023-12-06T20:37:28.198+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-9","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":1315}}

{"t":{"$date":"2023-12-06T20:37:29.755+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoordExtern-7","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":5226}}

{"t":{"$date":"2023-12-06T20:37:31.558+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"QueryAnalysisConfigurationsRefresher","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":2140}}

Mongo3:

{"t":{"$date":"2023-12-06T20:37:58.414+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-0","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":2465}}

{"t":{"$date":"2023-12-06T20:38:08.462+00:00"},"s":"I",  "c":"NETWORK",  "id":6496702, "ctx":"ReplCoord-14","msg":"Acquired connection for remote operation and completed writing to wire","attr":{"durationMicros":6440}}

Here’s my Replica status:

{
  set: 'myReplicaSet',
  date: ISODate("2023-12-06T20:39:49.863Z"),
  myState: 2,
  term: Long("34"),
  syncSourceHost: 'mongo3:27017',
  syncSourceId: 2,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
    lastCommittedWallTime: ISODate("2023-12-06T20:39:45.801Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
    appliedOpTime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
    durableOpTime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
    lastAppliedWallTime: ISODate("2023-12-06T20:39:45.801Z"),
    lastDurableWallTime: ISODate("2023-12-06T20:39:45.801Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1701895165, i: 1 }),
  electionParticipantMetrics: {
    votedForCandidate: true,
    electionTerm: Long("34"),
    lastVoteDate: ISODate("2023-12-06T20:10:42.003Z"),
    electionCandidateMemberId: 2,
    voteReason: '',
    lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1701893405, i: 1 }), t: Long("33") },
    maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1701893405, i: 1 }), t: Long("33") },
    priorityAtElection: 1,
    newTermStartDate: ISODate("2023-12-06T20:10:42.222Z"),
    newTermAppliedDate: ISODate("2023-12-06T20:10:42.784Z")
  },
  members: [
    {
      _id: 0,
      name: 'mongo1:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 1767,
      optime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
      optimeDate: ISODate("2023-12-06T20:39:45.000Z"),
      lastAppliedWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      lastDurableWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      syncSourceHost: 'mongo3:27017',
      syncSourceId: 2,
      infoMessage: '',
      configVersion: 1,
      configTerm: 34,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'mongo2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 1758,
      optime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
      optimeDurable: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
      optimeDate: ISODate("2023-12-06T20:39:45.000Z"),
      optimeDurableDate: ISODate("2023-12-06T20:39:45.000Z"),
      lastAppliedWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      lastDurableWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      lastHeartbeat: ISODate("2023-12-06T20:39:48.091Z"),
      lastHeartbeatRecv: ISODate("2023-12-06T20:39:48.015Z"),
      pingMs: Long("146"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongo3:27017',
      syncSourceId: 2,
      infoMessage: '',
      configVersion: 1,
      configTerm: 34
    },
    {
      _id: 2,
      name: 'mongo3:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 1758,
      optime: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
      optimeDurable: { ts: Timestamp({ t: 1701895185, i: 1 }), t: Long("34") },
      optimeDate: ISODate("2023-12-06T20:39:45.000Z"),
      optimeDurableDate: ISODate("2023-12-06T20:39:45.000Z"),
      lastAppliedWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      lastDurableWallTime: ISODate("2023-12-06T20:39:45.801Z"),
      lastHeartbeat: ISODate("2023-12-06T20:39:48.103Z"),
      lastHeartbeatRecv: ISODate("2023-12-06T20:39:48.206Z"),
      pingMs: Long("128"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1701893442, i: 1 }),
      electionDate: ISODate("2023-12-06T20:10:42.000Z"),
      configVersion: 1,
      configTerm: 34
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1701895185, i: 1 }),
    signature: {
      hash: Binary.createFromBase64("AAAAAAAAAAAAAAAAAAAAAAAAAAA=", 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1701895185, i: 1 })
}

Mongo URI used in my main.go file:

clientOptions := options.Client().ApplyURI("mongodb://writeUser:secret@mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=myReplicaSet")

PS:
All are running on the same docker network.
Tried changing DNS Server to 8.8.8.8 & 1.1.1.1 but to no avail
Turned off windows defender, but same result.
Also POST req. from Postman is taking way longer, like 30s

How to solve this?2023-12-06T18:30:00Z

How did you obtained the replica set status? Share the exact command you used.

This is not surprising since these public DNS servers have nothing to do with host names of your internal docker network.

The URI

does not match your replica set configuration. Both mongo2 and mongo3 listen to 27017

and

Where did you run the pings that failed? I ask because I am surprised that with ping IP it failed with Request Timeout.

I logged into the mongo shell by

docker exec -it mongo1 mongosh

Now, to get the status:

rs.status()

I got the IP of containers using the following command, I ran it in Powershell terminal of VS Code:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongo1

Got the IP: 172.18.0.6

I pingged it in Powershell:

ping mongo1

Got:

Ping request could not find host mongo1. Please check the name and try again.

Then tried:

ping 172.18.0.6

Got:

Pinging 172.18.0.6 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 172.18.0.6:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

But when I ping the same IP in Mongo Shell, It seems to work fine:

db.runCommand({ ping: "172.18.0.6" })

Result:

{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1701932839, i: 1 }),
    signature: {
      hash: Binary.createFromBase64("AAAAAAAAAAAAAAAAAAAAAAAAAAA=", 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1701932839, i: 1 })
}

Attaching Screenshot for reference:

Here’s another SS for reference:

I am not very familiar with Docker, but I should and I guess I will eventually, so I might be totally off.

The facts seem to be

1 - doing anything, ping, mongosh, …, using the docker command works
2 - doing anything directly from your host command line does not work

My only guess is that your docker network is not visible from the host. I would try to see if a configuration different from

would work. May be external has to be false. Or something else.

Everything is working fine now,

Here the updated docker compose file:

https://github.com/Praveen005/Mongodb-Replica-Set-Docker-Compose/tree/main

1 Like