— Linux — 1 min read
To ensure the credibility of the packages Ubuntu makes sure they are signed with GPG keys. Let’s begin by importing the GPG keys we need for the official MongoDB repository:
1$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Next add the MongoDB repository in /etc/apt/sources.list.d using this command:
1$ echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
Installing MongoDB
1$ sudo apt-get update2$ sudo apt-get install -y mongodb-org
Create service file for MongoDB
1$ sudo vim /etc/systemd/system/mongodb.service
Paste following code:
1[Unit]2Description=High-performance, schema-free document-oriented database3After=network.target4Documentation=https://docs.mongodb.org/manual5
6[Service]7User=mongodb8Group=mongodb9ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf10
11[Install]12WantedBy=multi-user.target
Now we have to update systemd to include our newly created service and we enable and start the service:
1$ sudo systemctl daemon-reload2$ sudo systemctl enable mongod3$ sudo systemctl start mongod
Create admin user in MongoDB:
1$ mongo2> use admin3> db.createUser({user:"admin", pwd:"admin54321", roles:[{role:"root", db:"admin"}]})4> exit
Enable MongoDB authentication
Open file /etc/systemd/system/mongodb.service and update following line:
1ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf
After that reload configurations:
1$ sudo systemctl daemon-reload2$ sudo systemctl restart mongod
Now connect to the MongoDB shell using this command:
1$ mongo -u admin -p --authenticationDatabase admin