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

Hello everyone,

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

here is my mongodb.cfg file:

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:

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

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

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:


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

yep… forget it… no auth support :slight_smile:

Opening an Issue now

Okay. :slight_smile:

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

Username and password are optional.

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


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

can you test this and report back?

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:)

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

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

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

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

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

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=}

1 Like

okay no issue.

Can you suggest any other DB better than MongoDB ?

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.

Okay thanks for suggestions. :smiley:

1 Like