lancegoh lancegoh - 5 months ago 39
Ruby Question

Dokku, RoR and Mongo DB: Unauthorized not authorized on db to execute command

I have been struggling with this issue for days, i am able to authenticate with the mongo shell.

But when i access it my application from the browser, i got the above mentioned error.

Ruby on rails logs:


2016-07-05T04:29:34.415943099Z app[web.1]: MONGODB | xx.xx.xx.xx:4121
| [db].count | STARTED | {"count"=>"listings", "query"=>{}}

2016-07-05T04:29:34.418337913Z app[web.1]: MONGODB | xx.xx.xx.xx:4121
| [db].count | FAILED | not authorized on [db] to execute command {
count: "listings", query: {} } (13) | 0.0021065790000000004s


Background
Hosting on Digital Ocean, with the One-Click Deployment of Dokku.

Dokku Version : 0.6.4
MongoDB : 3.2.6
Ruby: 2.2.4
Rails 4.2.6

I have added user (with DbOwner) to MongoDB and the same to mongoid.yml.

Here is
mongoid.yml


production:
clients:
default:
database: sample
hosts:
- ip:4121
user: "user"
password: "password"
options:
read:
mode: :primary
max_pool_size: 5

Answer

Ok. I found the solution for this and i thought i will close the loop on this and save someone a couple of hours/days trying to figure this out.

Basically, the one-click from digital ocean DOES work!

But you need the following in your mongoid.yml, add the URI in and remove the database, user and password.

To generate the URI, do the following in your ssh terminal console:

dokku mongo:info <<db name>>

Update the uri value with what was displayed on screen.

production:
  clients:
    default:
      uri: <<URI>>
      options:
        read:
          mode: :primary
        max_pool_size: 1