Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Change dbPath on Ubuntu

Hello,

I want to customise (change) the dbPath on ubuntu 20.04.

The configuration file:

/etc/mongod.conf

specifies the data directory/dbPath:

/var/lib/mongodb

The docs here: https://docs.mongodb.com/manual/reference/configuration-options/#storage.dbPath

note that on linux one can’t simply change the dbPath in the config file:

" 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."

Unfortunately, I haven’t found any information on those init script(s), i.e. where I can find them, and what in them might need editing in order for the change of dbPath in the config. file to be allowed to work.

Because I’m using ubuntu the init system is systemctl, see here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Aside: why do I want to change the dbPath from its default? Because my laptop is dual boot and I have mongodb on both win10 and unbuntu, but want to be able to access the same database files–i.e. access the win10 directory/data storage from within the mongodb on ubuntu. So I think I want to use something like:

/media/[username]/[win10 partition name]/data/db

which corresponds to:

C:\data\db

But as the docs say, the init script(s) also need changing for that to work.

Thanks for your help.

Changing the init script, if running systemd, is not that complicated.

The command

sudo systemctl status mongod

will tell you the location of the script. On my system, it is /usr/lib/systemd/system/mongod.service. You will see in this text file some ExecStartPre= statement. You just have to match those directories and files to the one specified in your mongod.conf. You will see that these are probably only needed at the first start and it is too make sure that the directories exist and are writable by mongod user.

But that’s the theory. On a production server I do not mess with this. On a dev machine I start mongod manually with the config file I pleased. Sometimes I have different config files that point to different directories depending of the project. Just make sure directories exist and are writable by the user that starts mongod.

1 Like

On top of what @steevej mentioned, you will also need to set the correct permissions on the directory. MongoDB in linux will need your new dbPath location to be writable by the mongod user. When you install MongoDB in Linux, the package will create the mongod user and group and then the /data/db path will be owned by this user/group. On a dual boot system, I don’t know what will happen if you try to change the ownership of a path on a Windows mount. You might need to open the path up to be world read/writable (chmod 777 /media/[username]/[win10 partition name]/data/db).

2 Likes

Thanks. Here’s the result when I tried that:

user@machine:~$ sudo systemctl status mongod
[sudo] password for user: 
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
     Active: active (running) since Wed 2020-06-03 18:28:28 NZST; 5h 5min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 902 (mongod)
     Memory: 209.8M
     CGroup: /system.slice/mongod.service
             └─902 /usr/bin/mongod --config /etc/mongod.conf

Jun 03 18:28:28 machine systemd[1]: Started MongoDB Database Server.

When I look inside the /lib/systemd/system/mongod.service file I see:

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongodb
Group=mongodb
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

It may just be my lack of knowledge about linux but I don’t see the sort of statement your describe. There is the line:

ExecStart=/usr/bin/mongod --config /etc/mongod.conf

but that’s the same config file I earlier wrote of wanting to be able to change–but when I do so, mongod will not start.

Is this line important?:

EnvironmentFile=-/etc/default/mongod

The thing is, I don’t know what the leading - is doing there. There’s no file at:

/etc/default/mongod

The EnvironmentFile bit states that the service should look in the provided file for environment variables. The - in front of the file means to not error out if the file doesn’t exist. You can read more about EnvironmentFile in the systemd man page.

The first place that I would start is changing the the path in the MongoDB config file. You can find the location of that file by looking at the ExecStart line:

In your case, the file is located at /etc/mongod.conf. Edit that file and change the strorage.dbPath location to the path you want. Save and exit from the file. After doing that run sudo systemctl restart mongod, check the status of the service with systemctl status mongod and verify if it’s running. If it is you’re all set. If it’s not you need to check the MongoDB log file (default should be /var/log/mongodb/mongodb.log I believe) to see why the service failed.

2 Likes

Good point by both you (Doug) and Steeve, earlier. These are the current permissions for that dir:

user@machine:~$ ls -l /media/[user name]/[win10 partition name]/data
total 8
drwxrwxrwx 1 owner group 8192 Jun 3 15:43 db

So I think that shows access permissions are not what’s preventing mongod working if I change the config file’s dbPath to that target dir.?

Would seeing the status help? After changing the dbPath in the config file to my target dir when mongod is stopped, I then:

user@machine~$ sudo service mongod start
user@machine:~$ sudo service mongod status
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor pres>
     Active: failed (Result: exit-code) since Thu 2020-06-04 01:37:16 NZST; 3>
       Docs: https://docs.mongodb.org/manual
    Process: 33255 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=>
   Main PID: 33255 (code=exited, status=100)

Jun 04 01:37:16 machine systemd[1]: Started MongoDB Database Server.
Jun 04 01:37:16 machine systemd[1]: mongod.service: Main process exited, c>
Jun 04 01:37:16 machine systemd[1]: mongod.service: Failed with result 'ex>

NB, '‘user’ and ‘machine’ are my placeholders.

The next step is to look at the the log file to see why it the mongod process failed. The service status just states that the process failed, but the log file will give more information about why.

1 Like

OK, thanks. Here’s nearly the last bit from that log file:

2020-06-04T01:37:16.571+1200 I CONTROL [initandlisten] options: { config: “/etc/mongod.conf”, net: { bindIp: “127.0.0.1”, port: 27017 }, processManagement: { timeZoneInfo: “/usr/share/zoneinfo” }, storage: { dbPath: “/media/[user name]/[win10 partition name]/data/db”, journal: { enabled: true } }, systemLog: { destination: “file”, logAppend: true, path: “/var/log/mongodb/mongod.log” } }
2020-06-04T01:37:16.572+1200 I STORAGE [initandlisten] exception in initAndListen: Location28596: Unable to determine status of lock file in the data directory /media/[user name]/[win10 partition name]/data/db: boost::filesystem::status: Permission denied: “/media/[user name]/[win10 partition name]/data/db/mongod.lock”, terminating

I don’t think it’s simply permissions on that file itself, though, as:

user@machine:/media/[user name]/[win10 partition name]/data/db$ ls -l mongod.lock
-rwxrwxrwx 2 owner group 0 Jun 3 15:43 mongod.lock

Is the file ownership itself important? The owner is not mongodb but rather my user name, while mongodb is the owner for the corresponding file on my linux partition’s default dbPath, i.e.:
/var/lib/mongodb/mongod.lock

user@machine:/var/lib/mongodb$ ls -l mongod.lock
-rw------- 1 mongodb mongodb 0 Jun 4 01:35 mongod.lock

I tried changing the owner & group to mongodb but after that mongod still failed to start, with the log:

2020-06-04T02:16:26.635+1200 I CONTROL [initandlisten] options: { config: “/etc/mongod.conf”, net: { bindIp: “127.0.0.1”, port: 27017 }, processManagement: { timeZoneInfo: “/usr/share/zoneinfo” }, storage: { dbPath: “/media/[user name]/[win10 partition name]/data/db”, journal: { enabled: true } }, systemLog: { destination: “file”, logAppend: true, path: “/var/log/mongodb/mongod.log” } }
2020-06-04T02:16:26.637+1200 I STORAGE [initandlisten] exception in initAndListen: Location28596: Unable to determine status of lock file in the data directory /media/[user name]/[win10 partition name]/data/db: boost::filesystem::status: Permission denied: “/media/[user name]/[win10 partition name]/data/db/mongod.lock”, terminating

edit: correction: my attempt to change the owner & group of the mongod.lock file in the target win10 partition actually failed, but I’m still not sure whether file ownership is important given everyone has rw access?

What are the permissions on the /media/[user]/[win10 partition]/data/db folder? My guess is they are wide open as well, but worth checking.

MongoDB is trying to access the lock file but it can’t because the permissions of the file are rw by the owner only. You could try removing the mongod.lock file to see if you can start the service in Linux. I am not sure how that would affect things when you go back to your Windows version however.

I get what you’re trying to do having MongoDB available no matter whether you boot into Windows or Linux, but I’m not sure that’s going to work well. If you have Windows 10 and enabled WSL this might work a little more smoothly as you’d have the same permissions and owners between the two systems. Trying to get the permissions on the files correctly and in a way that doesn’t affect the other OS might be more hassle than it’s worth.

A couple of options that might be a better choice:

  • If your database is going to stay smaller than 512MB you could get a free tier M0 Atlas cluster.
  • You could run docker containers on both Windows and Ubuntu and use a persistent volume that is stored in a path that is accessible to both operating systems.

Unfortunately I don’t have a dual boot Windows machine so I’m not able to test things out to see what would be needed to make this work on your set up.

2 Likes

That would be the way to go.

The free tier is very nice for that sort of things because it is accessible everywhere. Since I switch from a laptop on the go and a desktop at home having my data no matter what.

1 Like