Scroll to top
© 2018 All Rights Reserved.
Share

How to Install MongoDB on Debian


Dominik Sachsenhofer - 5. November 2017 - 0 comments

What is MongoDB?

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time. The document model maps to the objects in your application code, making data easy to work with Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data. MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use. MongoDB is free and open-source, published under the GNU Affero General Public License.

 

1. Install MongoDB

1.1 Importing the Public Key

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

 

1.2 Create Source List File MongoDB

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

 

1.3 Update the Repository

sudo apt-get update

 

1.4 Install MongoDB

sudo apt-get install -y mongodb-org

 

2. Create a Service

2.1 Create a service to run mongod on system startup

cd /etc/systemd/system/

sudo nano mongod.service

 

File mongod.service:

[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

Save file and exit.

 

Reload system daemon:
sudo systemctl daemon-reload

 

Start mongodb and add it as service to be started at boot time:

sudo systemctl start mongod

sudo systemctl enable mongod

 

Now check that mongodb has been started on port 27017 with the netstat command.
netstat -plntu

 

3. Create a Database and an User

Before you set up a username and password for MongoDB, you need to open the mongodb shell on your server. You can login by typing:
mongo

If you get error Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly, try the command:
export LC_ALL=C

mongo

 

3.1 Create a Database

Once you`re in the MongoDB shell, switch to the database named admin:
use admin

 

3.2 Create an User

Create the root user with this command:
db.createUser({user:"admin", pwd:"[PASSWORD]", roles:[{role:"readWrite", db:"admin"}]})

Now type exit to exit from MongoDB shell.

 

3.3 Add –auth option

Edit the mongodb service file ‘/etc/systemd/system/mongod.service’ with your editor.
sudo nano /etc/systemd/system/mongod.service

On the ‘ExecStart’ line 9, add the new option ‘–auth’.
ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

Save and exit.

Reload the systemd service:
sudo systemctl daemon-reload

Now restart MongoDB and connect with the user created.
sudo service mongod restart

 

4. Connect to the database

Connect to the mongodb shell with this command:
mongo -u admin -p [PASSWORD] --authenticationDatabase admin

 

5. Optional: Allow remote access to the database

sudo nano /etc/mongod.conf

File: /etc/mongod.conf

Listen to local interface only. Comment out to listen on all interfaces.
#bind_ip = 127.0.0.1

Links: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian

Post a Comment