image_pdfimage_print

Here we explain how to install MongoDB in a stand-alone configuration on Ubuntu and Mac. Setting up a cluster is significantly more complicated. So we will explain that in another post.

Install MongoDB on Ubuntu

MongoDB comes in Free (Community), paid (Enterprise), and cloud (Atlas) editions. It’s not easy to find the free download on their landing page, but here it is.

If you are using Ubuntu, you don’t need the download. Just run these steps to update the code repository then use apt-get to install the product.

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

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

sudo apt-get update

sudo apt-get install -y mongodb-org

The configuration file is /etc/mongod. It’s not necessary to change anything for a single-cluster installation.

Now start the service. The d in mongod means daemon.

sudo service mongod start

Look in the log to verify that it is running and look for any possible errors.

tail -f /var/log/mongodb/mongod.log

Then open the shell.

mongo

Create a database. Just using the word use creates the database.

use books

Install MongoDB on Mac

Here we install MongoDB on Mac. Go to this web page and fill out the version information to download MongoDB.

cd Documents
mkdir mongodb
cd mongodb
wget https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.5.tgz
tar xvfz mongodb-osx-ssl-x86_64-4.0.5.tgz

Update the path and make a data directory.

cd
vim .bash_profile

export PATH="/Users/walkerrowe/Documents/mongodb/mongodb-osx-x86_64-4.0.5/bin:$PATH"

source .bash_profile

mkdir -p Documents/mongodb/mongodb-osx-x86_64-4.0.5/data
mkdir -p Documents/mongodb/mongodb-osx-x86_64-4.0.5/data/db

Start the daemon, telling it where to find the database. There will be no log files, so look at stdout (i.e., the screen where you started it) for errors. With the Mac installation, there is no configuration file either as you can pass configuration options on the command line, which you can also do on Ubuntu and other platforms. Or you could create a configuration file and tell the daemon to use that.

mongod --dbpath Documents/mongodb/mongodb-osx-x86_64-4.0.5/data/db

Create a Database and add some data:

use books

switched to db books

Create a collection. A collection is a group of documents, like a table in a regular RDBMS database. Notice that the use statement makes the db object come into scope, so you can use that for subsequent operations.

db.createCollection("books")
{ "ok" : 1 }

MongoDB stores documents in JSON format. So you can add any JSON. But as we will see below, you still need to tell it what fields you want to index.

Create an index. The -1 means descending order.

db.collection.createIndex( { isbn: -1 } )
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

Add a data record:

db.collection.insertOne( { isbn: 100 } )
{
"acknowledged" : true,
"insertedId" : ObjectId("5c4493aa750820eae9756a15")
}

Free E-book: The Beginner’s Guide to MongoDB

MongoDB is the most popular NoSQL database today and with good reason. This e-book is a general overview of MongoDB, providing a basic understanding of the database.


Last updated: 01/23/2019

These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.

See an error or have a suggestion? Please let us know by emailing blogs@bmc.com.

BMC Bring the A-Game

From core to cloud to edge, BMC delivers the software and services that enable nearly 10,000 global customers, including 84% of the Forbes Global 100, to thrive in their ongoing evolution to an Autonomous Digital Enterprise.
Learn more about BMC ›

About the author

Walker Rowe

Walker Rowe

Walker Rowe is an American freelancer tech writer and programmer living in Cyprus. He writes tutorials on analytics and big data and specializes in documenting SDKs and APIs. He is the founder of the Hypatia Academy Cyprus, an online school to teach secondary school children programming. You can find Walker here and here.