Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoCompass not connecting #5

Open
felipegenef opened this issue Aug 20, 2022 · 9 comments
Open

MongoCompass not connecting #5

felipegenef opened this issue Aug 20, 2022 · 9 comments

Comments

@felipegenef
Copy link

felipegenef commented Aug 20, 2022

Hey, I'm trying to test an RS locally and connect to it with mongo compass but I'm having a hard time trying to do so. Everything looks fine until i try to make the connection.

Connection URL

mongodb://localhost:27021,localhost:27022,localhost:27023/?replicaSet=dbrs&tls=false

docker-compose.yml

version: "3.8"

services:
  mongo1:
    container_name: mongo1
    image: mongo:4.4
    volumes:
      - ~/mongors/data1:/data/db
      - ./scripts/rs-init.sh:/scripts/rs-init.sh

    networks:
      - mongors-network
    ports:
      - 27021:27017
    links:
      - mongo2
      - mongo3
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs"]
  mongo2:
    container_name: mongo2
    image: mongo:4.4
    volumes:
      - ~/mongors/data2:/data/db
    networks:
      - mongors-network
    ports:
      - 27022:27017
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs"]
  mongo3:
    container_name: mongo3
    image: mongo:4.4
    volumes:
      - ~/mongors/data3:/data/db
    networks:
      - mongors-network
    ports:
      - 27023:27017
    restart: always
    entrypoint: ["/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs"]

networks:
  mongors-network:
    driver: bridge

rs-init.sh

#!/bin/bash

mongo <<EOF
var config = {
    "_id": "dbrs",
    "version": 1,
    "members": [
        {
            "_id": 1,
            "host": "mongo1:27017",
            "priority": 3
        },
        {
            "_id": 2,
            "host": "mongo2:27017",
            "priority": 2
        },
        {
            "_id": 3,
            "host": "mongo3:27017",
            "priority": 1
        }
    ]
};
rs.initiate(config, { force: true });
rs.status();
EOF

rs-init.sh

#!/bin/bash

docker compose up -d

sleep 5


docker exec mongo1 /scripts/rs-init.sh

When i "docker exec -it mongo1 mongo" and "rs.status()"

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e2d79236-681e-4ae6-928e-e03ae11fb767") }
MongoDB server version: 4.4.16
{
	"operationTime" : Timestamp(1661026048, 1),
	"ok" : 0,
	"errmsg" : "already initialized",
	"code" : 23,
	"codeName" : "AlreadyInitialized",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1661026048, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
{
	"set" : "dbrs",
	"date" : ISODate("2022-08-20T20:08:23.821Z"),
	"myState" : 2,
	"term" : NumberLong(40),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1661026048, 1),
			"t" : NumberLong(40)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1661026048, 1),
			"t" : NumberLong(40)
		},
		"lastAppliedWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
		"lastDurableWallTime" : ISODate("2022-08-20T20:07:28.383Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1661025458, 1),
	"members" : [
		{
			"_id" : 1,
			"name" : "mongo1:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 5,
			"optime" : {
				"ts" : Timestamp(1661026048, 1),
				"t" : NumberLong(40)
			},
			"optimeDate" : ISODate("2022-08-20T20:07:28Z"),
			"lastAppliedWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"lastDurableWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 40,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 2,
			"name" : "mongo2:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 4,
			"optime" : {
				"ts" : Timestamp(1661026048, 1),
				"t" : NumberLong(40)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1661026048, 1),
				"t" : NumberLong(40)
			},
			"optimeDate" : ISODate("2022-08-20T20:07:28Z"),
			"optimeDurableDate" : ISODate("2022-08-20T20:07:28Z"),
			"lastAppliedWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"lastDurableWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"lastHeartbeat" : ISODate("2022-08-20T20:08:23.322Z"),
			"lastHeartbeatRecv" : ISODate("2022-08-20T20:08:23.667Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 40
		},
		{
			"_id" : 3,
			"name" : "mongo3:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 4,
			"optime" : {
				"ts" : Timestamp(1661026048, 1),
				"t" : NumberLong(40)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1661026048, 1),
				"t" : NumberLong(40)
			},
			"optimeDate" : ISODate("2022-08-20T20:07:28Z"),
			"optimeDurableDate" : ISODate("2022-08-20T20:07:28Z"),
			"lastAppliedWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"lastDurableWallTime" : ISODate("2022-08-20T20:07:28.383Z"),
			"lastHeartbeat" : ISODate("2022-08-20T20:08:23.323Z"),
			"lastHeartbeatRecv" : ISODate("2022-08-20T20:08:23.629Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 40
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1661026048, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1661026048, 1)
}
@keithics
Copy link

@AstaDK
Copy link

AstaDK commented Mar 4, 2023

use this instead https://registry.hub.docker.com/r/candis/mongo-replica-set

@keithics How can we convert to docker-compose?

@felipegenef Can you resolve it? I'm facing the same problem.

@humkins
Copy link

humkins commented Mar 15, 2023

@felipegenef, have you tried to add 127.0.0.1 mongo1 mongo2 mongo3 line in your /etc/hosts?

@niket-yende
Copy link

niket-yende commented Apr 5, 2023

Hi, I am facing similar issue. I have setup the mongo replicaset on a windows 11 machine.
In my case below are the mongo container names:
test-db1, test-db2, test-db3

rs-init.sh

#!/bin/bash

mongo <<EOF
var config = {
    "_id": "dbrs",
    "version": 1,
    "members": [
        {
            "_id": 1,
            "host": "test-db1:27017",
            "priority": 3
        },
        {
            "_id": 2,
            "host": "test-db2:27017",
            "priority": 2
        },
        {
            "_id": 3,
            "host": "test-db3:27017",
            "priority": 1
        }
    ]
};
rs.initiate(config, { force: true });
rs.status();
EOF

docker exec -it test-db1 mongo

dbrs:PRIMARY> rs.status()
{
        "set" : "dbrs",
        "date" : ISODate("2023-04-05T06:01:25.328Z"),
        "myState" : 1,
        "term" : NumberLong(13),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1680674480, 1),
                        "t" : NumberLong(13)
                },
                "lastCommittedWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1680674480, 1),
                        "t" : NumberLong(13)
                },
                "readConcernMajorityWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                "appliedOpTime" : {
                        "ts" : Timestamp(1680674480, 1),
                        "t" : NumberLong(13)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1680674480, 1),
                        "t" : NumberLong(13)
                },
                "lastAppliedWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                "lastDurableWallTime" : ISODate("2023-04-05T06:01:20.837Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1680674470, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "electionTimeout",
                "lastElectionDate" : ISODate("2023-04-05T04:25:20.475Z"),
                "electionTerm" : NumberLong(13),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(0, 0),
                        "t" : NumberLong(-1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1680583648, 1),
                        "t" : NumberLong(12)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 3,
                "electionTimeoutMillis" : NumberLong(10000),
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2023-04-05T04:25:20.554Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2023-04-05T04:25:21.345Z")
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "test-db1:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 5795,
                        "optime" : {
                                "ts" : Timestamp(1680674480, 1),
                                "t" : NumberLong(13)
                        },
                        "optimeDate" : ISODate("2023-04-05T06:01:20Z"),
                        "lastAppliedWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                        "lastDurableWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1680668720, 1),
                        "electionDate" : ISODate("2023-04-05T04:25:20Z"),
                        "configVersion" : 1,
                        "configTerm" : 13,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 2,
                        "name" : "test-db2:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5775,
                        "optime" : {
                                "ts" : Timestamp(1680674480, 1),
                                "t" : NumberLong(13)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1680674480, 1),
                                "t" : NumberLong(13)
                        },
                        "optimeDate" : ISODate("2023-04-05T06:01:20Z"),
                        "optimeDurableDate" : ISODate("2023-04-05T06:01:20Z"),
                        "lastAppliedWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                        "lastDurableWallTime" : ISODate("2023-04-05T06:01:20.837Z"),
                        "lastHeartbeat" : ISODate("2023-04-05T06:01:25.326Z"),
                        "lastHeartbeatRecv" : ISODate("2023-04-05T06:01:24.181Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "test-db1:27017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 13
                },
                {
                        "_id" : 3,
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1680674480, 1)
}

I feel the setup for the mongodb replicaset is correct but there is some issue with windows not able to resolve the connection uri.

I tried the below uri from nodejs code as well as composs, but results in getaddrinfo ENOTFOUND test-db2:
mongodb://localhost:27099/test-indisi?replicaSet=dbrs&ssl=false

However, if i pass the directConnection parameter as true it works. But, i wish to pass multiple db instances instead of one.
mongodb://localhost:27099/test-indisi?directConnection=true&replicaSet=dbrs&ssl=false

Please advice on the same. I would appreciate the help. Thanks.

@tericcabrel
Copy link
Owner

Hello guys! I'm sorry for the late reply; there might have been an update.
I will try to reduce this weekend and let you know 👍🏾

@heithemabbes
Copy link

heithemabbes commented Apr 13, 2023

Hi, I face the same problem with docker compose
I'm using this URI in compass
mongodb://mongo1:27021,mongo2:27022,mongo3:27023/?replicaSet=dbrs

I've added
127.0.0.1 mongo1 mongo2 mongo3 to /etc/hosts

I get error in Compass : connect ECONNREFUSED 127.0.0.1:27017

It works only when I add direct connection :
mongodb://mongo1:27021/?replicaSet=dbrs&directConnection=true

@sibelius
Copy link

sibelius commented Jul 4, 2023

I'm getting this error

MongooseServerSelectionError: getaddrinfo ENOTFOUND mongo1

@RavenColEvol
Copy link

Here is the solution which I found working
docker-compose.yml

version: '3.1'

services:
  main:
    container_name: main
    hostname: main
    ports:
      - 27017:27017
    image: mongo:4.4
    command: mongod --replSet rs
    depends_on:
      - rs1
      - rs2
    volumes:
      - 
      - ./rs-init.sh:/scripts/rs-init.sh
    networks:
      - dbnetwork

  rs1:
    container_name: rs1
    hostname: rs1
    ports:
      - 27018:27018
    image: mongo:4.4
    command: mongod --replSet rs --port 27018
    networks:
      - dbnetwork

  rs2:
    container_name: rs2
    hostname: rs2
    ports:
      - 27019:27019
    image: mongo:4.4
    command: mongod --replSet rs --port 27019
    networks:
      - dbnetwork

networks:
  dbnetwork:

rs-init.sh

#!/bin/bash

read -r -d '' CMD <<EOF
rs.initiate(
  {
    _id : 'rs',
    members: [
      { _id : 0, host : 'main:27017', priority: 3 },
      { _id : 1, host : 'rs1:27018', priority: 2 },
      { _id : 2, host : 'rs2:27019', priority: 1 }
    ]
  },
  { force: true }
)
EOF

echo $CMD | mongo --host main

In your host (local computer) /etc/hosts add this

# DOCKER custom
0.0.0.0 main
0.0.0.0 rs1
0.0.0.0 rs2

@keithics @sibelius @humkins @niket-yende @heithemabbes

@sibelius
Copy link

sibelius commented Sep 4, 2023

You can also follow this https://dev.to/woovi/best-dx-for-mongodb-replica-set-43lc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants