shell commands in mongo inside Docker fails for Authentication - mongodb

I am trying to run some shell commands in mongo which is inside docker container.
docker exec -it mongodb_mock bash
It opens the mongo shell. but when I run any command in this it gives authentication error:
> show dbs
2018-08-28T10:12:12.755+0000 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:62:1
shellHelper.show#src/mongo/shell/utils.js:781:19
shellHelper#src/mongo/shell/utils.js:671:15
#(shellhelp2):1:1
Please help.
EDIT: This db is created through mongoose/express, from another container.

first you need to authorize db with user, below is example command
use admin;
db.auth('admin','password');
then your able to run all querys. or start mongod without --auth parameter so you no need user authentication.

Related

Installing Mongodb in Kubernetes using Helm

I installed mongodb using Helm in my Kubernetes cluster.
https://github.com/kubernetes/charts/tree/master/stable/mongodb
It works fine and lets me connect to the db. But I cannot do any operation once I log into the mongo server. Here is the error I get when I try to see the collections.
> show collections;
2018-04-19T18:03:59.818+0000 E QUERY [js] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {}, $db: \"test\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand#src/mongo/shell/db.js:942:1
DB.prototype.getCollectionInfos#src/mongo/shell/db.js:954:19
DB.prototype.getCollectionNames#src/mongo/shell/db.js:965:16
shellHelper.show#src/mongo/shell/utils.js:836:9
shellHelper#src/mongo/shell/utils.js:733:15
#(shellhelp2):1:1
I am logging in as the root user using
mongo -p password
I don't know why even the root user has no authorization to do anything.
I found the issue. By default, MongoDB uses the admin DB to authenticate but in the helm chart, the authentication DB is the same as the DB that you create with it. So If I create a DB called test, the authentication DB will also be test

mongoexport works but mongo shell fails on mlab

I have a mongo db setup on mlab for use with meteor running on Galaxy. I can succesfully export from this database using mongoexport on my windows 8.1 64-bit laptop. I also want to connect with the mongo shell, but I get "not authorized" error messages, using the exact same mongo user and password.
In more detail - this works just fine, using mongoexport version r3.4.7:
mongoexport --host ds121225.mlab.com:21225 -d quarule-db01 --collection Quarule -u xxxxx -p xxxx -q "{vocabularyName: 'qre'}" --out QRE.json
while this fails:
mongo ds121225.mlab.com:21225/quarule-db01 -u xxxx -p xxxxx
MongoDB shell version v3.4.7
connecting to: mongodb://ds121225.mlab.com:21225/quarule-db01
MongoDB server version: 3.4.9
rs-ds121225:PRIMARY> show dbs
2017-10-30T19:50:37.795-0700 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:62:1
shellHelper.show#src/mongo/shell/utils.js:769:19
shellHelper#src/mongo/shell/utils.js:659:15
I have checked and rechecked that I am specifying the userid and password the same way in both commands.
Why would the export succeed and the shell fail?

windows 10 mongodb "show dbs" "missing ; before statement"

yes I just started to understand MongoDB. Therefore I have installed MongoDB in the folder "Program files" yesterday and tryed to figure out, how to work with it.
Today I simply tryed to start it again. But something went wrong. To understand the beginning better I deinstalled MongoDB, deleted the MongoDB, data, log folders and have installed MongoDB again direct on c:/.
Started the server:
mongod --directoryperdb --dbpath c:\mongodb\data\db --logpath c:\mongodb\log\mongo.log --logappend --auth --rest --install
Started the service:
net start MongoDB
And the Mongo shell:
mongo
Now I try "show dbs" with the following result:
>show dbs
2017-09-14T10:41:49.541+0200 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs#src/mongo/shell/mongo.js:62:1
shellHelper.show#src/mongo/shell/utils.js:769:19
shellHelper#src/mongo/shell/utils.js:659:15
#(shellhelp2):1:1
More I haven't done already. What can I do to show the dbs?
Of cource, I have tryed the learnings of the following post to, without success:
show dbs gives "Not Authorized to execute command" error
Looks like you are starting the db with --auth but not connecting with username/password. If you have not created any user then remove --auth and restart db. Then you will be able to connect.

MongoDB - What is the privilege needed to run setFeatureCompatibilityVersion?

Using mongoDB 3.4.3 I'm facing the problem described in this issue (https://jira.mongodb.org/browse/SERVER-26556) when trying to create an index with collation.
Since I've upgraded from a previous version, it says what is needed is to explicitly allow the backwards-incompatible 3.4 features set, but with a user with role userAdminAnyDatabase I still have no permission:
> db.adminCommand({setFeatureCompatibilityVersion: "3.4"})
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command {setFeatureCompatibilityVersion: \"3.4\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
My question is, what role does the user needs to be able to do this?
Using a user with root privilege worked.
After authentication:
> use admin
> db.adminCommand({setFeatureCompatibilityVersion: "3.4"})
{ "ok" : 1 }
I was not able to run the command as I was getting the following error :
db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { setFeatureCompatibilityVersion: \"3.4\", $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
The following link gives details :
https://dba.stackexchange.com/questions/159390/mongodb-all-commands-spit-out-not-authorized-on-admin-to-execute-command
Steps I followed (I am using Mac) :
sudo nano /usr/local/etc/mongod.conf
Comment the security as shown below:
Restart MongoDB. sudo brew services restart mongodb
Connect using mongodb and run the following code
use admin
db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
Uncomment the security in mongod.conf and restart the MongoDB server.
To be able to execute setFeatureCompatibilityVersion user must have "clusterManager" built-in role.
See documentation here: https://docs.mongodb.com/manual/reference/built-in-roles/#clusterManager

How to interact with MongoDB via shell in OpenShift Online 3 Dev Preview?

I've got multiple issues with an application which I suspect are related to permissions on the database.
Everything seems locked down however and I can't complete basic commands such as show dbs in order to troubleshoot the problems further and "see" what I'm working with. I've been stuck on this for two days now and it's really frustrating.
I've tried this both from the online console and on local terminal, both with and without user credentials supplied at login:
Online Console
Console > MongoDB Service > Deployment > Pod > mongodb-1-vs19d > Terminal:
sh-4.2$ mongo
MongoDB shell version: 2.6.9
connecting to: test
> show dbs
2016-07-13T04:33:10.809-0400 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases:
1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
Local Terminal
me#my-computer:~$ oc rsh mongodb-1-vs19d
sh-4.2$ mongo
MongoDB shell version: 2.6.9
connecting to: test
> show dbs
2016-07-13T04:35:06.449-0400 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
Local Terminal With User Credentials
me#my-computer:~$ oc rsh mongodb-1-vs19d
sh-4.2$ mongo -u $MONGODB_USER -p $MONGODB_PASSWORD $MONGODB_DATABASE
MongoDB shell version: 2.6.9
connecting to: users
> show dbs
2016-07-13T04:51:39.127-0400 listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : 13
} at src/mongo/shell/mongo.js:47
Troubleshooting envars are correct:
me#my-computer:~$ oc env pods mongodb-1-vs19d --list
# pods mongodb-1-vs19d, container mongodb
MONGODB_USER=admin
MONGODB_PASSWORD=secret
MONGODB_DATABASE=users
MONGODB_ADMIN_PASSWORD=very-secret
The database was created from local terminal with:
oc new-app mongodb-persistent -p MONGODB_USER=admin,MONGODB_PASSWORD=secret,MONGODB_ADMIN_PASSWORD=very-secret
As per official docs:
https://docs.openshift.com/online/getting_started/beyond_the_basics.html#btb-provisioning-a-database
https://docs.openshift.com/online/using_images/db_images/mongodb.html#running-mongodb-commands-in-containers
It looks like you're trying to show dbs as user who has not been granted the needed role. You can try authenticating yourself as admin as follows:
mongo -u admin -p $MONGODB_ADMIN_PASSWORD admin
Then you should be able to show dbs, show users, use other databases and show users there, etc...
It's a bit confusing that your $MONGODB_USER is named admin - this regular user will have access to the $MONGODB_DATABASE. Another admin has been created by the used image most likely as well.