Mongodb needs me to fork the path to the mongod.conf file every time I reboot my Ubuntu server. Why?

For some reason, I need to fork the mongod.conf every time I reboot my server by running the command:

    mongod --config /etc/mongod.conf

I can tell since, when I reboot my server and run mongod I get the following message:

    2020-09-03T14:49:33.713+0000 I  STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file., terminating

Is there a way to make mongodb to “read” the config file without needing me to fork it every time I reboot my server?

After reboot are you running just mongod command?
If yes it tries to start mongod on default port 27017 and default dirpath /data/db
Since the dir path is not existing it is failing
You need to create the directory
When you run manually mongod --config /etc/mongod.conf it may be using different dirpath and working fine
Please check contents of your configfile for dbpath to confirm this

Hi,

Yes, I use a custom path for the data (dbpath) being /var/www/site/data/db. I would like to make this the default though so every-time I reboot my system, mongodb knows this path without requiring me to run mongod --config /etc/mongod.conf.

This is my config file:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /etc/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/www/site.com/data/db
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

Why mongodb doesn’t recognize my config file?

Hi @George_K,

The storage.dbPath option has a hard-coded default of /data/db on Linux. A default path for a configuration file is not baked into the mongod binary, since the convention for config file locations can vary by Linux distro and administrative preferences.

However, the recommended way to manage starting and stopping MongoDB with consistent configuration & environment options is using a service definition which includes a config file path so you only have to start and stop the service. If you installed MongoDB using the official packages, you should already have a suitable service definition which includes /etc/mongod.conf as the configuration file path.

See Install MongoDB Community Edition on Ubuntu for examples of service management using systemd or System V Init.

Regards,
Stennie

1 Like

Hi @Stennie_X

Thank you. I have just created a /data/db root folder but mongodb still doesn’t even recognize it when I restart the server. I still cannot figure out what’s wrong with mongodb

I have followed the exact link you have shared when I went to install mongodb. However, now when y type mongo, I get the following error:

MongoDB shell version v4.2.9
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-09-07T12:53:59.326+0000 E  QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-09-07T12:53:59.331+0000 F  -        [main] exception: connect failed
2020-09-07T12:53:59.331+0000 E  -        [main] exiting with code 1

I am sure there is something wrong with my mongodb configuration. Is there a way to fix it?

Is your mongod up and running after you started as service

sudo systemctl status mongod

Please check mongod.log for additional details

This looks like the output of mongo shell rather than the output of mongod server.

Hi,

Yes mongodb starts as soon as the server boots. Just checked and it says that it fails to start

Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)

Active: **failed** (Result: exit-code) since Mon 2020-09-07 12:53:50 UTC; 2h 1min ago

Docs: https://docs.mongodb.org/manual

 Main PID: 1273 (code=exited, status=2)

Sep 07 12:53:50 me systemd[1]: Started MongoDB Database Server.

Sep 07 12:53:50 me mongod[1273]: Error opening config file: Permission denied

Sep 07 12:53:50 me mongod[1273]: try '/usr/bin/mongod --help' for more information

Sep 07 12:53:50 me systemd[1]: **mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT**

Sep 07 12:53:50 me systemd[1]: **mongod.service: Failed with result 'exit-code'.**

Sep 07 12:53:54 me systemd[1]: **/lib/systemd/system/mongod.service:10: PIDFile= references a path below legacy directory /var/run/, updating /var/run/mongodb/mongod.pid → /run/mongodb/mongod.pid; please update the unit file accordingly.** 

This is the mongo.log output log:

020-09-03T13:53:10.097+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-09-03T13:53:10.195+0000 W  ASIO     [main] No TransportLayer configured during NetworkInterface startup
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1989 port=27017 dbpath=/var/lib/mongodb 64-bit host=me
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] db version v4.2.9
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] git version: 06402114114ffc5146fd4b55402c96f1dc9ec4b5
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1c  28 May 2019
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] modules: none
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] build environment:
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten]     distarch: x86_64
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-09-03T13:53:10.196+0000 I  CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLo>
2020-09-03T13:53:10.196+0000 E  NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
2020-09-03T13:53:10.196+0000 F  -        [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 684
2020-09-03T13:53:10.196+0000 F  -        [initandlisten]

What can I do to fix this mess?

Hi,

I opened the mongod.log file which is located in the /etc path, so /etc/mongod.log. Is there another log file you are referring to?

Your both snapshots have different timestamps?
One is from Sep7th(latest) and other is of Sep 3rd
Is the mongod.log from the run just after reboot?
or you must have pasted older entries
Investigate this error
Sep 07 12:53:50 me mongod[1273]: Error opening config file: Permission denied
ls -lrt /etc/mongod.conf
Also review contents of your config file for dbpath,logpath