-
-
Save asoorm/7822cc742831639c93affd734e97ce4f to your computer and use it in GitHub Desktop.
version: "3" | |
services: | |
mongo1: | |
hostname: mongo1 | |
container_name: localmongo1 | |
image: mongo:4.0-xenial | |
expose: | |
- 27017 | |
ports: | |
- 27011:27017 | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] | |
mongo2: | |
hostname: mongo2 | |
container_name: localmongo2 | |
image: mongo:4.0-xenial | |
expose: | |
- 27017 | |
ports: | |
- 27012:27017 | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] | |
mongo3: | |
hostname: mongo3 | |
container_name: localmongo3 | |
image: mongo:4.0-xenial | |
expose: | |
- 27017 | |
ports: | |
- 27013:27017 | |
restart: always | |
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ] |
> rs.initiate({ ... _id : 'rs0', ... members: [ ... { _id : 0, host : "{YOUR_LOCAL_IP}:27717" }, ... { _id : 1, host : "{YOUR_LOCAL_IP}:27718" }, ... { _id : 2, host : "{YOUR_LOCAL_IP}:27720", arbiterOnly: true } ... ] ... }) { "operationTime" : Timestamp(0, 0), "ok" : 0, "errmsg" : "No host described in new configuration 1 for replica set rs0 maps to this node", "code" : 93, "codeName" : "InvalidReplicaSetConfig", "$clusterTime" : { "clusterTime" : Timestamp(0, 0), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } >
I am getting this error in window. I tested it in mac which works fine
I was able to solve the problem. Because I googled my ip address which is not configurable in mongoshell. running ipconfig to find the actual ipv4 address solve my issue
I resolved this problem using my local (host) IP:
rs.initiate({ _id : 'rs0', members: [ { _id : 0, host : "{YOUR_LOCAL_IP}:27011" }, { _id : 1, host : "{YOUR_LOCAL_IP}:27012" }, { _id : 2, host : "{YOUR_LOCAL_IP}:27013" } ] })
This worked for me too 👍
Im confused - Did you exec into the mongo node container in order to setup the cluster? e.g. follow this to the letter? https://gist.github.com/asoorm/7822cc742831639c93affd734e97ce4f#gistcomment-2707084
Because the docker containers for Mongo are in the same network. e.g.mongo1
should be able to communicate with mongo2 via it's hostname & port.mongo2:27017
.
If your mongo1 needs to go via the host machine (outside of docker) and come back into docker again, something clearly is not working.@asoorm, yes, I followed the steps exactly. I found that the issue was from my host machine resolving the hostnames (mongo1, mongo2, mongo3).
I fixed this by adding the hostnames to
/etc/hosts
:sudo echo "127.0.0.1 mongo1 127.0.0.1 mongo2 127.0.0.1 mongo3" | sudo tee -a /etc/hosts
and then this worked as expected:
rs.initiate( { _id : 'rs0', members: [ { _id : 0, host : "mongo1:27017" }, { _id : 1, host : "mongo2:27017" }, { _id : 2, host : "mongo3:27017" } ] } )
This worked for me, thanks.
Follow the below compose file, no more manual changes
mongodb: hostname: mongodb container_name: mongodb image: mongo:latest environment: MONGO_INITDB_DATABASE: moviebooking MONGO_REPLICA_SET_NAME: rs0 volumes: - ./mongo-initdb.d:/docker-entrypoint-initdb.d expose: - 27017 ports: - "27017:27017" restart: always healthcheck: test: test $$(echo "rs.initiate().ok || rs.slaveOk().ok || rs.status().ok" | mongo --quiet) -eq 1 interval: 10s start_period: 30s entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
shell into the container:
docker exec -it mongodb /bin/bash
you should see the below
I know this has been here for awhile, but just doing this and liked the post by @mohansainani about doing this in a single docker image.
On a Mac, the only problem was that I was getting a error
Could not find host matching read preference { mode: \"nearest\" }
that indicated there was a network issue.
So.. I also used
sudo echo "127.0.0.1 mongodb" | sudo tee -a /etc/hosts
to make the host "findable"
After that I could connect with
mongo mongodb://localhost:27017/moviebooking?replicaSet=rs0
If that helps, bitnami/mongodb is a super good image.
mongodb:
image: bitnami/mongodb
environment:
MONGODB_REPLICA_SET_MODE: primary
ALLOW_EMPTY_PASSWORD: "yes"
I can run everything perfectly but I can not connect to the set by any client.
I can run everything perfectly but I can not connect to the set by any client.
i have same problem
hel;p
Error:
getaddrinfo ENOTFOUND mongodb-primary
If that helps, bitnami/mongodb is a super good image.
mongodb: image: bitnami/mongodb environment: MONGODB_REPLICA_SET_MODE: primary ALLOW_EMPTY_PASSWORD: "yes"
How did you configure the port
I have setup everything in this https://github.com/nguyenduyhust/docker-mongodb-replica-set.
Great if that's what you're looking for.
When I tried to connect with string URI mongodb://localhost:27011,localhost:27012,localhost:27013/?replicaSet=rs0
using Compass
I got the following error
getaddrinfo EAI_AGAIN mongo1
By the way, you all would have to expose all Mongo replicaset instances ports in order to get connected to them, so I think It's redundant being exposing ports, It's better to use host network mode.
I was looking for a simple Compose for this and, unfortunately, I didn't find one complete and simple at the same time, because of that I made one as simple as possible where the secondary ones ingress their selfies on the master without any third-party script.
I hope you all find it useful
version: "3"
services:
node0:
image: mongo
restart: always
command: --replSet rs0 --bind_ip_all
network_mode: host
healthcheck:
test: >
mongosh --eval "try{rs.initiate().ok}catch(e){rs.status().ok}"
interval: 3s
node1:
image: mongo
depends_on:
- node0
restart: always
command: --replSet rs0 --bind_ip_all --port 27018
network_mode: host
environment:
PORT: 27018
healthcheck:
test: >
mongosh --host localhost:27017 --eval "var me = '$${HOSTNAME}:$${PORT}'; rs.status().members.some(m => m.name == me) || rs.add({host: me})"
interval: 3s
node2:
image: mongo
depends_on:
- node0
restart: always
command: --replSet rs0 --bind_ip_all --port 27019
network_mode: host
environment:
PORT: 27019
healthcheck:
test: >
mongosh --host localhost:27017 --eval "var me = '$${HOSTNAME}:$${PORT}'; rs.status().members.some(m => m.name == me) || rs.add({host: me})"
interval: 3s
docker exec -it mongodb /bin/bash
This worked perfectly for me to create and start the mongodb set on docker containers. I can also connect to any of the servers using compass. But when I tried to open a connection from another server in the docker network (a php server), using php mongodb client, I got "Authentication failed" using this url:
mongodb://172.24.0.5:27017,172.24.0.3:27017,172.24.0.4:27017?replicaSet=dbrs
I am also sending my username and password, same that I am using in the compass connection url. The internal ips are correct, so that is not the problem.
So, basically, replicaset is up and running, I can connect from MongoDB Compass but I can't connect from my PHP code from a docker container in the same docker network.
Do not use x.509 Certificates or keyfile?