MongoDB ignoring dbPath in config file - mongodb

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.

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

mongodb on Ubuntu 18.04 Bionic systemctl handling of mongod.service / mongodb.service

I had some issues installing mongod.service on a digital ocean droplet with systemctl. It is set up now except that it complains there is something else taking its port, although nothing actually is. The culprit, I suppose is the following: there is a failed service for mongodb (rather than mongod.service) whose error is "file not found". Do I need to set up or remove the missing mongodb.service? I see there is a /lib/systemd/system/mongod.service but there's not a mongodb.service in that directory.
Here is some of the config in /etc/mongod.conf
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
I also set sudo chown -R mongodb:mongodb /data/db which fixed errors then but now I'm getting /data/db/WiredTiger.turtle: handle-open: open: Permission denied
...Yay mongod is running after sudo chown -R mongodb:mongodb /data/db/WiredTiger.turtle
I suppose that file was generated and missed getting the permissions.
I just read in the documentation: 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. I believe I had originally changed the dbPath...
check if mongodb is running
sudo service mongodb status
if it says inactive, activate using the below command
sudo service mongodb start
I am adding the detail steps to check the MongoDB running on the localhost or not and how to start it.
To check MongoDB running or not type below command on Linux terminal
ps -Aef | grep mongod
After this command, you will properly see the details of mongod running instance with details of process Id and config file loaded by MongoDB.
If not running then check the status as below
sudo service mongod status
Provide the permission to /data/db or change the location and provide the monogdb ownership of that folder as sudo chown -R mongodb:mongodb /data/db Once done then do restart the MongoDB as below
sudo service mongod start
Now check again the status, its working or not. Still facing issue then visit the official MongoDB documentation with proper install guideline Install MongoDB Community Edition on Ubuntu

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

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 !

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 not using /etc/mongodb.conf after I changed dbpath

Ever since I changed the dbpath in /etc/mongodb.conf, MongoDB has not been starting automatically, nor using the new dbpath. Prior to the change, MongoDB would be running when the computer started and I was able to simply run the command mongo to get into the console or start my Ruby on Rails server with no issues.
After I made the modification (in order to switch to a new drive with more space), the only way I can get everything to work is by manually running the command mongod --config /etc/mongodb.conf. If I don't run that, it doesn't seem like the service is running and running without the --config option give me the following error: ERROR: dbpath (/data/db/) does not exist. even though the config file says nothing about data/db.
Some other notes:
In addition to changing /etc/mongodb.conf, I moved all files out of /var/lib/mongodb and into /home/nick/appdev/mongodb.
I changed the owner and group from root to nick. Tried changing it back, but it didn't seem to fix anything.
I'm running Ubuntu 12.10 Beta 1 and Mongo 2.2.0 with Ruby on Rails 3.2.8
A late follow up on the above question...
I had a similar issue after moving the db to an ebs on ec2.
It turns out that just running mongod still directs the dbpath to /data/db/ (which exists).
The /etc/mongodb.conf is completely ignored unless specifically directed to.
I manage to work around this by using the directive --config or just the --dbpath(both work)
But was left wondering where does mongod takes it defaults from...?!
I was unable to locate and override these defaults anywhere.
Anyone ?
Note:
I am really annoyed by this behaviour of mongod...This is just bad design,and bad documentation.
It turns out that I needed to set the owner and group to mongodb. When I transferred the files to the new directory, I had set the owner and group to my user account nick and also tried root, neither of which worked.
To do so, here are the following commands:
sudo chown mongodb /home/nick/appdev/mongodb -R
sudo chgrp mongodb /home/nick/appdev/mongodb -R
To confirm that it worked, you can check the file permissions with:
ls -l /home/nick/appdev/mongodb
After checking all permission in the data, journal and log folders as suggested, my problem was solved by giving permission to a lock file in the /tmp folder
sudo chown mongod:mongod mongodb-27017.sock
I was running it as a AWS Amazon Linux instance. I figured that out by executing as the mongod user as below, and then, researching the error code. It might be useful for other troubleshooting.
sudo -S -u mongod mongod -f /etc/mongod.conf
MongoDB 1.6 is very old and the latest production version is 2.2, which contains a large amount of bug fixes and enhancements since 1.6.
Am I correct that you haven't installed 1.6 via a package manager such as yum or aptitude? I don't believe there are packages for 1.6 at present afaik. Therefore, mongod is behaving correctly as you have not started MongoDB with a control script.
Please see this link on configuration file options.