How to resolve pymongo authentication failure error? - mongodb

I am trying to fetch data from my mongodb but it fails with:
pymongo.errors.OperationFailure: Authentication failed.
It is weird because when I am trying to authenticate with same credentials through robo3T (MongoDB GUI) it is able to connect easily.
Through code it is like:
client = MongoClient(host=host,
port=int(port),
username=username_admin_db,
password=password_admin_db,
authSource=authSource_admin_db,
maxPoolSize=15,
MaxIdleTimeMS=120000
)
db_handle = client[database_name]
for i in db_handle[collection_name].find({'account_id': "1234"}):
print(i)
In mongod.conf:
security:
authorization: enabled
It is also weird because through the same code I am able to authenticate to another db on a different server with same credentials and same db name.
Versions:
Python 3.6.10
MongoDB 3.4
pymongo 3.5.1
Django 1.11.7

Related

Mongodb not authenticating on localhost or connecting authenticated mongodb compas

System
Hi I am running mongodb on ubuntu 20.14.
Im running mongodb with systemctl
What I've done
I've tried to make it more secure by adding an admin user and enabled authentication.
Ive restarted the service multiple times.
config file:
security:
authorization: enabled
How I created user:
use admin
db.createUser({
user: "username",
pwd: "123456",
roles:["root"]
})
Problem
I am still able to connect through mongodb compass without any auth??? Im able to do everything even tho I enabled the authentication?
I am not able to login authenticated using these urls:
mongodb://username:password#localhost:27017/
mongodb://username:password#localhost:27017?authSource=admin
Im sure the config file is loading since authentication works in console and I can see the right config load in the mongod.log
It would be this one:
mongodb://username:password#localhost:27017?authSource=admin
See also: Authentication failure while trying to save to mongodb
Yes, even without authentication you can connect to Mongo database in any case. However, apart from harmless commands like db.help(), db.version(), db.getMongo(), etc. you cannot execute anything.
You can skip parameter enableLocalhostAuthBypass. The localhost exception applies only when there are no users created in the MongoDB instance.
Solution
I thought the issue was with mongodb compass.
So what I did was deleting the application and when I did that I saw that I had mongodb installed on my pc too.
I was never connecting to the mongodb that I have created on my ubuntu server but on my own pc.

parse migration : Server returned error on SASL authentication step: Authentication failed

im trying to migrate the parse mongodb to self hosted mongodb server (version 3.0.12 on debian 7). the firewall is open and i can connect to the mongodb server from my laptop but when i try to migrate the database using parse Migrate App then i get below error
Server returned error on SASL authentication step: Authentication failed.
here is what my connection strings look like
mongodb://rootuser:S3ecretw0rd#<mongodb-server-ip>:45045/dbtest
I've got the solution: I used a plus in my password and because of that it didn't worked.

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.

Cannot access mongoDB after "meteor deploy myapp.com"

I deployed my app using meteor deploy myapp.com, and directed my DNS to myapp.meteor.com.
The app is now available at myapp.com, and I have no problem running it. It's the correct version that was deployed to "myapp.com" and not the older "myapp.meteor.com" version.
But I cannot access mongodb for this deployed version.
When I run meteor mongo myapp.com, I get this at the terminal:
MongoDB shell version: 2.6.7
connecting to: sg-mother1-6242.servers.mongodirector.com:27017/myapp_com
2016-03-10T16:46:18.659-0800 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
I am also getting the same error when I run meteor mongo myapp.meteor.com:
MongoDB shell version: 2.6.7
connecting to: someserver.servers.mongodirector.com:27017/myapp_meteor_com
2016-03-10T16:45:54.367-0800 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
When I do meteor mongo --url myapp.com, I do get a URL back of the form:
mongodb://<user>:<password>#<some-servername>.servers.mongodirector.com:27017/myapp_com
But I cannot connect to this URL using Robomongo. I get an error:
Cannot connect to MongoDB (<some-servername>.servers.mongodirector.com:27017),
error: Unable to connect to MongoDB
What am I doing wrong? How do I connect to the mongoDB for the app I deployed on my custom domain? Preferably using some GUI tool such as Robomongo?
What I understand, is that the local install of Meteor uses Mongodb 2.6 and newly deployed Meteor sites use Mongodb 3.0
When you call meteor mongo myapp.meteor.com you are using meteor's locally installed version of mongo (version 2.6) but you are trying to access the deployed mongodb (version 3.0). This results in the authentication error you are getting.
This link describes a workaround that worked for me. I had to tweak it a little, but this is what I did:
Install or update your local version of Mongo (not through Meteor).
Run the command meteor mongo --url myapp.meteor.com to get the MONGO_URL. As you already mentioned, you'll get something like
mongodb://<user>:<password>#<some-servername>.servers.mongodirector.com:27017/myapp_com
Connect to the MONGO_URL using your updated non-meteor version of mongo by running
mongo mongodb://<user>:<password>#<some-servername>.servers.mongodirector.com:27017/myapp_com
You should now be in the mongoshell, connected to your deployed mongodb. You should see something like this RS-mother1-0:PRIMARY> in your mongo shell. You still need to switch to your app's DB though. So call use myapp_com from the shell.
You should now be able to view collections and run mongo commands on your deployed meteor mongodb.
I'm not sure why you can't connect to Robomongo using the username and password meteor generates for you in the MONGO_URL. I suspect it's because it might expire. If you still want to connect using Robomongo, I'd recommend creating a user on the database now that you are logged in. And then later on, using that user to log into Robomongo.
Creating the user in the mongo shell:
db.createUser({ "user" : "my_user", "pwd": "my_password", "roles" : ["readWrite"]})
New MONGO_URL:
mongodb://my_user:my_password#<some-servername>.servers.mongodirector.com:27017/myapp_com

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.