MongoDB C# driver WriteConcernException Unauthorized on writes for readOnly=false user - mongodb

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

Related

How to choose MongoDB Connect to Cluster option?

I'm new to MongoDB and I'm having some problems with MongoDB recently.
I'm not sure about Connect with the MongoDB Shell, Connect your application, Connect using MongoDB Compass, what's the difference?
The current demand is. I want to directly allow connection from anywhere and create a user account password to log into this database, which option should I choose?
https://i.stack.imgur.com/iwYMf.png
In Connect your application tab, you get a link that you need to copy
and paste in your application you are building to connect it to the
database. Remember to replace your password and databse name.
In Connect using MongoDB Compass tab, you get a link that you need
to paste in your compass application(A desktop application that
makes your mongodb data handling so much easier). And again remember to replace
your credentials.
I'm not very fond of Mongodb shell. It's actually an extensible
command-line interface.

Mongodbd login with Mongoose to new database

I use mongoDB in Atlas cloud but now I want to movie to a local database. I installed MongoDB and created a admin user. Now I connect with mongoose to :
mongodb://login:pass#db.myserver.com:27017/admin
Works well. Now I create a new development database "develop".
I try to connect
mongodb://login:pass#db.myserver.com:27017/develop
This failed to login. OK, I understand that the admin database is the "/admin" one and that user is authenticated to this database but I thought that an admin can access all databases in a mongodb server?
In Atlas I just created the develop database and connected to it without any problem.
How I can well connect with mongoose to the new develop database? Do I need to create a second admin for this database?
Ok, I found out that I have to name the auth database in my connection string.
mongodb://login:pass#db.myserver.com:27017/develop?authSource=admin

Connecting to MongoDB database on mLab fails authentication

I have a Parse app, and I'm trying to migrate my app's database to a MongoDB instance on mLab.
I already have a fork of Parse Server set up on Heroku, and I'm using Heroku's mLab MongoDB add-on.
I have a database on mLab called heroku_1ksph3jj, and I should be able to connect to it with the following template:
mongodb://<dbuser>:<dbpassword>#ds047124.mlab.com:47124/heroku_1ksph3jj
However, each attempt returns:
Server returned error on SASL authentication step: Authentication failed.
I'm unsure what to replace <dbuser> and <dbpassword> with. I have a database user with the same name as my database: heroku_1ksph3jjz, so I used that. And I used the password for that user in place of <dbpassword>. Should I have used something else here?
You can get the dbuser and dbpass with:
heroku config | grep MONGODB_URI
Grab the dbuser (example_user) and dbpass (example_pass) from the response:
MONGOLAB_URI: mongodb://example_user:example_pass#mlab.com:12345/db
As of March 2016, mLab.com only supports mongo 3.0+ (as per a conversation with support), because of their new onerous authentication requirements.
This was not on the website, but I hope it helps someone here!
There's a message to create a user for the specific database:
A database user is required to connect to this database. To create one now, visit the 'Users' tab and click the 'Add database user' button
I'm so sorry that this may seem obvious but, you have to remove this characters <> for the migration to work
In your example would look like this:
mongodb://dbuser:dbpassword#ds047124.mlab.com:47124/heroku_1ksph3jj
Check your mongo client version. If it is in older major version (probably 2.x), update it to 3.x
For future visitors - don't use special characters in password .Even if you change the special character to ascii or unicode it wont work for mLab using mongoose.
Also don't use mLab credential , use db user credentials . I created a new user.
Eg. For me a password containing # character was replaced with ascii value %40 in URI , which worked when using native mongodb driver.
But on using mongoose, i was always getting Authentication Failed .
I removed special characters and db was authenticated via mongoose.
It looks like it was the password that was incorrect, which I'm assuming was set up by Heroku's mLab add-on. There was no obvious way to reset this in the mLab UI, so in the end I created another database user (with a new username and password) and was able to connect with that just fine.
Just go to your Heroku dashboard and check your settings.
Under the name field there's a big button "Reveal Config Vars". Click it and you'll see a MONGODB_URI var with a uri to your db. It'll look something like this:
mongodb://heroku_user:PASSWORDyourLOOKINGfor#ds2238985.mlab.com:63295/heroku_user
Your password is right after the semicolon after the heroku user name.
I know I am too late, just for information.
For getting the info of mLab account that got auto created when mLab addon got added to the application in your heroku account, try the below command.
heroku config:get MONGODB_URI
Ref:
http://algebra.sci.csueastbay.edu/~grewe/CS6320/Mat/NodeJS/Heroku/Heroku_MLabMongoDB.html

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

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.

Connecting to Remote MongoDB Server with Python

I have a script that I wrote to query mongodb in python I am using PyMongo. I am trying to use this script to connect to a remote MongoDB server and then run the query within the script and then I want to be able to dump the data I get back from the mongodb into a file.
What are the parameters I need to have at the top of the script to connect to this database, use my username and password, switch to the correct database and then run the query?
Couple of options.
First you could provide a MongoDB URI which can be provided to the MongoClient as an argument. Then you can switch as needed using the standard methods for getting a database once connected.
Alternately, you can connect as normal, use the getting a database once connected method to get the desired database and then use the authenticate function to authenticate against the database.