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

Connection error MongoDB Backend #32

Closed
gloffreda opened this issue Sep 3, 2019 · 7 comments
Closed

Connection error MongoDB Backend #32

gloffreda opened this issue Sep 3, 2019 · 7 comments

Comments

@gloffreda
Copy link

Hi there

I’m getting this error:

time=“2019-09-03T16:29:36Z” level=debug msg=“checking auth cache for user1”
time=“2019-09-03T16:29:36Z” level=debug msg=“checking user user1 with backend Mongo”
time=“2019-09-03T16:30:06Z” level=debug msg=“Mongo get user error: server selection error: server selection timeout\ncurrent topology: Type: ReplicaSetNoPrimary\nServers:\nAddr: localhost:27017, Type: Unknown, State: Connected, Avergage RTT: 0, Last error: dial tcp 127.0.0.1:27017: connect: connection refused\n”

Even though my parameters are all corrects, I’ve placed a debug on source code mongo.go and got:

time=“2019-09-03T16:29:35Z” level=debug msg=“Host: ip-XXX-XX-XX-XXX.sa-east-1.compute.internal”
time=“2019-09-03T16:29:35Z” level=debug msg=“Port: 27017”
time=“2019-09-03T16:29:35Z” level=debug msg=“Username: mqtt”
time=“2019-09-03T16:29:35Z” level=debug msg=“Password: mqtt123*”
time=“2019-09-03T16:29:35Z” level=debug msg=“DBName: iotBackend”
time=“2019-09-03T16:29:35Z” level=debug msg=“UsersCollection: mqttusers”
time=“2019-09-03T16:29:35Z” level=debug msg="AclsCollection: "

Maybe a relevant information, my Mongo Server isn’t on localhost, but in another VPS on same network and firewall rules are tuned.

if I try connecting to the same database using same credentials in command line I’m getting no problems:

ubuntu@ip-XXX-XX-XX-XXX:~/mosquitto-go-auth$ mongo mongodb://mqtt:mqtt123*@ip-XXX-XX-XX-XXX.sa-east-1.compute.internal:27017/iotBackend
MongoDB shell version v3.6.3
connecting to: mongodb://ip-XXX-XX-XX-XXX.sa-east-1.compute.internal:27017/iotBackend
MongoDB server version: 4.0.11
WARNING: shell and server versions do not match
MongoDB Enterprise rs0:PRIMARY>
The collection mqttusers exists but not acl collection.

Why does the driver states the error on localhost:27017 ?

@iegomez
Copy link
Owner

iegomez commented Sep 4, 2019

As I mentioned at the forum, I'm not sure why you're seeing that localhost mention. The host option is only used here to set the uri:

	addr := fmt.Sprintf("mongodb://%s:%s", m.Host, m.Port)

	to := 60 * time.Second
	opts := options.ClientOptions{
		ConnectTimeout: &to,
	}

	opts.ApplyURI(addr)

I just started a new job and I'm very short in time, so if you could try writing a dummy Go program that connects to your remote instance and see if there's any problem, that could be helpful.

@gloffreda
Copy link
Author

It is really weird.
I wrote a dummy go script and happens the same when I execute on server.

I don't have your email and not feeling comfortable to give the address and credentials in an open space, if you have a way to PM you it would be great if you could test it.

Thanks

@iegomez
Copy link
Owner

iegomez commented Sep 5, 2019

Yeah, I imagined it could have something to do with some driver option as it didn't make sense to me either. Tomorrow I'll have time to do some tests and let you know what I can find. Also, you can reach me at iegomez@uc.cl (it's in my profile) or PM me at the loraserver's forum (I just didn't want to mix things in the public topic), whichever you prefer.

@iegomez
Copy link
Owner

iegomez commented Sep 5, 2019

I fixed a couple of things (e.g., you had no port set) on the dummy Go program you sent me an tried it, getting:

server selection error: server selection timeout
current topology: Type: ReplicaSetNoPrimary
Servers:

It's a similar error, but it doesn't mention localhost. Then I realized you were actually connecting from the same server and using the internal ip-XXX-XX-XX-XXX.sa-east-1.compute.internal hostname in the original issue, which should resolve to localhost in that machine. So bottom line, don't worry about the localhost in the error.

On the other hand, I was able to connect remotely from a terminal with this command (credentials and host are changed here, of course):

mongo mongodb://your-user:your-password@ec2-external-ip.sa-east-1.compute.amazonaws.com:27017/your-db

So it's not a remote connection issue either. I've tried a few changes to the dummy program but still can't get it to connect, so if you can bare with me a little I'll try a couple more things and check for any ongoing issue with the Mongo driver tomorrow to see if I can resolve it.

@gloffreda
Copy link
Author

gloffreda commented Sep 5, 2019 via email

@gloffreda
Copy link
Author

It is solved, a very nice and explained answer in SO can be found here:

https://stackoverflow.com/questions/57778089/mongodb-go-driver-looking-on-localhost-when-should-not/57814946#57814946

So for your future users whose need to use Replica Set in a single server may fall on this pitfall.
Maybe, a little mention on doc can save time.
Thank you very much for your interested in helping the problem.

Cheers !

@iegomez
Copy link
Owner

iegomez commented Sep 6, 2019

Great! I'm glad that you could solve it and thanks for the research, I'll add a mention to the docs.

Cheers!

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

2 participants