Mongodb – Can’t change MongoDB default DB path

mongodbnosql

I know there are multiple questions on the same issue and I followed them but not sure what is happening here. First of all I am trying to change the default db path to a a separate mounted disk. And I changed the group and permissions and I updated the SELinux Contexts. But still I can see that the db is not starting saying permission denied in the log. Here is my log report

2019-03-03T18:53:25.514-0600 I CONTROL  [main] ***** SERVER RESTARTED *****
2019-03-03T18:53:25.517-0600 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] MongoDB starting : pid=34998 port=27017 dbpath=/run/media/thomson/data2/mongo 64-bit host=localhost.localdomain
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] db version v4.0.6
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] git version: caa42a1f75a56c7643d0b68d3880444375ec42e3
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] allocator: tcmalloc
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] modules: none
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] build environment:
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten]     distmod: rhel70
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten]     distarch: x86_64
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten]     target_arch: x86_64
2019-03-03T18:53:25.528-0600 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid", timeZoneInfo: "/usr/share/zoneinfo" }, storage: { dbPath: "/run/media/thomson/data2/mongo", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2019-03-03T18:53:25.529-0600 I STORAGE  [initandlisten] exception in initAndListen: Location28596: Unable to determine status of lock file in the data directory /run/media/thomson/data2/mongo: boost::filesystem::status: Permission denied: "/run/media/thomson/data2/mongo/mongod.lock", terminating
2019-03-03T18:53:25.529-0600 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2019-03-03T18:53:25.529-0600 I NETWORK  [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2019-03-03T18:53:25.529-0600 I CONTROL  [initandlisten] now exiting
2019-03-03T18:53:25.529-0600 I CONTROL  [initandlisten] shutting down with code:100

But it works when I put the default path in mongo.conf file
Here is the default directory details (/var/lib/mongo)

[root@localhost lib]# stat mongo/
File: ‘mongo/’
Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 70738359    Links: 4
Access: (0755/drwxr-xr-x)  Uid: (  979/  mongod)   Gid: (  973/  mongod)
Context: system_u:object_r:mongod_var_lib_t:s0
Access: 2019-03-03 18:23:09.971149485 -0600
Modify: 2019-03-03 17:49:21.178099641 -0600
Change: 2019-03-03 17:49:21.178099641 -0600
Birth: -

Here is the details of my new directory (/run/media/thomson/data2/mongo)

[root@localhost data2]# stat mongo/
File: ‘mongo/’
Size: 4096          Blocks: 8          IO Block: 4096   directory
Device: 820h/2080d  Inode: 4980737     Links: 2
Access: (0777/drwxrwxrwx)  Uid: (  979/  mongod)   Gid: (  973/  mongod)
Context: system_u:object_r:mongod_var_lib_t:s0
Access: 2019-03-03 18:52:09.764192229 -0600
Modify: 2019-03-03 18:32:17.212162930 -0600
Change: 2019-03-03 18:53:18.814193926 -0600
Birth: -

I am not sure what else need to be done. A help would be greatly appreciated. Thanks.

Best Answer

Run the following command to enable the user to write to /run/media/thomson/data2/mongo

sudo chown -R $USER:$USER /run/media/thomson/data2/mongo