MongoError: not authorized on [db] to execute command

This is my first attempt to use mLab on Heroku. I have the url-shortener project working correctly on Cloud9 where the application connects and is able to read and write to my mLab database. However, when deployed on Heroku, the application connects successfully, but any query executed produces the following error:

: /app/node_modules/mongodb/lib/utils.js:98
: MongoError: not authorized on fcc-dgw to execute command { count: “urls”, query: {} }
: process.nextTick(function() { throw err; });

Any help would be appreciated.

It looks like an authorization error. Your authorization and user settings is set in your mLab account. Here’s some steps I would go through - 1. Make sure you’re on the latest major version of MongoDB - I think c9 is on an earlier version by default 2. Make sure you’re using the process.env variables in your server for your port and db and set the config variable for the db in Heroku. 3. In your mLab account, there is a MongoDB URI that you should copy and paste exactly as the db config variable 4. Finally, make sure you’re an admin user - Here’s the steps from mLab’s start guide:

Log in to the mLab management portal
From your account’s Home page, navigate to the deployment
If applicable, then navigate to the database for which you will be adding a user
Click the “Users” tab
Click the “Add database user” button to create a new user
You can then use this database user to connect to your database.

This error was caused by a userid and password with the wrong separator

mongodb://myuserid/ [wrong] mongodb:// [right]

While the node application was able to successfully connect to MongoDB, the incorrectly formatted URI caused the driver to skip authentication prior to issuing database commands.

Thanks and a hat tip to the people at mLab support.

I solved this issue by getting into my mLab account, displaying the list of users and making sure that my user was NOT set up as read only.

1 Like

tnx! excatlly solution i searched! :slight_smile: