Cannot connect to mongodb replica remote server

I create mongoDB replica with docker file

FROM mongo:latest
RUN echo "rs.initiate({'_id':'rs', members: [{'_id':1, 'host':''}]});" > "/docker-entrypoint-initdb.d/init_replicaset.js"
RUN echo "12345678" > "/tmp/key.file"
RUN chmod 600 /tmp/key.file
RUN chown 999:999 /tmp/key.file

CMD ["mongod", "--replSet", "rs", "--bind_ip_all", "--keyFile", "/tmp/key.file", "--enableMajorityReadConcern"]

after run, i can’t connect to mongodb server via mongo compass and in log received this message

{“t”:{"$date":“2022-06-20T09:46:27.333+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22943, “ctx”:“listener”,“msg”:“Connection accepted”,“attr”:{“remote”:“”,“uuid”:“a7570807-78a9-4351-a963-6f785f9ea15b”,“connectionId”:7534,“connectionCount”:16}}
{“t”:{"$date":“2022-06-20T09:46:29.902+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22943, “ctx”:“listener”,“msg”:“Connection accepted”,“attr”:{“remote”:“”,“uuid”:“fafcf29a-25ac-4a89-aa66-5f62b963bda7”,“connectionId”:7535,“connectionCount”:17}}
{“t”:{"$date":“2022-06-20T09:46:29.903+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:51800, “ctx”:“conn7535”,“msg”:“client metadata”,“attr”:{“remote”:“”,“client”:“conn7535”,“doc”:{“application”:{“name”:“MongoDB Shell”},“driver”:{“name”:“MongoDB Internal Client”,“version”:“5.0.9”},“os”:{“type”:“Linux”,“name”:“Ubuntu”,“architecture”:“x86_64”,“version”:“20.04”}}}}
{“t”:{"$date":“2022-06-20T09:46:29.932+00:00”},“s”:“I”, “c”:“ACCESS”, “id”:20250, “ctx”:“conn7535”,“msg”:“Authentication succeeded”,“attr”:{“mechanism”:“SCRAM-SHA-256”,“speculative”:true,“principalName”:“ramooz”,“authenticationDatabase”:“admin”,“remote”:“”,“extraInfo”:{}}}
{“t”:{"$date":“2022-06-20T09:46:29.943+00:00”},“s”:“I”, “c”:“COMMAND”, “id”:21577, “ctx”:“conn7535”,“msg”:“Initiate: no configuration specified. Using a default configuration for the set”}
{“t”:{"$date":“2022-06-20T09:46:29.944+00:00”},“s”:“I”, “c”:“COMMAND”, “id”:21578, “ctx”:“conn7535”,“msg”:“Created configuration for initiation”,“attr”:{“config”:"{ _id: “rs”, version: 1, members: [ { _id: 0, host: “f0f4572d950a:27017” } ] }"}}
{“t”:{"$date":“2022-06-20T09:46:29.944+00:00”},“s”:“I”, “c”:“REPL”, “id”:21356, “ctx”:“conn7535”,“msg”:“replSetInitiate admin command received from client”}
{“t”:{"$date":“2022-06-20T09:46:29.950+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:22944, “ctx”:“conn7535”,“msg”:“Connection ended”,“attr”:{“remote”:“”,“uuid”:“fafcf29a-25ac-4a89-aa66-5f62b963bda7”,“connectionId”:7535,“connectionCount”:16}}

Server selection timed out after 30000 ms

Hi @Ja7ad,

Personally, I like to create my Single Node Replica Sets with an alias that I have in my ~/.bashrc:

alias mdb='docker run --rm -d -p 27017:27017 -h $(hostname) --name mongo mongo:5.0.9 --replSet=test && sleep 4 && docker exec mongo mongo --eval "rs.initiate();"'

Less complicated in my opinion and works just fine to test stuff quickly and run some tests.

In your case, you are missing a few things. I think because you added the keyFile constaints, this requires Auth activated.

So I updated your Dockerfile like this:

FROM mongo:5.0.9
RUN echo "rs.initiate({'_id':'rs', members: [{'_id':1, 'host':''}]});" > "/docker-entrypoint-initdb.d/init_replicaset.js"
RUN echo "12345678" > "/tmp/key.file"
RUN chmod 600 /tmp/key.file
RUN chown 999:999 /tmp/key.file

CMD ["mongod", "--replSet", "rs", "--bind_ip_all", "--auth", "--keyFile", "/tmp/key.file", "--enableMajorityReadConcern"]

And you didn’t provide your docker run command so I made my own version:

docker build -t mabeulux/mdb . && docker run --rm -d -p 27017:27017 -h $(hostname) --name mongo mabeulux/mdb:latest

Finally, I have to create the root user. So I connect like this:

docker exec -it mongo mongosh --quiet

Then I create the root user:

use admin
db.createUser({user: "max", pwd:"secret", roles: ["root"]})

And then I can finally connect using Compass with this URI:


So like I said, my solution is a one liner so a bit easier :smiley: !


I found bitnami mongodb image, make easy replica with this image

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.