How to find mongodb data and log files location through command?
like below SQL server command.
SELECT * FROM sys.database_files
The easiest way is probably with the getCmdLineOpts command:
db.getSiblingDB("admin").runCommand({getCmdLineOpts:1})
This Mongo Shell command will first switch to the admin database then execute the getCmdLineOpts command. An alternative is the shell wrapper:
db.serverCmdLineOpts()
These will return the parsed command line options, which should contain both the data directory being used and the log path.
{
"argv" : [
"C:\\****\\3.4.10\\bin\\mongod.exe",
"--dbpath",
"C:\\****\\data",
"--port",
"27017",
"--logpath",
"C:\\****\\data\\mongod.log",
"--bind_ip",
"0.0.0.0"
],
"parsed" : {
"net" : {
"bindIp" : "0.0.0.0",
"port" : 27017
},
"storage" : {
"dbPath" : "C:\\****\\data"
},
"systemLog" : {
"destination" : "file",
"path" : "C:\\****\\data\\mongod.log"
}
},
"ok" : 1
}
Note: I obfuscated my paths, they do not normally contain ****.
You can see it provides both the raw values as well as the parsed values. If both command line options and config file options are specified on the command line, this will show the effective values being used by the process. Keep in mind there are several extra options that can effect where data is stored but this should get you on your way pretty quickly.
If you would like to know this information without using the Mongo Shell you will have to either grep the config file or look at the command line options of the running process, or both.
You can view logs in mongoCLI also
to list all logs
> show logs
global
startupWarnings
show log content
> show log global
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] MongoDB starting : pid=778 port=27017 dbpath=/var/lib/mongodb 64-bit host=ubuntu
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] db version v3.6.1
2018-01-30T09:14:10.305+0530 I CONTROL [initandlisten] git version: 025d4f4fe61efd1fb6f0005be20cb45a004093d1
data path will be printed on global log line 1, in my machine its dbpath=/var/lib/mongodb
Related
I am trying to use scripts js in the mongo shell. I am reading a book on the topic. I have tried several options, such as opening the cmd at a specific place at my computer.
I found here the question "How to execute mongo commands through shell scripts?", but nothing seems to work for me. My mongo is working properly and I can enter mongo simply typing mongo at cmd. However, it cannot find the js files.
I am using Windows; anything has an idea what may be happening under the hood?
An example after:
mongo demo.js
2020-02-20T11:03:33.098-0300 E - [main] file [demo.js] doesn't exist
2020-02-20T11:03:33.098-0300 F - [main] failed to load: demo.js
2020-02-20T11:03:33.098-0300 E - [main] exiting with code -3
Create your my_script.js file with this one command:
db.testColl.insertOne( { a: "hello" } )
Place the script file in your current directory.
1. Run JS Script from OS Command-line:
From OS prompt do this:
> mongo localhost/testDB my_script.js
Once the above command is run, you will see the output as follows (similar, depending upon your MongoDB version and the OS (Windows, in this case)):
MongoDB shell version v4.2.3
connecting to: mongodb://localhost:27017/testdb?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("456b350f-668f-4389-9901-7c456e2c50fe") }
MongoDB server version: 4.2.3
Now, from the Mongo Shell (i.e., do mongo, and then from the mongo prompt):
mongo > use testDB
mongo > show collections
You will see the testColl listed.
mongo > db.testColl.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
This is the document created in the testDB database and the collection testColl as per the command in the my_script.js.
2. Run JS Script from Mongo Shell:
Also, you can run the my_script.jsfrom within the Mongo Shell.
mongo > load("my_script.js")
true
mongo > db.test.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
See there are two documents with different _id field values.
NOTE: I think you can run only some commands from the .js file.
3. Another Example:
Create a JS file named script2.js with the following content:
db.test.find().forEach(printjson)
Note the printjson shell method prints a document to the shell output.
mongo > load("script2.js")
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
References:
load()
Print document value in
MongoShell
How to set rs.slaveOk() in secondary mongodb servers in replicaset via commandline?
I tried following methods :
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(rs.slaveOk())"
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(rs.slaveOk(true))"
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(db.getSiblingDB('admin').getMongo().setSlaveOk())"
the command executes with undefined in the output log.
I am trying to set this via the shell in primary server.
Create a file /etc/mongorc.js and add rs.slaveOk() there. The file is being evaluated on each shell startup.
For more information have a look here
From MongoDB version 4.4 onwards, you might get a warning displayed like:
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
So, please prefer using rs.secondaryOk()
Calling the below should work fine, there is no return type for the method so nothing will get printed back to the screen
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "rs.slaveOk()"
Running rs.slaveOk in the mongo.exe will also how how it is implemented as it is just a helper method:
> rs.slaveOk
function (value) { return db.getMongo().setSlaveOk(value); }
>
And also the setSlaveOk method:
> db.getMongo().setSlaveOk
function ( value ) {
if( value == undefined ) value = true;
this.slaveOk = value;
}
You could always try to query one of the collections on the secondary to make sure the node is queryable:
> db.test.findOne()
null
Update - bit more clarity
Setting slaveOk() is only valid for that console session that it was executed in, so you would need to pass in a script or stay connected to the console with the --shell arguments for exmaple
C:\mongodb\bin>mongo.exe --port 27012 --eval "rs.slaveOk()" --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
{ "_id" : ObjectId("5630fdf2af4abd9f8ae7f79c"), "test" : true }
rs1:SECONDARY>
If we don't pass in the rs.slaveOk() the we get the following response:
C:\mongodb\bin>mongo.exe --port 27012 --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
rs1:SECONDARY> exit
bye
JFYI :
looks like rs.slaveOk() will be deprecated soon, instead MongoDB suggest to use rs.secondaryOk()
Following is the official warning you gonna see in MongoShell:
WARNING: slaveOk() is deprecated and may be removed in the next major
release. Please use secondaryOk() instead.
Cheers
I'd like to copy a collection from one database to an instance on another server. From other stackoverflow questions, I understand the correct way to do that is with this command:
{ cloneCollection: "<collection>", from: "<hostname>", query: { <query> } }
via http://docs.mongodb.org/manual/reference/command/cloneCollection/
However, I don't understand where do I enter this command? It isn't accepted as...
$ mongod { cloneCollection: "remote", from: "ec2-whatever-amazon.com"}
How do I copy a remote collection at db.remote.collname to db.local.collname using this cloneCollection syntax via command line?
MongoDB database commands are run using db.runCommand() from the mongo shell. Refer to http://docs.mongodb.org/manual/tutorial/use-database-commands/.
Try something like this (using another database command for simplicity):
$ mongo
> db.runCommand({ isMaster: 1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"localTime" : ISODate("2014-02-18T22:30:04.417Z"),
"ok" : 1
}
>
Environment:
3.4.9-gentoo
mongodb (OpenRC) 0.9.8.4 (Gentoo Linux)
if I use mongod daemon to start mongodb,the default db path is /data/db
But if I use /etc/init.d/mongodb script to start mongodb, the /etc/conf.d/mongdb write the default db path is /var/lib/mongodb,
I am puzzled that why the db path is not the same?
The default dbpath if you start MongoDB without a configuration file is /data/db.
Your init script (/etc/init.d/mongodb) is starting mongodb with the --config (aka -f) option and a path to a config file to use (/etc/conf.d/mongodb).
If you look at the contents of your /etc/config.mongodb configuration file, you should see the dbpath setting with the /var/lib/mongodb directory path that overrides the default. In this case the maintainer of your MongoDB install package has decided that /var/lib is the most appropriate default directory for data files. Generally this is done to be more consistent with the default locations used by other packages in your distribution; the MongoDB data files can live anywhere on your filesystem.
You can also check for any settings that have been overridden by your configuration file in the mongo shell using:
getCommandLineOpts()
The output will be similar to:
{
"argv" : [
"mongod",
"--dbpath",
"/var/lib/mongodb"
],
"parsed" : {
"dbpath" : "/var/lib/mongodb"
},
"ok" : 1
}
For example, I want to know the database directory that is used by mongodb to run what it is?
The documentation said that the default data is in /data/db however, there is no such directory
I wonder if there is a mongodb command to get that simple info. I look through the web and could not find it.
You can see the Configuration Parameters used with:
> db.serverCmdLineOpts()
For example:
{
"argv" : [
"mongod",
"--dbpath",
"/usr/local/db"
],
"parsed" : {
"dbpath" : "/usr/local/db"
},
"ok" : 1
}
Any parameters not specifically listed will be using their default values.
Just create a folder called data anywhere in your system.
Then let Mongo know where the this folder is by updating the path.
in windows you would run this on the command line.
C:\mongodb\bin\mongod.exe --dbpath c:\test\mongodb\data
This is where your mongo stores your data.