RMongo: Connect to mongoDB using port forwarding

3k Views Asked by At

I connect to a mongodb server using port forwarding like this:

ssh -i key.pem -Nf -L 27018:xx.xxx.xxx.xxx:27017 [email protected]
mongo -u user -p pass --authenticationDatabase "db" --port 27018

Then I run R to connect and query the database:

library(RMongo)
mg1 <- mongoDbConnect(dbName = 'db', host = 'xx.xxx.xxx.xxx', port = 27018)
auth <- dbAuthenticate(rmongo.object = mg1, username = 'user', password = 'pass')

This gives me an error while authentication:

Error in .jcall(rmongo.object@javaMongo, "Z", "dbAuthenticate", username,  : 
  com.mongodb.MongoException$Network: IOException authenticating the connection

Without port forwarding, my credentials work:

library(RMongo)
mg1 <- mongoDbConnect(dbName = 'db', host = 'xx.xxx.xxx.xxx', port = 27017)
auth <- dbAuthenticate(rmongo.object = mg1, username = 'user', password = 'pass')

How do I set my port in mongoDbconnect?

Thanks!

1

There are 1 best solutions below

0
Komal Rathi On

Ok, this works. No need to put the host because now we are mapped to the localhost:

library(RMongo)
mg1 <- mongoDbConnect(dbName = 'db', host = 'localhost', port = '27018')
auth <- dbAuthenticate(rmongo.object = mg1, username = 'user', password = 'pass')