[MongoDB] Persistence Service cannot connect to MongoDB Atlas (DBaaS)

database
mongodb
persistence
Tags: #<Tag:0x00007f01430ea510> #<Tag:0x00007f01430ea358> #<Tag:0x00007f01430ea1f0>

(Nikhil Komalan) #1

Hello everyone,

I am currently using mongodb database to store my openhab items values.

here is my mongodb.cfg file:
url=mongodb://localhost:27017
database=openhab
collection=serial

I am able to access the Local MongoDB database from mongo client running on my PC.

Now in MongoDB Atlas, i have created database named openhab, with collection serial. But as I try to connect to my MongoDB Atlas port, by replacing localhost with the port details as given below:
url=mongodb://gettingstarted-shard-00-01-cawjp.mongodb.net:27017

It gives me error of " The activate method has thrown an exception
java.lang.RuntimeException: Cannot connect to database ".

Can anyone help me to understand how to store data on MongoDB Atlas using mongodb persistence.

Thank You


(Angelos) #2

OH2 version?
MongoDB Addon version ? (latest is here: https://ci.openhab.org/job/openHAB1-Addons/lastSuccessfulBuild/artifact/bundles/persistence/org.openhab.persistence.mongodb/target/)

Maybe? (just maybe) the binding has a problem and it does not accept FQDN at the url configuration

Try to apply a workaround to see if this is the case (if possible, use the IP Address or create an alias)

A hyphen is a valid character in a hostname, so this shouldn’t be the problem


(Angelos) #3

I just realized something else… the binding configuration does not provide config parameters for authentication and most likely (99,999% chances) the cloud based MongoDB you are using @ mongodb.com/cloud/atlas requires auth

so… this won’t work

although undocumented (untested and most likely not going to succeed), try to set in your mongodb.cfg:

user=<username>
pwd=<password>

I will check the code @ https://github.com/openhab/openhab1-addons/tree/master/bundles/persistence/org.openhab.persistence.mongodb


(Angelos) #4

yep… forget it… no auth support :slight_smile:

Opening an Issue now


(Nikhil Komalan) #5

Okay. :slight_smile:

Also in MongoDB Atlas the username and password are not mandatory to connect.

Username and password are optional.
https://docs.mongodb.com/manual/reference/connection-string/


(Angelos) #6

I believe that this is true for a “standard” (not Atlas cloud) MongoDB

Are they optional for Atlas? I doubt it (otherwise everyone would be able to connect to your database… if they knew the FQDN)

From: https://webassets.mongodb.com/_com_assets/collateral/Atlas_Security_Controls.pdf

https://docs.mongodb.com/manual/core/security-scram/

Now you gave me another idea… pass auth in the URI

can you test this and report back?


(Angelos) #7

I opened an issue to add auth support on the MongoDB persistence addon here: https://github.com/openhab/openhab1-addons/issues/5712

I believe that this will be treated with super low priority, so don’t wait for a fast fix :slight_smile:

Even if this happens, I don’t know if the developers will enable the advanced auth mechanisms that Atlas requires…

If I was you, I would consider: store locally on a MongoDB and find a way to replicate the data that you want to the Cloud using a synchronization method that MongoDB provides for (I have no idea what replication mechanisms MongoDB supports with Atlas DBaaS… you will need to read up on that :stuck_out_tongue:)


(Nikhil Komalan) #8

Okay i have tried passing authorization in URI, but still facing issue of cannot connect to DB.
url = mongodb://:@gettingstarted-shard-00-01-cawjp.mongodb.net:27017


(Angelos) #9

try with basic auth. Check an example here: https://www.openhab.org/addons/bindings/http1/#http-basic-authentication

Ps: When posting configs, use code fences :slight_smile: simple one line code: single grave accent (`) character : `text` , more complex code: three grave accent characters in separate lines : ``` new line <code> new line```

This is a single code example

This is a multi line code example

Read more here: How to use code fences


(Nikhil Komalan) #10

Here is my mongodb url as per docs stated to access MongoDB Atlas

url = mongodb://nkomalan83:xyz@gettingstarted-shard-00-01-cawjp.mongodb.net:27017


(Angelos) #11

I don’t think that this will work, but try it for fun:

url = mongodb://gettingstarted-shard-00-01-cawjp.mongodb.net:27017{Authorization=Basic bmtvbWFsYW44Mzp4eXo=}

The nkomalan83:xyz has been encoded to bmtvbWFsYW44Mzp4eXo= using https://www.base64encode.org/

use the correct credentials, encode them and replace them in the url above


(Nikhil Komalan) #12

Same issue !
java.lang.RuntimeException: Cannot connect to database


(Angelos) #13

Of course :slight_smile:

Give up on OH2<->Atlas for now… it won’t work

setup a local Mongo and link that to Atlas :wink:

edit: one last try with Authorization: Basic instead of Authorization=Basic

url = mongodb://gettingstarted-shard-00-01-cawjp.mongodb.net:27017{Authorization: Basic bmtvbWFsYW44Mzp4eXo=}


(Nikhil Komalan) #14

okay no issue.

Can you suggest any other DB better than MongoDB ?


(Angelos) #15

it really depends on what you want to achieve (give us a short description of your desired scope)

If only restoreOnStartup = mapdb (nice and simple with zero maintenance)
if lots of historical data should be retained and more advanced graphs should be generated = InfluxDB (see: InfluxDB+Grafana persistence and graphing)

Of course, you can combine both of them (mapdb + InfluxDB Persistence Services in OH2) to achieve both goals at the same time.


(Nikhil Komalan) #16

Okay thanks for suggestions. :smiley: