Create MongoDB database and set its path to a specific folder - mongodb

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

Related

IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB in ubuntu 20.04

I have just installed mongodb on my remote ubuntu server for using it with an angular and nodejs project. I created a user using the db.createUser command along with password and roles in the mongo shell.
Then when i try to start the mongodb instance with access control using the command:
mongod --auth --port 27017 --dbpath /var/lib/mongodb
its displaying the error:
IllegalOperation: Attempted to create a lock file on a read-only directory MongoDB
First of all, have you checked the permissions of the folder /var/lib/mongodb (ls -l /var/lib)?
The folder and its contents should be assigned to the mongod user. You could also check the ACL permissions (getfacl), if that's installed in your system.
If permissions seem correct, please continue reading to know how I solved the same error in a different environment.
This happened to me in CentOS 8 after a wrong reinstallation of MongoDB and the data folder /var/lib/mongo had to be created manually. In my case, the problem was that the security context of SELinux (https://www.redhat.com/en/topics/linux/what-is-selinux) had to be updated to give mongod access the folder.
The command below solved my problem (note the path to the data folder in my configuration is slightly different, just update it for your case):
chcon -Rv --type=mongod_var_lib_t /var/lib/mongo
It basically tells SELinux that the context used by MongoDB to access the files should be associated with the target folder.
You can have more information about chcon here: https://www.man7.org/linux/man-pages/man1/chcon.1.html

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

Unable to start mongodb

I'm trying to install mongodb on my window 7 machine. When trying to start it by using the mongod command I get this error. dbpath (\data\db) does not exist. I followed the steps ath the tutorial. All the folders exist C:\mongodb\log, C:\mongodb\data and C:\mongodb\data\db .
The default path is c:\data\db. You are trying to use c:\mongodb\data\db.
This means you have to use the option --dbpath c:\mongodb\data\db for mongod command.
By default, when we start the mongodb, it looks for the folder /data/db (on Mac/Linux) or C:\data\db (on windows)...however, during the installation it doesn't create this folder, so when when we run mongodb for the first time, it fails with the error that it can't find C:\data\db folder. The solution is to manually create C:\data\db folder and then start mongodb. This error can be totally avoided if the mongodb installer can just give an option to choose this folder path and then create it as part of mongodb installation itself.
In windows you might not have permission for your user. And mongo installer could not create it because of lack of Administrative permission for your user.
So in C:\Program Files\MongoDB:
Create a folder named data
Create a folder named db inside the folder data
Now right click on the data folder and choose properties
Click security tab and select your user there
Click the Full control select box
Click ok, ok, ok ...
important! if you don't have the path "C:\Program Files\MongoDB\Server\3.4\bin" set in environment variable, please set it.
Now go to shell and type : mongod --dbpath "C:\Program Files\MongoDB\data\db"
That's it :)
Try to use 2 command Prompts (CMD) at the same time.
First CMD, use the command:
mongod (or mongod --dbpath C:\mongodb\data)
Second CMD, use the command:
mongo
The second will connect in the database. Do not close the first.

Is it possible to install mongodb without root privilages?

How can I install mongodb on a Linux server without root privileges? is this possible? What would the steps be? Is there a way to configure mongo to not use /data/db?
You may download the binaries from MongoDB website.
There are a lot of options for the command mongod to change the default behavior, one of these is:
--dbpath arg directory for datafiles
Based on comments:
But it is preferable to specify all configurations in a configuration file and just invoke mongod with:
--config filename

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