Are the changes done by db command in MongoDB permanent and how? - mongodb

Are the changes done using the shell in mongodb permanent ?
And how MongoDb detects the config file?
I am stuck in setting Slow query logs in MongoDb.
We have a command db.setProfilingLevel() to set profiling level.
But how can we set profiling for all databases and how to set it via Config file?
How mongodb remember this change after restart?

Yes, Changes made to data in MongoDB shell are permanent but for configuration parameters, they will last only till next restart of instance if they are not specified in config file.
MongoDB runs with default values if config file is not specified.
You can specify config file as,
mongod --config /etc/mongod.conf
or
mongod --f /etc/mongod.conf
and for profiling you can provide parameters from command line or in config file like,
From command line,
mongod --slowms <value in milliseconds for slow queries>
or
mongod --profile <0/1/2>
In config file,
profile = <0/1/2>
slowms = <value>
logpath = <path>

Related

why does mongodb not care what my config file is?

I can tell mongod does not respect its own default config file because when I type 'mongod' in the terminal, i see
MongoDB starting : ... dbpath=/data/db ...
however in my /etc/mongodb.conf i have
storage:
dbPath: /home/ubuntu/data/db
So it appears mongod does not care about what is defined in its own default config file?
(i know i can add --config /etc/mongodb.conf but I don't see why if this config file is by definition the default config file)
Second question, if mongod does not use /etc/mongodb.conf - is there another config somewhere telling it to put the database path do /data/db (so i can change it) - or is /data/db hard-coded in the int main() of the mongod.exe
Third question: if so does this basically mean you have to always specify the path of the config file when starting mongo?
Fourth question: If it is the case that i must manually define the path to the mongodb config file when starting mongo, how would it be possible to both start mongo as a service like:
service start mongodb
and also specify the config file to be used for the service?
ps. i already know about mongod --fork .... but i'm trying to avoid big long inputs and just have a simple server start/stop/status function with one config file that is respected.
thanks
i'm trying to avoid big long inputs and just have a simple server start/stop/status function with one config file that is respected.
If you run MongoDB as an service in Ubuntu, the config path of /etc/mongod.conf is included in the service definition. Starting and stopping MongoDB as a service is the recommended way to run with consistent settings using a standard config location. You do not need to include any extra parameters when starting or stopping the service; any parameters should be set via the config file.
in my /etc/mongodb.conf i have
Note that the expected location is usually /etc/mongod.conf (named to match the corresponding server daemon). An /etc/mongod.conf with defaults for your O/S should be created as part of the installation (assuming you are using packages provided by MongoDB).
If you installed packages from an alternative source such as the Ubuntu Universe repository, it is possible that your service definition is using /etc/mongodb.conf and may have different defaults from the MongoDB documentation.
So it appears mongod does not care about what is defined in its own default config file?
If you start mongod from the command line, default values for mongod options are used. As at MongoDB 4.0 there is no default config file path baked into the server binary, but there is a default dbPath (/data/db on Linux).
There's a relevant feature suggestion you can watch/upvote in the MongoDB Jira issue tracker: SERVER-36150: Improved default config file handling.

How to set mongo db path permanently

Is there a way to over-rite the mongodb default db path. Even after editing the storage path in mongod.conf to the custom directory path. Still it looks for /data/db, and not the custom path.
Since every time mongod path needs to be specified for the custom path.
mongod --dbpath /Users/customData
Is there a permanent way to deal this.
You can try to run it as a service, so that you don't need to run this command everytime you want to use it, and it runs it in the path you set it to
Here's how:
the link
from official mongodb website: the link
According to documentation of MongoDB
To run a mongod process as a daemon (i.e. fork), and write its output
to a log file, use the --fork and --logpath options. You must create
the log directory; however, mongod will create the log file if it does
not exist.
The following command starts mongod as a daemon and records log output
to /var/log/mongodb.log.
mongod --fork --logpath /var/log/mongodb.log

Need to launch mongod using launchctl

I have installed MongoDB using manual option.
When I fire mongod it will take parameters from somewhere else specially for dbPath.
I want to have my own dbPath without specifying it with --dbPath each time.
So I found option to provide mongod.conf file and call mongod --config option but still I need to pass path of .conf file.
My ultimate goal is to just launch mongod as service and it will read config file from path I configured. For this I tried LaunchAgent and LaunchDaemon with no luck. What I thought is that if I can configure LaunchAgent or Daemon then I will provide path to mongod of bin directory of MongoDB folder and provide --config and path for .conf file as argument and then I need to just launch mongod using launchctl with name mongod and it will start mongod --config <.conf file path>.
But I am not able to do so. I read nearly 15-16 posts on LaunchAgent and Daemon but did not get solution my problem. I tried to do so with making mongod.plist file at various places like /Library/LaunchDaemon, /Library/LaunchAgent and ~/Library/LaunchDaemon but I always get error "Path had bad ownership/permissions". I tried chown and chgrp but no luck.
I hope you got my point. I really need complete solution to achieve my goal.
Do you use linux or windows?
This is the default location of mongodb.conf in linux:
/etc/mongodb.conf
You can simply edit it with your parameters, and start the service using:
sudo service mongodb restart
If you are using windows, it depends on the version of MongoDB you are using, but check in:
C:\Program Files\MongoDB\Server\3.4\...
For the same file.

How to specify the dbpath in mango configuration file

I have a Grails application with MongoDB as database, each time that I run my app I have to go to Command Prompt and define dbPath otherwise Test connection will fail. In MongoDB documentation I read that dpPath can be specified in configuration file, but I can not figure it out how I am supposed to do it. Please advise.
create mongo.config file..
dbpath=D:\mongodb\your path
logpath=D:\mongodb\log\mongo.log
bind_ip=127.0.0.1
port=3979
then start the mongod as
mongod --config D:\mongodb\mongo.config

How do I force save the dbpath parameter?

I'm just starting out with developing with Mongodb locally and following the tutorials.
I want to use 1 path for all my development.
In the Mongodb documentation it says:
Create a data directory
By default MongoDB will store data in
/data/db, but it won't automatically
create that directory. To create it,
do:
$ mkdir -p /data/db
You can also tell
MongoDB to use a different data
directory, with the --dbpath option.
I want to use one path for all my dbpaths so that I know where they all are and not get confused.
The path I want to use is:
sudo mongod --config=/Applications/XAMPP/xamppfiles/var/mongodb/mongodb.conf
The mongodb.conf file I have is set up as thus:
# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /Applications/XAMPP/xamppfiles/var/mongodb
# Only accept local connections
bind_ip = 127.0.0.1
However, I have noticed that I have to keep typing this config path in every time I want to run Mongo.
How do I make it so that Mongodb save the dbpath, or the path to the config without me having to type it out every time?
Thanks.
You can create a startup script, like a bash/cshc script. You must specify those options when starting up.
Make a bash script that runs the commands you want.
Make a file called startMongo.sh
#! /bin/bash
mongod --config=/Applications/XAMPP/xamppfiles/var/mongodb/mongodb.conf
Run command
sudo startMongo.sh