mingo-app / mingo

Found a bug? have a FR ?
59 stars 3 forks source link

Cant connect to local mongo server created via docker #566

Closed joezappie closed 8 months ago

joezappie commented 1 year ago

Describe the bug

I have a VSCode devcontainer project that starts up a mongodb replica server. Mongo Compass and Studio 3T both can connect via the URI:

mongodb://localhost:27017

Mingo however gives me an ECONNREFUSED error:

image

Really looking forward to getting this to work so I can uninstall compass and Studio 3T off my computer. Loving the cleanness of the UI and how easy it makes it to migrate data between collections, databases and other connections! Finally a good modern mongo client.

Setup

I create the mongo DB via docker-compose in WSL:

  mongodb:
    hostname: mongodb
    image: mongo:5.0.9
    environment:
      MONGO_INITDB_DATABASE: zyphox
      MONGO_REPLICA_SET_NAME: rs0
    volumes:
      - mongo-data:/data/db
    restart: always
    entrypoint: ['/usr/bin/mongod', '--bind_ip_all', '--replSet', 'rs0']
    healthcheck:
      test: test $$(echo "rs.initiate({_id:'rs0', version:1, members:[ { _id:0, host :'localhost:27017' }]}).ok || rs.slaveOk().ok || rs.status().ok" | mongo --quiet) -eq 1
      interval: 10s
      start_period: 30s

Its started as a 1 node replica set to be able to use transactions in dev. There are no mongo users, admin DB is empty.

Does compass and 3T just add some required default parameter that I'm missing in Mingo? I tried adding replicaSet= parameter, and my default DB but cannot get it to connect. Works find connecting to my Atlas and Digital Ocean connections, just not my dev environment.

mongodb://localhost:27017/zyphox?replicaSet=rs0

Screenshots

Studio 3T image

Compass image

Mingo image

image

image

Desktop (please complete the following information):

tothradoslav commented 1 year ago

Hi, thanks for the detailed report. Could you try changing "localhost" in the URI to "127.0.0.1"?

I can't reproduce your error for now. I have also made some minor changes in the system that will be available in the next patch.

joezappie commented 1 year ago

I tried it with 127.0.0.1 yesterday but I got the same error. I'm working on setting up a sample vscode devcontainer that I can provide you with so maybe you can reproduce it.

tothradoslav commented 1 year ago

Could you please confirm this is still an issue with v1.13 ?

https://github.com/mingo-app/mingo/releases/tag/v1.13.0

joezappie commented 1 year ago

Sorry got distracted on a totally different project. I'll try it out tonight.

joezappie commented 1 year ago

After upgrading to 1.13 I'm still getting an MongoServerSelectionError: connect ECONNREFUSED ::1:27017 error despite Compass, Studio 3T and QueryAssist being able to connect with the same URI

joezappie commented 8 months ago

@tothradoslav I'm still having issues with this even in 1.13.4.

I created a sample VS Code DevContainer that spins up a local mongo replica set: https://github.com/jrj2211/mingo-local-db-issue

Enviroment: VScode Docker Desktop

Steps to use:

Try to connect to mingo with mongodb://localhost:27017 or mongodb://127.0.0.1:27017/ and see that you get an connection refused error message. Works with Mongo Compass and every other mongo client.

joezappie commented 8 months ago

I did also just test on my laptop to make sure it wasn't something weird with my desktop. Got the same connection refused in mingo but compass connected fine. I'd love to be able to uninstall all the other mongo clients I have and just use mingo because its so much better!

joezappie commented 8 months ago

Looking at the logs from docker, its accepting the connection and then disconnecting:

2023-11-09 16:08:16 {"t":{"$date":"2023-11-09T21:08:16.661+00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:46360","uuid":"26aeb801-058e-4dae-91b4-42f35178738b","connectionId":23,"connectionCount":6}}
2023-11-09 16:08:16 {"t":{"$date":"2023-11-09T21:08:16.662+00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn23","msg":"client metadata","attr":{"remote":"127.0.0.1:46360","client":"conn23","doc":{"application":{"name":"mingo"},"driver":{"name":"nodejs","version":"6.1.0"},"platform":"Node.js v18.16.1, LE","os":{"name":"win32","architecture":"x64","version":"10.0.19045","type":"Windows_NT"}}}}
2023-11-09 16:08:16 {"t":{"$date":"2023-11-09T21:08:16.671+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn23","msg":"Connection ended","attr":{"remote":"127.0.0.1:46360","uuid":"26aeb801-058e-4dae-91b4-42f35178738b","connectionId":23,"connectionCount":5}}
joezappie commented 8 months ago

@tothradoslav I spent a few hours on this today and got Mingo to connect, though I still don't have a reason why

Tried running it directly in docker (no vs devcontainer) and it connected fine:

docker run --rm -d -p 27017:27017 --name mongo mongo:6.0.5 --replSet=dbrs && timeout /t 5 && docker exec mongo mongosh --quiet --eval "rs.initiate({_id:'dbrs', version:1, members:[ { _id:0, host :'localhost:27017' }]});"

Thought it might be because I upgraded from mongo:5.0.9 to mongo:6.0.5 but trying the previous command with 5.0.9 works as well.

After more playing around I changed my devcontainer docker compose script to this: image

The healthcheck to initialize the replicaset doesn't work with these changes for some weird reason so I manually ran it: exec mongo mongosh --quiet --eval "rs.initiate({_id: 'dbrs', version:1, members:[{_id:0, host:'localhost:27017'}]});"

After that mingo was able to connect!

None of my changes feel meaningful, but something in there is what causes it not to work. The biggest change I think is switching from using entrypoint to command. My test repo posted earlier still has the old code where Mingo will not connect but compass will. With my old code I did verify by checking the logs the healthcheck works for initializing the replicasets. If it didnt, compass also would not connect.

I've since switched to bitnami/mongo image as it handles setting up replicasets much easier and that all works. For some reason, something with the replica sets causes this. I'll consider my issue resolved, but it still might be worth looking into as Studio 3T, query assist, compass could all connect while Mingo failed and someone else might run into this in the future and because I still don't have a concrete reason why these changes makes it work.

tothradoslav commented 8 months ago

hi @jrj2211 , thanks for the detailed report. I will try to go through the problem again using the older and newer version. If you could send me the old and new version of your devcontainer docker compose scripts, that would really be helpful.

joezappie commented 8 months ago

Taking a break clearly helps because I just realized there's a MASSIVE difference between those two compose scripts I posted earlier. The one that works with Mingo forwards port 27017, which should have been obvious to me and is the key to this.

I now know this is just a weird use case with how Docker handles networking between the containers and the host and less of an issue with Mingo. I'm good to close this issue as I feel I have closure lol.

It is still odd that somehow without the ports forwarded Compass can resolve it but Mingo can't. IF you're intrigued into still looking into it I have two repos for it:

Non-working - Mongo Compass, Studio 3T can connect but Mingo cant https://github.com/jrj2211/mingo-local-db-issue

Working - Mingo can connect as ports are forwarded https://github.com/jrj2211/mingo-local-db-issue-working

Only difference is adding the port forwarding. You can clone the repo by clicking the VS code button in the readme, and you can only have one running at a time.

Again, you probably have better things to do with your time so feel free to close this issue.

tothradoslav commented 8 months ago

I just started the docker at https://github.com/jrj2211/mingo-local-db-issue which didn't work for you and did not work for me either. However, I could not connect to this instance of mongodb with mingo, nor compass or studio 3t.

Then I added port mappings into the yml file, such as:

ports:
  - "27017:27017"

And then it started working fine in both mingo and compass. Could it be that you might have some mapping set up in compass or on you system? Because I could not connect to that server with compass until I configured the port mappings.

joezappie commented 8 months ago

Interesting that it didn't work in Compass for you. It's very possible I did something weird as I know at one point I was messing around with my host file for Mongo in Docker when I first started learning Docker.

Closing this issue as port forwarding is the answer. Thanks for your help and loving Mingo!