Unable to connect to MongoLab "auth fails" error - mongodb

I am new to mongo. Recently created an account in mongoLab.
I am trying to connect to the database using the below statement.
mongo ds039487.mongolab.com:39487/webbies -u <dbuser> -p <dbpassword>
Getting the below error
MongoDB shell version: 2.4.8
connecting to: ds039487.mongolab.com:39487/webbies
Wed Dec 11 22:30:16.686 Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails" } at src/mongo/shell/db.js:228
exception: login failed
Note:
dbuser - is an User created in mongoLab
What is missing in this? Kindly advice.

Old thread, but just in case it helps anyone else: my problem was using shell version 2.4.9 to connect to mongo db version 3.0.7 (http://docs.mongolab.com/connecting/#mongo_shell).
Updating my shell sorted it (https://docs.mongodb.org/master/tutorial/install-mongodb-on-ubuntu/).

Thanks to pneumee.
The issue was because I was using my mongolab.com username and password, instead of the database username and password.
It is working now.

OK - I figured it out and would also like to add onto pneumee's answer.
Go to mongolab.com
Login.
Navigate to the "Users" tab.
If you have forgotten your Username and Password for the database, I suggest you "Add a new user" and supply it with the username and password.

default users in db
login: admin
password: admin
check users in
https://mongolab.com/databases/MYDBNAME#users
my problem was being putting login and password mongolab

Related

I'm trying to connect to mongodb Atlas but I keep getting error and can't proceed

My error goes :
mongo "mongodb+srv://cluster0-ts5b5.mongodb.net/test" --username admin-David
MongoDB shell version v4.0.6
Enter password: Cannot get console mode 6
I meant to write password there but I can't proceed to password so I can't connect to Atlas at all. Please sort this problem!!
I ran into this same problem today, I was able to log in by including the password in the same statement where the url and username are.
So in your case try doing this:
mongo "mongodb+srv://cluster0-ts5b5.mongodb.net/test" --username admin-David --password <yourPasswordHere>
That worked for me, hopefully it helps!
MongoDB docs reference: https://docs.mongodb.com/manual/reference/program/mongo/#cmdoption-mongo-username

Can't Connect to Mlab

I have a very simple Mlab account and I'm trying to connect via command line with this command:
mongo server_name.mlab.com:port/inventory -u <dbuser> -p <dbpassword>
but I keep running into this problem:
2016-05-26T15:41:18.195+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
exception: login failed
How do I get rid of this and how can I connect to mlab easily?
I think that you're using your mLab credentials. You must set a DB User at the User Tab like this:
If (like me) you were positive that you were using the correct credentials for the database and for some reason you were still getting the Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } thing, here's what I did.
First check your version of mongo.
$ mongo --version
If it is less than 3.x.x congratulations, you've found the problem.
For my linux users out there, go here to update your mongo. After that your connection should succeed!
I had the same problem, if your username and password is correct then the issue connecting is your firewall blocking certain ports.
Use a different internet connection and it will work.
Make sure to create a user with a different username and password than your mLab account.
In addition to Rafa's answer, if you choose to create a user with the same username and password as your mLab account (which is what I did to save myself from remembering another username and password), you will also not be able to connect.
Upon deleting that user and creating a new one, I was immediately able to connect.

Connecting to MongoLab shell (Azure cloud server)

So i spent quite a bit of time pulling my hair out trying to connect to our MongoLab account at work with a command line shell. I followed the instructions here but no for some reason mongoDB would always return: Error: 18 { code: 18, ok: 0.0, errmsg: "auth fails"}
My user name and password were coming from my database under the users collections which worked fine with my Node.js driver but wouldn't work with my shell.
The question is: where does one find the correct user name and password to connect to MongoLabs with the mongodb shell.
Turns out the user name and password to connect and authorize with the shell aren't found in MongoLabs but in Azure's portal.
Log into your Azure account, in the left column scroll down to Add-Ons and click.
You should see a MongoLab's. Go ahead and click on that.
From there click on Connection Info at the bottom of the window and it will display a string similar to this:
mongodb://username:password#ds012345.mongolab.com:56789/mongolab
go ahead and copy and paste that username and password into your shell with this command :
mongo ds012345.mongolab.com:56789/dbname -u dbuser -p dbpassword
NOTE: Your command will look different because you're using a different MongoLabs account.

Can't insert anything in MongoHQ database

I registered for a free sandbox database on MongoHQ.
As I am starting to use mongoDB I thought it would be a good idea to connect to MongoHQ database from the mongo console and run some commands before using it in any program.
I tried connecting to my database using:
Here testuser is my username and 123456 is my password (I know posting user id and passowrd on forums is bad, this user is temporary and will be deleted by me later).
mongo oceanic.mongohq.com:10076/tnh_data -u testUser -p 123456
and I got this:
MongoDB shell version: 2.4.9
connecting to: oceanic.mongohq.com:10076/tnh_data
Hoping that everything is running fine I tried running a few commands, but I am getting a not authorized error.
> show dbs
Sat Apr 19 22:00:18.090 listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/mongo.js:46
> use learn
switched to db learn
> db.collection0.insert({name: 'Harry', gender: 'Male'})
not authorized for insert on learn.collection0
Please help me what am I doing wrong ?
Just for the information. I created the users using the web interface on mongoHQ.
When using MongoHQ sandbox plans and logging in from the console, there are a couple of things that are at play:
When you connected, using the string above, you are already in your database (tnh_data)
You do not have admin-level access, so "show dbs" will not work in MongoDB console.
So, in MongoDB console, connect again using the same string you started with and, once connected, run this command: "show collections".
Also, without trying to switch to another database, run your insert again. It should work normally this time.
Hope this information is helpful!

Grails - Connect to a MongoDB database using authentication

I am trying to connect to my MongoDB using authentication.
I did this on my Mongo server:
use admin
db.addUser('adminLogin','adminPassword')
db.shutdownServer()
exit
Then I started my server again issuing mongod --auth
I set my db configurations in DataSource.groovy as follows:
grails {
mongo {
host = "localhost"
port = 27017
username = "adminLogin"
password = "adminPassword"
databaseName = "my DB name"
options {
autoConnectRetry = true
connectTimeout = 300
}
}
}
I get the following error message when I start my application:
ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean
with name 'mongoDatastore': FactoryBean threw exception on object creation; nested
exception is org.springframework.data.mongodb.CannotGetMongoDbConnectionException:
Failed to authenticate to database
Any suggestion is most welcome.
Thanks in advance.
I ran into this same issue so I can help explain how Mongo does authentication. You see what you did is you created an admin user in the admin database which is great. However you are trying to connect to "mydb" directly with the admin user which is not allowed. Sound confusing? It's because it is. To illustrate this better here is a simple exercise:
Create a user for the admin db like you have above.
exit the mongo shell
run following
mongo
use myDBname
db.auth("adminlogin", "adminpwd")
That will fail. But try this instead.
mongo
use admin
db.auth("adminlogin", "adminpwd")
use myDBname
This will work because you switched to this db with the admin context and didn't try to connect to it directly.
So all you need to do to make this work is connect directly to the DB you want and create a user right in that db like follows:
mongo
use myDBname
db.addUser("dblogin", "dbpwd")
Update your grails config file with this and I bet you it will work.
Note that just the last part is your answer and solves your problem but since I struggled with this and figured it out the hard way I think the context really helps understand mongo auth better.
Take Care