Mongodb "auth fails" with mongodb php driver and new php library - mongodb

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6

I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.

So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

Related

How to copy database from MongoDB 1.6 instance

I have a MongoDB instance with two databases, let's call them "realdb" and "copydb".
All I want to do is to periodically copy realdb to copydb. The copydb database is our "testing" instance of the actual database "realdb", and we want to periodically update it.
Normally the answer to this question would be "copydb" or "export/import". However, there are some challenges:
The target Mongo instance is running version 1.6 and is not likely to be updated anytime soon.
The target Mongo instance is on a remote server to which I don't have direct access.
This seems like basic enough a function that even 1.6 should have the ability to do it. But when I try anything I get "No such cmd" errors as if the newer Mongo can't communicate with the ancient Mongo.
Any thoughts on how this could be done?
The error "no such command" might mean that:
The command was mistyped, or does not exist
There is a mismatch of the client version (e.g. mongo shell, mongodump, mongoexport, or a mongodb driver) and the server version.
In this case it was a mismatch of the mongo shell version (3.4) vs server version (1.6).

Auth Failed trying to connect from a Fantom script to a MongoDB hosted in MongoLab. What's wrong?

I'm trying to connect to a remote MongoDB v3.0.8 hosted in MongoLab using a Fantom v1.0.67 script. The driver I'm using is afMongo v1.0.4. The connection line is:
mongoClient := MongoClient(
ActorPool(),
`mongodb://mydbuser:mydbpassword#ds0#####.mongolab.com:#####/mymongodb`
)
Please note that the placeholders mydbuser, mydbpassword and ##### are replaced with the correct values. The connection is successful when I run the following command from a shell:
mongo ds0#####.mongolab.com:#####/mymongodb -u mydbuser -p mydbpassword
but from the Fantom script, I get this error:
afMongo::MongoCmdErr: Command 'authenticate' failed. MongoDB says: auth failed
afMongo::Operation.runCommand (Operation.fan:36)
afMongo::Operation.runCommand (Operation.fan)
afMongo::Connection$.authenticate (Connection.fan:34)
afMongo::TcpConnection.authenticate (Connection.fan:51)
afMongo::ConnectionManagerPooled.checkOut (ConnectionManagerPooled.fan:458)
afMongo::ConnectionManagerPooled.leaseConnection (ConnectionManagerPooled.fan:320)
afMongo::Cmd.run (Cmd.fan:71)
afMongo::Database.runCmd (Database.fan:36)
afMongo::MongoClient.runAdminCmd (MongoClient.fan:107)
afMongo::MongoClient.buildInfo (MongoClient.fan:64)
afMongo::MongoClient.startup (MongoClient.fan:119)
afMongo::MongoClient.makeFromUri$ (MongoClient.fan:41)
afMongo::MongoClient.makeFromUri$ (MongoClient.fan)
afMongo::MongoClient.makeFromUri (MongoClient.fan:38)
I have also tried Robomongo v0.8.4 with a similar result: A pop-up dialog saying...
Successfully connected to ds0#####.mongolab.com:#####
Authorization failed
I ran the Fantom script as well as Robomongo connecting and authenticating successfully against MongoDB v2.6.3 and v3.0.8 databases hosted locally.
My Java version is Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
I don't know if Robomongo or Fantom/afMongo depend on a Java or System library that is out of date.
What is wrong or what I should be checking, please?
The auth failures in both Robomongo v0.8.4 and afMongo 1.0.4 are due to a new authentication mechanism in MongoDB v3 called SCRAM-SHA-1.
afMongo has been updated with an implementation of SCRAM-SHA-1 over SASL. It auto detects Mongo v3 databases and switches the auth protocol, falling back to the older MONGODB-CR when needed - see this commit.
These changes haven't been released yet because it depends on a new method Buf.pbk(...) that is only available in Fantom 1.0.68 - which itself is unreleased. An updated afMongo v1.0.6 will be released as soon as Fantom 1.0.68 is.
You could compile Fantom 1.0.68 and afMongo 1.0.6 from source, but to be honest, the easiest fix (in the meantime) is to just use a v2.6 MongoDB database.
Note that, if needed, the MongoChef GUI client can authenticate against MongoDB v3 databases.

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 C# driver WriteConcernException Unauthorized on writes for readOnly=false user

I've been running my application with a readOnly=false admin user for 6 months in development on MongoDB 2.2.2. We're getting closer to launch and I wanted to switch over the user that my application runs under to a user in the application's database only with readOnly=false.
I ran the following script from the robomongo as the admin user.
use MyDB
db.addUser("api", "MyPassword", {"readOnly": false});
I also updated the MongoDB connection string to the following.
<add key="DataServer" value="mongodb://api:MyPassword#localhost:27017/MyDB" />
It created my user correctly and I validated that it is in MyDB and is readOnly=false. However, when I use the MongoDB c# driver 1.8.1 it fails on any writes with a WriteConcernException unauthorized. I am able to read records just fine from the database. Looking at the MongoDB logs it is correctly logging into my DB from my c# application but failing on the write calls.
Using the robomongo shell I was able to successfully auth with the api user and write records into one of the collections in the application's DB. So it's leading me to believe that it's something to do with the c# driver. I've also debugged my application code and validated that the MongoClient's GetServer method is returning a MongoServer that has the correct credentials.
Here's the robomongo shell script I ran to validate the user is able to write to the DB.
use MyDB
db.auth("api","MyPassword");
db.Person.insert({"first_name":"Test","last_name":"User"});
db.Person.find({"first_name":"Test"});
Any help as to why the MongoDB c# 1.8.1 driver connecting to MongoDB 2.2.2 and not being able to write records in a user DB with a readOnly=false user would be greatly appreciated.
I can't really see much wrong with your code. I'd suggest trying the following:
Make sure you're reading the DataServer app setting correctly in your C# code (including the DB name)
Create the user in the admin database and see if that helps:
use admin
db.addUser("api", "MyPassword"); // readonly defaults to false

How is MongoDb installed by Meteor?

I'm new to both Meteor.js and MongoDB and after installing Meteor in the official way described I wonder how to connect to my MongoDB.
MongoDB was installed by Meteor during the installation and everything works fine but now I would like to have a look into it with another tool (like RazorSQL) to see what's in there.
But the standard connection parameters (localhost:27017) doesn't work, what can I do? Login? Password?
Update: February 2014 - Meteor 0.7.1 - The meteor port has been shifted to 3001 instead of 3002. So instead of adding two to the port meteor runs on, you add 1 instead.
MongoDB's database is installed in the meteor package containing your files in a hidden folder called .meteor. To access it from a remote tool simply add 2 to whatever your web server port is while meteor is running. It will be stored in the meteor database
e.g
http://localhost:3000 would have its mongodb server running at mongodb://localhost:3002/meteor there is no username/password on this instance if you ran it with meteor or meteor run
To get the Meteor Mongo url and port, first run your Meteor app using meteor run then run meteor mongo in a different terminal tab. You should see an output like this
[meteor-app] meteor mongo
MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
this means that your Meteor Mongo is running at 127.0.0.1:3001.
If you are running your Meteor app with meteor run then you neither need username/password nor authentication configuration just make sure that you set your default database name as meteor