I installed mongohq:sandbox at Heroku. When I want to connect to mongo, it occurs an error:
mongo linus.mongohq.com:10123/app10575123 -u my_user -p pwd123
MongoDB shell version: 2.2.2
connecting to: linus.mongohq.com:10123/app10575123
> show dbs
Wed Jan 9 06:00:50 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
Login and password are correct.
You are connected to the database, but for the shared database plans at MongoHQ (especially the sandbox ones), for security reasons, they do not include admin-level access to the Mongo instance ... only access to your actual database.
"show dbs" is an admin-level command and, in this case, would show other databases on that sandbox MongoDB process.
Instead, you will want to use commands like:
show collections
db.[collection_name].findOne()
db.stats()
db.[collection_name].stats()
db.[collection_name].ensureIndex({foo:1, bar:1}, {background:true})
... and so on.
I hope this helps!
You should be able to show collections, but show dbs requires admin privileges.
Related
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
I have sensu client installed on Mongo DB server and I am executing the Mongo DB Check using this script It Works great when my Mongodb is configured without Authentication, But it does not show the metrics when Authentication is enabled on the Mongo DB, Though I am passing the DB Authentication Credentials correctly, but authentication works with same credential from irb prompt. What should be the fix to get the Monitoring Metric using MongoDB Authentication ?
I am using Sensu version 0.1 6 and MongoDB 2.4.11 on Ubuntu 12.04.
I resolved it,
In fact it is not an sensu-plugin issue, it was an user authentication issue with MongoDB 2.4, Mongo allows the user which is created within admin db to collect the information of other databases.
So the following simple steps solved my issue
rahul#rahul:~$ mongo
MongoDB shell version: 2.4.12
connecting to: test
> use admin
switched to db admin
> db.addUser("rahul","rahul#123")
{
"user" : "rahul",
"readOnly" : false,
"pwd" : "85a20670734aeb830a7903183bd4132f",
"_id" : ObjectId("54d88f4950e99f42d01abfe8")
}
> use mydb
switched to db mydb
>
Then I modified following paramaters in mongodb-metrics.rb which is freely available here
option :user,
description: 'MongoDB user',
long: '--user rahul',
default: nil
option :password,
description: 'MongoDB password',
long: '--password rahul#123',
default: nil
and note that the following has to be unchanged
db_name = 'admin'
here I was giving db_name as 'mydb' which was going wrong
after all I can see my metrics and graphs
I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI:
mongodb://username:password#somewhere.mongolayer.com:10011/my_database
I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program in order to connect to this remote database.
However, I can't find the right command to get me the full shell access I would like. Using instructions found here http://docs.mongodb.org/manual/reference/program/mongo/ (search for "remote") I am able to get what looks like a connection, but without giving my username or password I am not fully connected. Running db.auth(username, password) returns 1 (as opposed to "auth fails" when I provide incorrect username and password), but I continue to get an "unauthorized" error message when issuing the show dbs command.
You are probably connecting fine but don't have sufficient privileges to run show dbs.
You don't need to run the db.auth if you pass the auth in the command line:
mongo somewhere.mongolayer.com:10011/my_database -u username -p password
Once you connect are you able to see collections?
> show collections
If so all is well and you just don't have admin privileges to the database and can't run the show dbs
With Mongo 3.2 and higher just use your connection string as is:
mongo mongodb://username:password#somewhere.mongolayer.com:10011/my_database
Another way to do this is:
mongo mongodb://mongoDbIPorDomain:port
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!
I am bit new to MongoDb and I am using MongoDb v.2.4.Here the problem is that when I run the mongod with --auth parameter, even after authenticating I am not able to perform simple operations such as "show users". But works fine if I run mongod without --auth parameter.
> use admin
switched to db admin
> db.auth("dbadmin","mypassword")
1
> show users
**Thu Feb 27 16:50:17.695 error: { "$err" : "not authorized for query on admin.sys
tem.users", "code" : 16550 } at src/mongo/shell/query.js:128**
> use admin
switched to db admin
> db.grantRolesToUser("your_admin_name" ,[ "root"])
This command will give you all privileges on any db as admin.
Firstly you should to run mongod on localhost without --auth and create user with necessary roles that you needed. In your case you should add userAdminAnyDatabase or userAdmin role. Than you could run mongod with --auth and authenticate by this user for have remote access to system.users collections.
You could read about it here.
I think your admin user has not been configured as mongo says.
I have just answer a question in another thread with explanation step by step :
Not authorized for query on admin.system.namespaces on mongodb
try it.
Not for the OP, but for those of you landing here on an internet search for
Failed: error counting admin.system.users: not authorized on admin to execute command { count: "system.users", query: {}, $db: "admin" }
The answer, most likely, is that you need to specify the database name in the connection string. For example, in my case, I was doing a mongorestore, like so
mongorestore --uri=mongodb+srv://[username]:[password]#[mongo-server] --dir=[backup-file]
And what I needed to do was
mongorestore --uri=mongodb+srv://[username]:[password]#[mongo-server]/[database-name] --dir=[backup-file]
The same thing would apply to a mongodump. The point is that it tries to connect to the admin DB by default, and that might not be what you're trying to do.