About the mongodb's default db path? - mongodb

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
}

Related

How can I change the .dbshell history file location

With cmd in Win10, after terminate the mongo service, I got the error:
Error saving history file: FileOpenFailed Unable to fopen() file
C:\Users\鍒樺簡鏂嘰.dbshell: Access is denied.
If use Powershell with Admin, I will get this strange file in C:\Users:
鍒樺簡鏂嘰.dbshell
So, the error occurs for the reason of my non-English name directory, so how to change the location of the .dbshell file in MongoDB? Thanks!
You can set it with environment variable USERPROFILE:
c:\>set USERPROFILE=C:\Temp Files\IMP
c:\>mongo --norc
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b242c05d-68d5-48a5-ba06-43665f3eb2e9") }
MongoDB server version: 4.4.1
MongoDB Enterprise > db
test
MongoDB Enterprise > print("Hello World")
Hello World
MongoDB Enterprise > exit
bye
c:\>cat "C:\Temp Files\IMP\.dbshell"
db
print("Hello World")
c:\>
see How to get to the Mongo shell history file or all history on Windows
Note, at startup the mongo shell reads file %USERPROFILE%\.mongorc.js, so you may have to move this file as well.

Unable to set HomePath And config in grafana

I am new to grafana and I am getting this error while executing the grafana-server.exe
Grafana-server Init Failed: Could not find config defaults, make sure homepath command line parameter is set or working directory is homepath
Firstly, I am not clear about which path to specify as homepath and which to specify as config path.
Secondly, I have tried to set the homepath using this command:
grafana-cli admin reset-admin-password --homepath "c:\" mynewpassword
But getting this error :
"Incorrect Usage: flag provided but not defined: -homepath"
in grafana version 7.3.5 this is the help message.
NAME:
Grafana CLI - A new cli application
USAGE:
grafana-cli [global options] command [command options] [arguments...]
VERSION:
7.3.5
AUTHOR:
Grafana Project <hello#grafana.com>
COMMANDS:
plugins Manage plugins for grafana
admin Grafana admin commands
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--pluginsDir value Path to the Grafana plugin directory (default: "/var/lib/grafana/plugins") [$GF_PLUGIN_DIR]
--repo value URL to the plugin repository (default: "https://grafana.com/api/plugins") [$GF_PLUGIN_REPO]
--pluginUrl value Full url to the plugin zip file instead of downloading the plugin from grafana.com/api [$GF_PLUGIN_URL]
--insecure Skip TLS verification (insecure) (default: false)
--debug Enable debug logging (default: false)
--configOverrides value Configuration options to override defaults as a string. e.g. cfg:default.paths.log=/dev/null
--homepath value Path to Grafana install/home path, defaults to working directory
--config value Path to config file
--help, -h show help (default: false)
--version, -v print the version (default: false)
so you can set it by passing --homepath .
be careful, it seems its diffrent with grafana-server parametes. in that case you must set flag with only 1 hyphen (-homepath)
but come back to your problem there is 2 things to say.
first is to order your command correctly. i mean something like this
grafana-cli --homepath path ...
because homepath flag is for grafana-cli so it must come right after that or there will not be any guarantee of "what you want to do is what you write".
second is the homepath. consider this tree directory
.
|_LICENSE
|_NOTICE.md
|_README.md
|_VERSION
|_bin
|_conf
|_data
|_plugin-bundled
|_public
|_scripts
here is installation directory or homepath which you must set. more specifically exacly around bin(or conf or data or ...) directory.

How to find mongodb data and log files location through command?

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

mongo --shell file.js and "use" statement

can't find solution for simple question:
I have file text.js
use somedb
db.somecollection.findOne()
When I run this file in cmd with redirection command from file:
"mongo < text.js"
it's work properly
But when I try this way
"mongo text.js" or "mongo --shell test.js"
I got this error message
MongoDB shell version: 2.2.0
connecting to: test
type "help" for help
Wed Dec 05 16:05:21 SyntaxError: missing ; before statement pathToFile\test.js.js:1
failed to load: pathToFile\test.js.js
It's fail on "use somedb". If I remove this line, it's run without error, but console is clear.
is there any idea, what is this and how to fix?
I'm tying to find sollution for this, to create build tool for Sublime Text 2.
default build file was
{
"cmd": ["mongo","$file"]
}
but in this case I get the error above
PS. right after posting this question I find sollution for SublimeText2:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo < ${file}"]
}
PSS. right after posting this question I find sollution for SublimeText3:
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"]
}
this build tool work properly
use dbname is a helper function in the interactive shell which does not work when you are using mongo shell with a JS script file like you are.
There are multiple solutions to this. The best one, IMO is to explicitly pass the DB name along with host and port name to mongo like this:
mongo hostname:27017/dbname mongoscript.js // replace 27017 with your port number
A better way to do this would be to define the DB at the beginning of your script:
mydb=db.getSiblingDB("yourdbname");
mydb.collection.findOne();
etc.
The latter is preferable as it allows you to interact with multiple DBs in the same script if you need to do so.
You can specify the database while starting the mongo client:
mongo somedb text.js
To get the output from the client to stdout just use the printjson function in your script:
printjson(db.somecollection.findOne());
Mongo needs to be invoked from a shell to get that mode, with Ansible you would have this:
- name: mongo using different databases
action: shell /usr/bin/mongo < text.js
Instead of this:
- name: mongo breaking
command: /usr/bin/mongo < text.js
This is what finally worked for me on Windows + Sublime Text 2 + MongoDB 2.6.5
{
"selector": "source.js",
"shell":true,
"cmd": ["mongo","<", "$file"],
"working_dir" : "C:\\MongoDB\\bin"
}

What mongodb command I should run to know server info?

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.