Error 13 when trying to authenticate to MongoDB - mongodb

I'm trying to connect to MongoDB through JDBC. The connection string is like below,
mongodb://localhost:27017/games?authSource=admin
However I'm getting the following trace:
{ "ok" : 0.0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1 }", "code" : 13 }
My intention is not listing all the databases, but the user has to authenticate against admin database and can read/write on games database. What mistake I'm making here?
I want user X to authenticate against admin DB but read just games DB so not sure why it asks for listDatabase privilege.

First, I assume you are using the MongoDB Java Driver, which is actually not JDBC.
It would be helpful for you to share:
How you created your user
The Java code that you are executing
The version of mongo-java-driver and MongoDB that you are using
But based on the error, it appears that you are successfully authenticating. I strongly suspect that you are either directly calling listDatabases() or listDatabaseNames().
The other thing that does not look quite right is the fact that you are specifying authSource=admin in your MongoClientURI. But that issue should have given you an Autentication Failed error. You should be either leaving the authSource off of the connection string or specify authSource=games.
Based on what you described, when you created your user, you should have created the user in the games database (users will actually be stored in the admin database, but you would be authenticating against the games database).

Related

Why is there a database named "test" when connecting MongoDB using Studio3T?

I'm learning MongoDB and trying to use Atlas. I created a remote cluster and tried to connect it using both MongoDB Compass and Studio 3T. However, I noticed that after connecting with Studio 3T, there was an empty database named "test" appearing in the left panel, below "admin" and "local" databases. Where did it come from? And how can I drop it? Because when I tried to drop this database, I got this error
Mongo Server error (MongoCommandException): Command failed with error 8000 (AtlasError): 'user is not allowed to do action [dropDatabase] on [test.]' on server ac-bkmhuxm-shard-00-02.w2nutv2.mongodb.net:27017.
The full response is:
{
"ok" : 0.0,
"errmsg" : "user is not allowed to do action [dropDatabase] on [test.]",
"code" : 8000.0,
"codeName" : "AtlasError"
}
After changing the roles in Atlas, I can now delete the database. However it keeps appearing when I make a new connection to MongoDB. Why is that?
Database test is the default database when you don't define anything.
Databases local, admin and config are MongoDB system internal databases, you should not touch them unless advised by MongoDB support or special admin tasks.
See also 3 default database in MongoDB

MongoDB Atlas: cluster paused and Authentication failed

I am back working on a project I didn't touch for monthes.
On mongoDb Atlas I have this message :
As you can see, the Resume button is disabled so I cannot click it.
When I try to connect to mongo through either NodeJs, Mongo Compass or mongosh, i get the same error over and over :
bad auth : Authentication failed.
I am not including the <> in the connection string password.
I tried to change my user password and user the new one.
I tried to create a new user for the database and use this user/password.
I am not using the account password (I use the one created with the db user)
I use the exact same connection string given in the "connect" button in mongo atlas and I replace the "<password>" field with my user's password.
But I always have this error :
bad auth : Authentication failed.
I have another cluster which was paused too. But for this second one the resume button was enabled and I can connect the databases without any error.
Any help will be appreciated. Thanks.

MongoDB: how do I authenticate when executing the `copydb` command

I have a replica set on a remote host which requires authentication in order to connect. The original (root) user was created in the admin database which I have used in order to remotely connect. I building some sort of a "backup" script which copies a db into the replica set and in a later time I should be able to copy a db from the remote location into other MongoDB instances.
So I wrote the script to copy a database by connecting TO the remote location, authenticating and then running the db.runCommand using copydb: 1. It works great, no problems here.
When I try to copy a db back into my local machine that's when things go wrong, mainly because I have to authenticate as part of the copydb command. I originally tried to use the same technique (db.runCommand) but since the nonce and key authentication are messy by themselves I tried to solve the problem first by writing the commands manually into mongo's shell using db.copyDatabase, according to the documentation it should do this process for me.
This is the command:
db.copyDatabase('from_db', 'to_db', 'remote.host.example.com', 'my_user', 'my_password')
Which responds with:
{ "ok" : 0, "errmsg" : "Authentication failed.", "code" : 18 }
I tried switching roles (root, userAdmin, readWrite, ...) but nothing works. I tried creating another user inside the db I am trying to copy, but that didn't seems to do much other than change the response a little into:
{
"ok" : 0,
"errmsg" : "unable to login { ok: 0.0, code: 18, errmsg: \"Authentication failed.\" }"
}
I searched everywhere, went over anything in the manual which seemed remotely relevant and I still can't figure it out.
How am I suppose to copy a db from a remote location which requires an authentication??

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!

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