How do I force save the dbpath parameter? - mongodb

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

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.

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.

Create MongoDB database and set its path to a specific folder

I'm using ubuntu 16.04! Is it possible to change the data directory for a specific database in mongo? I'm trying to setup this recommendation engine and I was asked to create a new MongoDB database and set its path to folder.
Pre-version 3.0.0, all the databases that one mongod hosts have to be in the same dbpath folder which can be set via command arguments using the --dbpath flag or the config file.
You could however run multiple mongods and host them individually? either on different ip/ports.
Version 3.0.0 you can use the --directoryperdb flag within the command arguments or config to set multiple directories per database (See https://docs.mongodb.com/manual/reference/program/mongod/#cmdoption--directoryperdb)
Uses a separate directory to store data for each database. The directories are under the --dbpath directory, and each subdirectory name corresponds to the database name.
When you start mongod.exe you can set the database path with the
mongod.exe --dbpath "path to your folder"
setting.
yes it's possible, run mongod process with --dbpath option

Mongodb not starting

I installed mongodb a few days ago on my ubuntu machine and I was using it without any problems. Today I had to restart my PC. After that, mongo simply wont start. It says:
Wed Sep 12 21:41:21 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
It was working fine just a few hours ago and now it's all screwed up on reboot. I had a lot of important data stored there and I really hope it's not all lost! I need to find that data and run mongodb on that data again. I use pymongo to interract with mongodb.
I just saw the config file and it's storing the data in /var/lib/mongod as the dpath. Now how do I start mongodb specifying this dpath?
You can start mongodb with the following switch.
mongod --dbpath /path/to/mongodb/data
Or edit the config file.
/etc/mongod.conf
Change dbpath to your data directory.
dbpath = /path/to/mongodb/data
There's a lot more you can add and change in the config file go here for more details.
Use this command to create your directory tree.
mkdir -p data/{db}
Use the following command to start mongod:
sudo service mongodb start
This will pass your system configuration file as an argument to mongod, which (as you saw) will cause it to use the correct path.
you need a path to store your database. by default it is /data/db/. if it does not exist you have to create it first
One of the reason for this error can be the fact that you are starting the mongodb with user as yourself. If you execute the command with sudo, the mongodb should start without error:
sudo mongod --dbpath /path/to/mongodb/data
One can find the path to mongodb data in file such as /etc/mongod.conf

Why is the database of a Meteor app that has been run once (and never loaded) taking up nearly 3GB?

UPDATE: this was fixed after Meteor v0.4 (2012). For historical purposes:
Excerpt from du:
2890768 ./Code/Meteor/QuarterTo/.meteor/local/db/journal
2890772 ./Code/Meteor/QuarterTo/.meteor/local/db
2890776 ./Code/Meteor/QuarterTo/.meteor/local
2890788 ./Code/Meteor/QuarterTo/.meteor
2890804 ./Code/Meteor/QuarterTo
I merely ask because it was in my Dropbox and pushed me over my limit.
When meteor run is executed, it starts mongodb with default mongo settings, so it creates (massive) prealloc files in .meteor/local/db/journal.
There is no obvious way to disable this behavior. What I have done as a workaround is change the file app/lib/mongo_runner.js and add a --nojournal parameter that gets passed to mongodb at startup.
I created an issue for this: https://github.com/meteor/meteor/issues/15
Maybe you can use smallfiles=true parameter for mongoDB? It will create smallest prealloc files
You can turn off preallocation by passing the --noprealloc arg to mongod. The downside is that there will be pauses each time a new storage file needs to be allocated. Depending on the filesystem you are using (e.g., ext3 vs. ext4), this could result in noticeable latency for a user.
The commands that work for me are:
stop mongodb instance if it is running
sudo service mongod stop
create new mongodb instance without requiring 3+GB preallocated space and use smallfiles.
mongod --noprealloc --smallfiles
If you are getting “ERROR: dbpath (/data/db) does not exist.” when running 2,
then run these commands before 2.
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db