Linux: Make mongodb to use a different data folder (dbPath) - mongodb

Using CentOS 7.
Used yum install mongodb-org
If I change the dbPath in /etc/mongod.conf it is not working when I start the mongod with systemctl start mongod.service. However if I start like mongod --dbpath /new/folder it is working fine.
In the documentation (https://docs.mongodb.com/manual/reference/configuration-options/) it is given that
The Linux package init scripts do not expect storage.dbPath to change
from the defaults. If you use the Linux packages and change
storage.dbPath, you will have to use your own init scripts and disable
the built-in scripts.
How to change the default init scripts?
I have gone through
Changing MongoDB data store directory
Moving MongoDB's data folder?
how to set mongod --dbpath

You can try to remove your service, restart your computer, then create a new service with mongod option --dbpath : systemctl enable mongod.service. It should work !

Related

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.

MongoDB ignoring dbPath in config file

I'm setting up a fresh centOS 7 install following these instructions.
https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-red-hat/
Using systemctl start mongod I get a error saying /data/dbs does not exist (which it doesn't). If I run
sudo mongod -f /etc/mongod.conf
Everything seems to work. I don't really want to edit the init script if I can avoid it, but is there any way to fix this?
If you are installing using yum in CentOS7 by using the repository file described in Configure MongoDB v3.0 yum, the MongoDB instance stores its data files in /var/lib/mongo and its log files in /var/log/mongodb by default, and runs using the mongod user account.
the default config should contain a section similar to:
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
Make sure whether there's no modification made to the content of your /etc/mongod.conf.
So the solution has ended up just being a new service which calls mongod with the parameters I want. I've disabled the default mongod service
systemctl disable mongod
Added my service file to /etc/systemd/system and enabled it
systemctl enable my-mongod
It works well enough and shouldn't be broken by updates as they come down. The only thing that threw me for a bit is that the service had to be of type forking for it to work.

How to discover MongoDB's data directory? [duplicate]

I got an error about dbpath (/data/db/) does not exist, but /etc/mongodb.conf named it dbpath = /var/lib/mongodb.
So, which is the default dbpath for MongoDB?
The default dbpath for mongodb is /data/db.
There is no default config file, so you will either need to specify this when starting mongod with:
mongod --config /etc/mongodb.conf
.. or use a packaged install of MongoDB (such as for Redhat or Debian/Ubuntu) which will include a config file path in the service definition.
Note: to check the dbpath and command-line options for a running mongod, connect via the mongo shell and run:
db.serverCmdLineOpts()
In particular, if a custom dbpath is set it will be the value of:
db.serverCmdLineOpts().parsed.dbpath // MongoDB 2.4 and older
db.serverCmdLineOpts().parsed.storage.dbPath // MongoDB 2.6+
I have version 2.0.7 installed on Ubuntu and it defaulted to /var/lib/mongodb/ and that is also what was placed into my /etc/mongodb.conf file.
For a Windows machine start the mongod process by specifying the dbpath:
mongod --dbpath \mongodb\data
Reference: Manage mongod Processes
I depends on the version and the distro.
For example the default download pre-2.2 from the MongoDB site uses: /data/db but the Ubuntu install at one point used to use: var/lib/mongodb.
I think these have been standardised now so that 2.2+ will only use data/db whether it comes from direct download on the site or from the repos.
The dbPath in Mongo can be confusing. If you don't specify the dbPath at all (neither as command line parameter nor in mongod.conf file) then it defaults to
/data/db on Linux and macOS
\data\db on Windows (on current drive)
However, the default mongod.conf files which comes along the installation and which is used when you start mongod as a service (e.g. systemctl start mongod) uses these ones:
Platform
Package Manager
Default storage.dbPath
RHEL / CentOS and Amazon
yum
/var/lib/mongo
SUSE
zypper
/var/lib/mongo
Ubuntu and Debian
apt
/var/lib/mongodb
macOS
brew
/usr/local/var/mongodb
Windows
MSI
C:\Program Files\MongoDB\Server\{release}\data\
So, you must carefully check what you are using.
See Run-time Database Configuration
The Windows x64 installer shows the a path in the installer UI/wizard.
You can confirm which path it used later, by opening your mongod.cfg file. My mongod.cfg was located here C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg (change for your version of MongoDB!
When I opened my mongd.cfg I found this line, showing the default db path:
dbPath: C:\Program Files\MongoDB\Server\4.0\data
However, this caused an error when trying to run mongod, which was still expecting to find C:\data\db:
2019-05-05T09:32:36.084-0700 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory C:\data\db\ not found., terminating
You could pass mongod a --dbpath=... parameter. In my case:
mongod --dbpath="C:\Program Files\MongoDB\Server\4.0\data"

getting an error when firing up mongodb in terminal [duplicate]

I'm getting the following error when I try to run "mongod" in the terminal. I've tried uninstalling, reinstalling, and restarting the machine. Any suggestions on how to get it working would be amazing.
ERROR:
dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
Side note:
Node also stopped working on my machine around the same time that I got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
Any help would be much appreciated!
This should work to ensure that the directory is set up in the right place so that Mongo can find it:
sudo mkdir -p /data/db/
sudo chown `id -u` /data/db
You need to create the directory on root /data/db or set any other path with the following command :
mongod --dbpath /srv/mongodb/
See the example link
I solved the problem with :
sudo mongod --dbpath=/var/lib/mongodb and then mongo to access the mongodb Shell.
Change the user of the new data directory:
chown mongodb [rute_directory]
And try another time to start the mongo service
service mongod start
I solve the same problem with this.
Daemons (usually ending with d) are normally started as services. Starting the service (daemon) will allow mongodb to work as designed (without permission changes if integrates well with your distro). I start it using the service named mongodb instead of starting mongod directly--on distro with systemd enable on startup then run like:
sudo systemctl enable mongodb
sudo systemctl start mongodb
or, on distro with upstart (if you have /etc/init) or init (if you have /etc/init.d) ( https://www.tecmint.com/systemd-replaces-init-in-linux/ ) instead run:
sudo service mongodb enable
sudo service mongodb start
If you have a distro with rc ("run commands") such as Gentoo (settings in /etc/init.d) (https://forums.gentoo.org/viewtopic-t-854138-start-0.html) run:
rc-update add mongodb default
/etc/init.d/mongodb start
In a distro/version of FreeBSD which still has rc (check whether your version switched to systemd, otherwise see below):
add the following line to /etc/rc.conf:
mongod_enable="YES"
then:
sudo service mongod start
After starting the service, an unpriveleged user can use mongo, and each user will have separate data.
I also got the error that "The file /data/db doesn't exist" when I tried to save my file using the "mkdir -p /data/db" command(using both with and without sudo command). But later on one site, a person named Emil answered that the path "/data/db" no longer works on Mac, so use "~/data/db" instead
i.e., use the command
mkdir -p ~/data/db
instead of previous command.
Moreover, use
mongod --dbpath ~/data/db
to run mongod
It worked for me, hope it work for others too facing the same problem

Mongodb replications sets...tying ot get tutorial to work

I am trying to do the Replica Sets tutorial for mongodb. I am using ubuntu on 64 bit and installed using sudo apt-get install mongodb-10gen.
Tutorial is at http://www.mongodb.org/display/DOCS/Replica+Sets+-+Basics
I stop the service using
/etc/init.d/mongodb stop
I then try:
/etc/init.d/mongodb start --rest --replSet myset
When I go to http://localhost:28017/_replSet I get this message.
REST is not enabled. use --rest to turn on. check that port 28017 is
secured for the network too.
So...how do I resolve? How do I open a port on ubuntu is that is the problem? Did I do things correct?
Thanks
You need to edit your /etc/mongodb.conf file , setting rest = true and replSet = myset, and restart mongo.
Init.d script don't accept parameters. Or: /etc/init.d/mongodb is not the same as /usr/bin/mongod - The parameters you're passing are not making it to the mongo executable.