Mongo::Error::OperationFailure (not authorized on [db] to execute command....(13)) - mongodb

On my production server, I keep getting not authorized error randomly. The following is my setup:
MongoDB 3.2.1
Mongo (ruby driver) 2.2.3
Mongoid 5.1.1
bson 4.0.2
I don't have username and password configured in mongoid.yml. I only have uri configured to connect to remote database server.
I search-fu was weak, and couldn't find anything that could explain/help what's going on.
Any help would be much, much appreciated.
Thank you,

It seems like upgrading to mongo 3.x fixes the problem, as this jira ticket shows.
https://jira.mongodb.org/browse/RUBY-1100

Related

Dart - error when trying to authenticate with mongodb

After running:
import 'package:mongo_dart/mongo_dart.dart';
//Create account with given credentials
createAccount(Map<String, String> credentials) async {
Db db = new Db('mongodb://127.0.0.1/exampledb');
await db.open();
await db.authenticate("user", "password");
//TODO: Post credentials into DB
}
I get an error on the Dart server saying:
Unhandled exception:
Uncaught Error: {ok: 0.0, errmsg: auth failed, code: 18}
And a different error comes up up on the mongod server:
2015-09-27T20:04:25.921+0100 I ACCESS [conn1] Failed to authenticate user#exampledb with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentia
ls missing in the user document
The only time when the authentication has succeeded and has allowed me access to the database is when I am using the command prompt.
Example:
$ use exampledb
$ db.auth("user", "password")
How can I make my Dart script gain access to my local mongodb, using the authenticate method?
Fresh update:
Since version 0.2.5 mongo_dart supports SCRAM-SHA-1 authentification mechanism. It used by default in connections to MongoDb 3
Update:
That seems to be rather widespread problem with some drivers, programs e.t.c not supporting yet new authentication mechanism (SCRAM-SHA-1) of mongodb 3.0
By default, mongodb 3.0, do not create credentials in old format (MONGODB-CR) while creating new users.
There is round-about solution, that force mongodb 3.0 and upper version use MONGODB-CR mode while creating users.
Look for example solution at https://jira.mongodb.org/browse/SERVER-17459
As stated in that thread
both new drivers and legacy software work with that solution
Obviously best solution for mongo_dart would be to add implementation of new authentication mode to the driver. Can not give any date, but I think it should be first feature that I can take in consideration as soon as I have some time for mongo_dart.
And obviously it would be great if somebody beat me at that with pull request :)
Original answer
I've reproduced that error in my environment too.
Error appears to be related to changed default authentication mode in version 3.0 of MongoDb. I'll update this answer, when the problem will be resolved
I tried that in my mongodb 4.0.10 and mongo_dart 0.3.6 and the authentication went all right and correct just update your components if you didn't yet and everything will be fine.
PS: I know I am late but hope someone get used from my "Note".

MongoDB unauthorized users can create databases

We have an old MongoDb 2.2.6 deployment and lately we were requested to introduce anonymous user blocking to the Db. I followed the MongoDb tutorials and created an authentication user first and started the mongod with
--auth
parameter.
Then after login, I could not execute any db related commands except for the
use << db >>
. Everything was perfect. But then I tried issuing
use <<< some_junk_db_name >>
without authentication and then issued
db.some_collection.find()
command. Mongodb correctly raised an exception but later when I checked, that DB was there.
Is this a bug with MongoDb 2.2.6 or am I missing something here ?
This is a reported Bug with the number 2080 and the ticket is still not fixed. The last comment, May 17 2013, from a MongoDB Inc. member says that the Bug is still existing in the 2.4 version of MongoDB.
In Version 3.4 the Bug was no longer reproducable.

Datanucleus Mongodb with authentication

I am using datanucleus for JPA on Heroku with mongodb (mongolabs). Even when I set these properties:
datanucleus.ConnectionPassword=YYYYY
datanucleus.ConnectionUserName=XXXXX
I get PersistenceException: Authentication of the connection failed for datastore heroku_app2765673 with user XXXXXXX
Does datanucleus support connecting to mongodb with a password? It seems to know my username, so I assume so. What am I doing wrong?
Removing and readding my mongolabs account seems to have fixed this. Don't know what the actual problem was, but it clearly wasn't datanucleus.

Mongo vs Mongoid - why can 1 connect and the other not?

I have a rails-app which uses both mongoid and mongo. I use mongoid for my own models, and I use mongo because I have ruote with a ruote-mon storage.
In production however; I get
Mongo::ConnectionFailure: Failed to connect to a master node at localhost:27017
when I try to connect to the ruote storage. Even when I just do Mongo::MongoClient.new
Steps I have taken so far to try to resolve this:
I have made my mongodb an explicit master by setting master = true in /etc/mongod.conf
There are no $ENV variables set that could intervene with Mongo::MongoClient.new (double checked)
I have tried to connect using Mongo::MongoClient.new(:slave_ok => true) - same error
I have restarted my mongo database several times (w/o success).
I have checked my firewall settings and I can connect to localhost:27017 with telnet (as said, the mongoid documents can be fetched and stored w/o issue)
I am out of my wits... Any suggestions?
The reason this happened is because we were sending queries with meta operators ($query, $orderby, etc...) for the ismaster command during a connect. This command's output is used to determine whether you are connected to a primary or not and would fail because very old versions of mongodb don't support the use of meta operators.
This fix will be in version 1.8.2 of the gem but I strongly encourage anyone who is still running pre-1.8 versions of mongodb to upgrade. 2.0 is the current legacy release as of the time of this post and even 1.8 is no longer widely supported.
As jmettraux mentioned you can find more details about this on the MongoDB project Jira under Ruby-525
please look at: https://jira.mongodb.org/browse/RUBY-525
Should be fixed by the 1.8.2 mongo gem.

Heroku could not authenticate mongolab

It was corrected two days ago. And yesterday I committed some changes to heroku and the application crashed, I found from heroku logs that:
/app/.bundle/gems/ruby/1.9.1/gems/mongo-1.4.0/lib/mongo/db.rb:137:in `issue_authentication': Failed to authenticate user 'larryzhao' on db 'uthenera' (Mongo::AuthenticationError)
I even explicitly input the host/port/username/password/db name into mongoid.yml, still it could not get pass authentication.
I tried both database in Rackspace and in Amazon, but neither could authenticate.
Have you met with that before? Is there anything else I could check on Heroku besides heroku logs ?
Thanks.
Today I encountered the same on Heroku:
Mongo::AuthenticationError: Failed to authenticate user 'heroku_app111' on db 'heroku_app111'.
from /home/ad/.rvm/gems/ruby-2.1.3#app/gems/mongo-1.11.1/lib/mongo/functional/authentication.rb:205:in `issue_authentication'
It's not because of this RUBY-890 bug, but because MongoLab upgraded to MongoDB 3.0 which uses SCRAM-SHA-1 challenge-response user authentication mechanism. So I needed latest ruby mongo driver version which is counting with this. In my case latest 1.x branch.
The minimum driver versions that support SCRAM-SHA-1 are:
Driver Language Version
C 1.1.0
C++ 1.0.0
C# 1.10
Java 2.13
Node.js 1.4.29
Perl 0.708.0.0
PHP 1.6
Python 2.8
Motor 0.4
Ruby 1.12
Scala 2.8.0
Of course you have to be sure your credentils are ok :)
Make sure you're using the right username to connect to your database. The username you use to log into mongolab.com is different from the one attached to the 'uthenera' database. Have a look at the Users tab for that database at mongolab.com to see what database user(s) you've configured.
Hope that helps. Feel free to email support#mongolab.com if you need further assistance.
-Robert
You can get your database username and password using:
heroku config | grep MONGODB_URI
The response is in this format:
MONGOLAB_URI: mongodb://username:password#mlab.com:12345/db