I am trying to access mongodb from java using mongo java driver 3.4. I wrote below code to access mongodb and even though I entered wrong credentials while connecting I am able to access my database.
MongoCredential credential = MongoCredential.createScramSha1Credential("rupesh_user", "rupesh_db",
"admin123".toCharArray());
MongoClient mongoClient= new MongoClient( new ServerAddress( "localhost", 27017 ), Arrays.asList(credential));
When I am trying to access mongo database from terminal in ubuntu. Its asking for authentication.
> use rupesh_db
switched to db rupesh_db
> show collections
2017-08-03T13:07:52.970+0530 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on rupesh_db to execute command { listCollections: 1.0, filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
} :
From Console you will have to authorize
> use rupesh_db
switched to db rupesh_db
> db.auth("rupesh_user","admin123")
> show collections
More details
Related
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
I have my AWS EC2 instance given by my client.
On Which There Is an existing database.
But When I SSH to the server using given credentials and run the following command
show collections
I am getting this error
2016-08-30T12:22:52.216+0000 E QUERY Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on parse to execute command { listCollections: 1.0 }",
"code" : 13
How to get my user authenticated perform such operations.Or how to get existing Admin credentials
Just turn off the security from mongodb.conf
noauth = true
#auth = true
so you should be able to logged in as admin now. (use show users to get the list of existing users in base)
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.
I know this is a very common question for MongoDB, but I really tried most of the answers, but could not solve this issue.
So I feel I'm missing something Somewhere
Here are the steps:
Downloaded latest version 3.03 of MongoDB.
Custom installed to f:\MEAN\MongoDB.
F:\MEAN>sc.exe create MongoDBSvc binPath= "\"F:\mean\mongodb
\bin\mongod.exe\" -- service --config=\"F:\MEAN\MongoDBData\log\1.cfg\""
DisplayName= "MongoDBSvc" start= "auto"
[SC] CreateService SUCCESS
F:\MEAN>net start MongoDbSvc
The MongoDBSvc service is starting.
The MongoDBSvc service was started successfully.
F:\MEAN>mongo
MongoDB shell version: 3.0.3 connecting to: test
> view dbs
2015-05-28T14:00:10.907+1000 E QUERY SyntaxError: Unexpected identifier
> show dbs
2015-05-28T14:00:21.886+1000 E QUERY Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { listDatabases:
1.0 }",
"code" : 13
}
at Error (<anonymous>)
at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
at shellHelper.show (src/mongo/shell/utils.js:630:33)
at shellHelper (src/mongo/shell/utils.js:524:36)
at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> ^C
bye
I have a remote mongodb database, and I want to make a copy to my local machine. I have a read-only user on the remote machine, and I must connect to a slave instance from a replica set.
I am trying to run the copydb command, but this is what happens:
> mongo localhost
MongoDB shell version: 2.4.6
connecting to: localhost
> use admin
switched to db admin
> db.runCommand(
{
"copydb" : 1,
"fromhost" : "thisismyurl_blhblbhlblh:31337",
"fromdb" : "remote-db-name",
"todb" : "local-db-name",
"username" : "myusername",
"key" : "543827989734kjjkf3997834897",
"slaveOK" : true
}
)
{
"ok" : 0,
"errmsg" : "namespace query failed remote-db-name.system.namespaces"
}
>
By the way, I can connect the mongo client to the remote machine with the same username and password, and perform queries with no problems.
Any thoughts?