MongoDB : "Couldn't add user : Could not find role" - mongodb

I'm trying to create a role and an user for my MongoDB database.
I created the role, but when I try to create the user I get this error : "Couldn't add user : Could not find role XXX ". Ok ... But when I try to create the role again, mongo tells me the role already exists. What am I suppose to do please ? It's a school project, I'm not familiar with MongoDB.
MongoDB errors

Looks like you added some extra spaces in your database name: " cubesdb ".
Try "cubesdb"

Related

Inserting data to MongoDB Atlas using Mongoose and SRV connection string

I have created a cluster in MongoDB Atlas and can't seem to be able to write data to it.
const uri = "mongodb+srv://myUser:myPass#myDB-4myav.mongodb.net/portfolio";
I have this as my uri to connect to, and every time I try to write to the database, I get this error:
{ MongoError: not authorized on admin to execute command { insert:
"users", documents: [[{name Daniel} {_id
ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
Error: couldn't add user: not authorized on admin to execute command
So basically I don't have a user that can write to my database.
I've also tried making a user with every role possible on the MongoDB Atlas website (for my cluster of course) and then connecting through the mongo shell with it, but that failed as well.
To summarize: I've made a new cluster on MongoDB Atlas. How do I write data to it?
Thanks in advance, feel free to point out if I'm missing something simple and stupid.
{ MongoError: not authorized on admin to execute command { insert: "users", documents: [[{name Daniel} {_id ObjectIdHex("5aa6d7d6396deb25844ccb52")} {__v 0}]], ordered: false }
This error indicates your code is trying to insert documents into the admin database, which is a system database reserved for user and role information.
In your connection string you intended to use the portfolio database. However, the srv connection string format does not support specifying a database so your option was ignored and the default database of admin was used. You need to select the portfolio database after connecting.
This was also reported in the Mongoose issue tracker (GitHub issue #6106) where a user posted a workaround you could adapt:
mongoose.connection.on('connected', function() {
// Hack the database back to the right one, because when using mongodb+srv as protocol.
if (mongoose.connection.client.s.url.startsWith('mongodb+srv')) {
mongoose.connection.db = mongoose.connection.client.db('portfolio');
}
console.log('Connection to MongoDB established.')
});
mongoose.connect(...);
I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:
You need to manage users via MongoDB Atlas rather than the mongo shell. You can create a user account with the "Read and write to any database" role (or more refined privileges using Advanced Options).
You need to create a user with root role from Mongo Shell,
use admin
db.createUser(
{
user: "admin",
pwd: "password",
roles: [ { role: "root", db: "admin" } ]
}
);
exit;
or you need to add new user through atlas(refer below snapshot)
Goto Clusters
click Security
Click ADD NEW USER
type username
create Password based on SCRAM-SHA1 method
choose ROLE Atlas admin
click Add User
After created user, goto Clusters-->OverView --> CONNECT
Verify the IP Whitelist
Choose a connection method:
Connect with Mongo Shell (I am choosing)
Connect your Application
Connect with MongoDB Compass
$ mongo "mongodb+srv://project-u-s3cgp.azure.mongodb.net/test" --username dbAdmin
MongoDB shell version v3.6.1
Enter password:
connecting to: mongodb+srv://project-u-s3cgp.azure.mongodb.net/test
.........
.........
MongoDB Enterprise project-U-shard-0:PRIMARY>db.user.insert({name:"Daniel"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise O2Ci-U-shard-0:PRIMARY>
I hope, you can able to insert the record after creation of root user.
Thanks,
Karthick

PostgreSQL + IntelliJ : FATAL : role xxx doesn't exist

I am able to connect with psql and do what I want, not in IntelliJ.
PostgreSQL version : postgres (PostgreSQL) 9.6.1
IntelliJ version : 2016.3.1
IntelliJ configuration :
URL : jdbc:postgresql://localhost:5432/sample
User : sample
Pwd : sample
Test connection : Successful
So, I connect (red square appears) and then try a simple query :
select 1;
And I got the error :
FATAL : role 'xxx' doesn't exist`
Why IntelliJ doesn't use the role specified in my configuration and want to use my personal login ?? Whatever I use as role, still got the same error.
Note: If I add my system username as role it works, but I would prefer to avoid that.
Adding the LOGIN role attribute fix the problem :
ALTER ROLE sample LOGIN
The documenation states :
Only roles that have the LOGIN attribute can be used as the initial
role name for a database connection
However, I could connect in CLI and do what I want, as I could "connect" in IntelliJ but not do query, without this attribute. More details about this behaviour are welcome.

Error 13 when trying to authenticate to 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).

Mongo authentication issues

Hello i have a new AWS server with a Bitnami MEAN stack.
I'm root user on the server, and i started up mongo on the command line.
when i try to do anything (other than "use test" , or "use admin"), such as "show dbs"
I get the following error:
show dbs
listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" } at src/mongo/shell/mongo.js:46
I know i'm doing something wrong with regard to permissions, i just dont' know what it is.
when I looked at the mongodb.config, everything looks ok, but i see:
# Turn on/off security. Off is currently the default
#noauth = true
auth = true
Also, possibly relevant, when i try to run mongod, I get errors about "/data/db" doesn't exist, or if i make the directory, I get "not enough space issues"
thank you for your time.
You should not need to run mongod again if it is already running, and chances are that it already is. You are also not saying how you are trying to connect, which is likely your problem.
I would suggest reading the relevant documentation which explains what the default user authentication is and how to connect:
$ mongo admin --username root --pasword YOURPASSWORD
Where the default password is contained in the documentation page. There is also information on setting up new user for your application.
For more information, see the official MongoDB documentation which has many examples:
http://docs.mongodb.org/manual/administration/security-access-control/
You are able to successfully connect to database but you does not have admin privilege for this.
If you know the admin user id and password,You can authenticate by
db.auth("user_name",'passwd');

Unable to add authenticated shard to mongos

I'm trying to add a shard which is authenticated. So when I try to use this command
mongos> sh.addShard("xxx.xxx.xxx:27018")
I'm getting the following error.
{
"ok" : 0,
"errmsg" : "failed listing xxx.xxx.xxx:27018's
databases:{ ok: 0.0, errmsg: \"unauthorized\" }"
}
Please share your thoughts?
It is not clear on what authenticated user you are using and what all authorizations it is entitled to for your session.
I would suggest you provide following information to complete the question:
User you have authenticated with in mongo. i.e. user being used for starting mongo session or any db.auth(..) after that in the mongo shell.
db.isMaster() to confirm you are connected to appropriate mongos. i.e. in mongo shell output of following:
db.isMaster()
Assigned roles for this user in admin / config database i.e. in mongo shell output of following (by replacing the user id):
use admin
db.system.users.find({user: "ReplaceYourUserIdHere"}).pretty()
use config
db.system.users.find({user: "ReplaceYourUserIdHere"}).pretty()
This should help figure out what roles you may be missing and are required for sh.addShard operation.
Some of the operations need specific privileges and sh.addShard is one of them. You can find detailed privilege / role requirements for various operations at http://docs.mongodb.org/manual/reference/user-privileges/ .