It's a bit of a hack but yes, this can be done. There is an undocumented step needed to actually start the service and (weirdly enough) unless you want to futz around with upstart job conversion manually, you need to install the Ubuntu mongodb
package first, then remove it so that you have a usable service. A straight install of the MongoDB packages will not give you a working service on a fresh 16.04.
Update - July 2016: The official fix for this (adding support for 16.04 to the packages) has been completed in the 3.3 dev branch as part of SERVER-23043 - and has been released as a backport in the 3.2 series as part of 3.2.7. Hence, if you are using 3.2.7+ you do not need to do this and the official MongoDB package install should just work.
In any case, after doing the add/remove, follow the usual instructions for 14.04, summarized here:
# install the Ubuntu package (to get the service set up correctly)
sudo apt-get install mongodb
# Now remove it and continue with the MongoDB instructions
sudo apt-get remove mongodb
sudo apt-get autoremove
# import key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
# add trusty repos
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# update apt
sudo apt-get update
# install the MongoDB package
sudo apt-get install -y mongodb-org
The install succeeds, and thanks to the Ubuntu package we have all the pieces needed for a service, but attempting to start the service will initially fail:
$ sudo service mongodb start
Failed to start mongodb.service: Unit mongodb.service is masked.
Fixing this is not too hard:
$ sudo systemctl unmask mongodb
Removed symlink /etc/systemd/system/mongodb.service.
Now we can enable/start the service, get its status etc.
$ sudo service mongodb enable
$ sudo service mongodb start
$ sudo service mongodb status
● mongodb.service - LSB: An object/document-oriented database
Loaded: loaded (/etc/init.d/mongodb; bad; vendor preset: enabled)
Active: active (running) since Thu 2016-04-14 16:40:35 IST; 7s ago
Docs: man:systemd-sysv-generator(8)
Process: 2849 ExecStart=/etc/init.d/mongodb start (code=exited, status=0/SUCCESS)
Main PID: 1593 (code=exited, status=0/SUCCESS)
Tasks: 13 (limit: 512)
Memory: 51.9M
CPU: 100ms
CGroup: /system.slice/mongodb.service
└─2861 /usr/bin/mongod --config /etc/mongodb.conf
Success - and we get a nice pointer to the active config file (which is in the old key=value
format, see here for examples in the newer recommended YAML format).
Note: There will actually be two config files /etc/mongod.conf
and /etc/mongodb.conf
thanks to slight differences between the two packages we have installed. The now-active service points to the old-format one by default /etc/mongodb.conf
- you can switch by moving/copying the mongod.conf
to mongodb.conf
if you so wish, or simply edit the file directly.
Once started, to test, connect to the database with the mongo
shell:
$ mongo
MongoDB shell version: 3.2.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten]
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten]
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten]
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten]
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-04-14T16:40:34.962+0100 I CONTROL [initandlisten]
If you wish to get rid of the THP warnings, check out this answer.
Best Answer
You can do as following
after upgrading your service will be masked. To unmask it
for more detail see:
How to Upgrade MongoDB to Latest Stable Version – Mongoaudit — the mongoaudit guides – Medium -> https://medium.com/mongoaudit/how-to-upgrade-mongodb-to-latest-stable-version-9607266834cf
error: Unit mongodb.service is masked when starting mongodb - Ask Ubuntu -> error: Unit mongodb.service is masked when starting mongodb
update:
If you use different ubuntu version, you should update
<UBUNTU VERSION>
in this command:For example, if you use 18.10, change it to
cosmic
You can find your version with this command