Install and Configure MongoDB on Ubuntu
/Import the MongoDB public key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
Create a list file for MongoDB
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Reload the package database
sudo apt-get update
Install the MongoDB packages
sudo apt-get install -y mongodb-org
Create a new MongoDB systemd service if one doesn't already exist. First check to see if the file already exists:
ls /lib/systemd/system
If the file doesn't exist then create it:
sudo vim mongod.service
Paste the script below and save the file:
[Unit] Description=High-performance, schema-free document-oriented database After=network.target Documentation=https://docs.mongodb.org/manual [Service] User=mongodb Group=mongodb ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf [Install] WantedBy=multi-user.target *Esc Key* :w
Update the systemd service
systemctl daemon-reload
Enable the service
systemctl enable mongod
If you start your mongo service at this point (let's wait) you will see several errors:
** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
MongoDB suggests setting these to 'never'
To address this we are going to create another service that is dependent on the mongod service.
Create service to set transparent_hugepage settings to never
cd /lib/systemd/system sudo vi mongodb-hugepage-fix.service
Paste the contents below and save the file:
[Unit] Description="Disable Transparent Hugepage before MongoDB boots" Before=mongod.service [Service] Type=oneshot ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' [Install] RequiredBy=mongod.service *Esc Key* :w
Update the systemd service
systemctl daemon-reload
Enable the service
systemctl enable mongodb-hugepage-fix.service
Now we are ready to start the services
systemctl start mongodb-hugepage-fix systemctl start mongod
Optional, check netstat to see if the service is listening
netstat -plntu
There are a few more things we need to do before our install is complete. We still need to modify our config file which is used to set configuration options for MongoDB, like data directories and user authentication. Before we enable auth we first need to create the mongo admin user with the root role.
Create the mongo admin account
use admin db.createUser ( {user: "mongoadmin", pwd: "UrStrongPassword", roles: [ "root" ]} )
Go ahead and exit mongo and stop the service:
*Ctrl+C* systemctl stop mongod
Next we are going to modify the config file. There are a few things I want to do here, the most important are setting authorization so that users have to provide a username and password to access MongoDB and to make sure my data directories are in the correct place. Let's open up the config file (this file should already exist, if not you'll want to create it). We only want to take a quick look at the file then close it so we can work on the data directories.
cd /etc sudo vi mongod.conf :q!
To see all your configuration options in check out MongoDB documentation:
https://docs.mongodb.com/manual/reference/configuration-options/
The first option in the config file we want to look at is the default data directory. The default directory for MongoDB data files is /var/lib/mongodb, but we don't want to store our data files there. Instead we have a raid 1/0 partition we'd like to use for our data. The directory for our RAID 1/0 is /data, so we are going to move the mongo data files.
Move our mongo data files to a new directory
*Make sure the mongo service is stopped.
cd /var/lib sudo mv mongodb /data/mongodb sudo ls -s /data/mongodb/ /var/lib/mongodb
Now we are ready to get back in to the config file
cd /etc sudo vi mongod.conf
We need to update the data directory in the config file to point to the directory we just moved our data files from the previous step, and set the bindIp of the network so we can connect remotely. Below is what the configuration file looks like for those options:
# Set the location for your data storage: dbPath: /data/mongodb journal: enabled: true engine: wiredTiger # Set the network options net: port: 27017 bindIp: 0.0.0.0 ssl: mode: disabled *Esc* :w
Enable authentication for the mongod.service
Open the mongod.service file in your editor
sudo vi /lib/systemd/system/mongod.service
Add the --auth option to the mongod.service and save the file, the entire line should look like:
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf *Esc* :w
Update the systemd service
systemctl daemon-reload
Now we are ready to start the service
systemctl start mongod
Connect to mongo with the user we created
mongo YourServerName:27017/admin -u mongoadmin -p password