I have a peculiar question that I haven't been able to find an answer to yet. I have a database and a collection in it. Let's call them "test database" and "test collection". There are a range of users from root to a user with read-only access to the "test database". I need to output a special log file for operations that are carried out in a "test database".
I have already found "mongo.log" in the path "/var/log/mongodb/". But all possible requests from all "databases" are loaded into this file at once, which is not convenient. Is it possible somehow to create a separate file for writing logs only from the "test database"?
I also found out earlier that in order to keep track of all requests, I need to set the following settings:
$ mongo
MongoDB shell version: 2.4.9
connecting to: test
> use myDb
switched to db myDb
> db.getProfilingLevel()
0
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
> db.getProfilingLevel()
2
Related
We dumped the definition of indexes using mongo-index-exporter and we try to apply them at each launch of our application, so to achieve a reproducible environment, by executing
mongo --verbose "mongodb://$MONGO_DB_USER:$MONGO_DB_PASSWORD#$MONGO_DB_HOST/$MONGO_DB_NAME?authSource=$MONGO_AUTH_SOURCE&authMode=scram-sha1" indexes.js
on a file like so:
print('Using db ' + db)
setVerboseShell(true)
print('Creating indexes on mycoll1')
db.mycoll1.createIndex({"_id":1}, {"name":"_id_", "background": true});
db.mycoll1.createIndex({"createdAt":1,"field1.field2.field3":1}, {"name":"coll1_field123_idx"});
print('Creating indexes on mycoll2')
db.mycoll2.createIndex({"_id":1}, {"name":"_id_", "background": true});
db.mycoll2.createIndex({"createdAt":1,"field1.field2.field3":1}, {"name":"coll1_field123_idx"});
To create indexes on a replicaSet with five nodes, we are performing the
following command:
mongo --verbose "mongodb://$MONGO_DB_USER:$MONGO_DB_PASSWORD#$MONGO_DB_HOST/$MONGO_DB_NAME?authSource=$MONGO_AUTH_SOURCE&authMode=scram-sha1" indexes.js
this works fine for us in QA, where MONGO_DB_HOST is a single node and we are using a fresh new empty database, and doesn't work in production where additionally the database is already existing (and the collection have contents).
Additional relevant information:
background mode doesn't make any difference
The verbose mode doesn't work, i.e. although we launch mongo client with -v and we do a setVerbose(true) nothing is logged on the console
Our mongo shell is 3.6.11 and our mongo server is 3.2.9
Copy pastying the commands inside a mongo shell executed on the server works terminates immediately and results in the following output
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 11,
"numIndexesAfter" : 11,
"note" : "all indexes already exist",
"ok" : 1
}
I am trying to copy a database from a remote host over to the current database. I am using this command:
rs0:PRIMARY> db.copyDatabase("olddb", "newdb", "xx.xx.xx.137", "user1", "abc123");
But I am getting this error:
{
"done" : true,
"ok" : 0,
"errmsg" : "failed to create collection \"newdb.email_batches\": collection already exists"
}
Except the collect does not exist. What could I be doing wrong in this command?
The problem was different database versions. The DB I was on was Mongo 3.0 and the database I was trying to transfer from was 3.2
What is very strange about that is the command copyDatabase as functionaliy for transferring data from 2.6 to 3.2, yet for some reason does not work with 3.0.
According to Getting Started with MongoDB, we can use show dbs to get the list of existing databases.
But this is a kind of command running in the mongo shell.
My question is that how to write a mongodb query (bson) for the list of databases, and where this query should be sent to?
It's not a query, but you can run the listDatabases command against the admin database to get a list of databases in BSON:
> use admin
switched to db admin
> db.runCommand({listDatabases : 1})
{
"databases" : [
{
"name" : "local",
"sizeOnDisk" : 83886080,
"empty" : false
},
],
"totalSize" : 83886080,
"ok" : 1
}
Most programming language engines for MongoDB can also run commands in addition to queries given the right syntax. For example, in Java:
DB db = mongo.getDB("admin");
DBObject cmd = new BasicDBObject("listDatabases", 1);
CommandResult result = db.command(cmd);
Interestingly, if you're really insistent on it being a query, you can query the virtual collection $cmd on the admin database to run commands by submitting queries:
> use admin
switched to db admin
> db.$cmd.findOne({"listDatabases":1})
I'm playing around with this tutorial that uses Sinatra, backbone.js, and mongodb for the database. It's my first time using mongo. As far as I understand it the app uses both local storage and a database. it has these routes for the database. For example, it has these routes
get '/api/:thing' do
DB.collection(params[:thing]).find.to_a.map{|t| from_bson_id(t)}.to_json
end
get '/api/:thing/:id' do
from_bson_id(DB.collection(params[:thing]).find_one(to_bson_id(params[:id]))).to_json
end
post '/api/:thing' do
oid = DB.collection(params[:thing]).insert(JSON.parse(request.body.read.to_s))
"{\"_id\": \"#{oid.to_s}\"}"
end
After turning the server off and then on, I could see in the server getting data from the database routes
127.0.0.1 - - [17/Sep/2012 08:21:58] "GET /api/todos HTTP/1.1" 200 430 0.0033
My question is, how can I check from within the mongo shell whether the data's in the database?
I started the mongo shell
./bin/mongo
I selected the database 'use mydb'
and then looking at the docs (http://www.mongodb.org/display/DOCS/Tutorial) I tried commands such as
> var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
but they didn't return anything.
You don't really have to create a cursor just to read some data.
% mongo
MongoDB shell version: 2.2.0
connecting to: test
> use mydb
switched to db mydb
> show collections
system.indexes
things
> db.things.find()
{ "_id" : ObjectId("50569a52c0ba23837b5dd811"), "name" : "TV set" }
{ "_id" : ObjectId("50569a5bc0ba23837b5dd812"), "name" : "car" }
If db.things.find() doesn't print anything, then there's no data in that collection (or you mistyped its name, or are using the wrong database).
I am trying to get started with the salat plugin in playframework. I have configured the database in application.conf and added all the dependencies to Build.scala and added salat to the play.plugins file. I haven't actually added any code to the project yet, I just followed the instructions on the github page, and then tried to run the project. I am getting the following error message
(Server started, use Ctrl+D to stop and go back to the console...)
[info] play - mongodb [default] connected at heroku_app4620908#ds031907.mongolab.com:31907/heroku_app4620908
[error] application -
! #6bchnaacn - Internal server error, for request [GET /] ->
play.api.Configuration$$anon$1: Configuration error [couldn't connect to [ds031907.mongolab.com/107.21.153.211:31907]]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:71) ~[play_2.9.1-2.0.3.jar:2.0.3]
at play.api.Configuration.reportError(Configuration.scala:258) ~[play_2.9.1-2.0.3.jar:2.0.3]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:105) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:98) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
Caused by: com.mongodb.CommandResult$CommandFailure: command failed [listDatabases]: { "serverUsed" : "db-uri" , "errmsg" : "need to login" , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:88) ~[mongo-java-driver-2.8.0.jar:na
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134) ~[mongo-java-driver-2.8.0.jar:na]
at com.mongodb.Mongo.getDatabaseNames(Mongo.java:356) ~[mongo-java-driver-2.8.0.jar:na]
at com.mongodb.casbah.MongoConnection.getDatabaseNames(MongoConnection.scala:190) ~[casbah-core_2.9.1-2.4.1.jar:2.4.1]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:103) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
at se.radley.plugin.salat.SalatPlugin$$anonfun$onStart$1.apply(SalatPlugin.scala:98) ~[play-plugins-salat_2.9.1-1.0.8.jar:1.0.8]
I am stumped because I added my password and everything to the conf file. From the log it looks as though whatever is trying to connect to the database for me, is not logging in first, using the info I provided.
As you say, it looks like the root issue is that MongoDB is rejecting the command "listDatabases". This command requires administrator access to the MongoDB process since it reveals information about the other databases hosted there.
Unfortunately, the message it returns, "need to login", is a little misleading. You have logged in! You just don't have permission to list the databases.
Here's a simple experiment you can try yourself with the MongoDB shell. See that "show dbs" fails with the same error message you got in your app, but "show collections", which doesn't require access to any other databases outside your own, succeeds?
% mongo ds031907.mongolab.com:31907/heroku_app4620908 -u heroku_app4620908 -p your_password
MongoDB shell version: 2.0.7
connecting to: ds031907.mongolab.com:31907/heroku_app4620908
> show dbs
Fri Aug 17 13:12:10 uncaught exception: listDatabases failed:{ "errmsg" : "need to login", "ok" : 0 }
> show collections
system.indexes
system.users
(Note: I did this with my own MongoLab account and modified the text when copying and pasting here so you could just copy it into your terminal.)
Is there a way to avoid making the listDatabases call? I'm not familiar with the framework you're using.
I've the problem as well...
it should come with the SalatPlugin's onStart method that is requesting all database names: source._2.connection.getDatabaseNames().
This code is just testing the aliveness of the server...
I'm gonna check with leon how we could this differently! Sadly, you won't be able to connect until this will be fixed!
Stay tuned on this issue https://github.com/leon/play-salat/issues/23
This is fixed in the latest version of play-salat, it now uses getCollectionNames instead
use admin
db.addUser('userName', 'userPassword')
db.auth('userName', 'userPassword')
show dbs
java:
DB db = mongo.getDB("YouDBName") ;
db.authenticate("userName", "userPassword".toCharArray()) ;
System.out.println(mongo.getDatabaseNames()) ;