TL;DR Summary: Instructions for installing MongoDB on Ubuntu and Mac are detailed, covering standalone configurations for each OS. The Ubuntu setup uses apt-get for installation, while the Mac installation requires manual downloads and configuration.
- Ubuntu installation involves updating the repository and using apt-get.
- Mac installation requires manual downloads, path updates, and starting the daemon.
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")
}
MongoDB Guide
- MongoDB Overview: Getting Started with MongoDB
- How to Install MongoDB on Ubuntu and Mac
- Creating a Database in MongoDB
- MongoDB Shell Basic Commands
- MongoDB Compass: Using the Mongo GUI
- MongoDB Role-Based Access Control (RBAC) Explained
- MongoDB Projection & Projection Operators Explained
- MongoDB Aggregate Functions Explained
- MongoDB Sorting: sort() Method & Examples
- Using Push & Pull Operators in MongoDB
- How To Use MongoDB $unwind
- 23 Common MongoDB Query Operators & How to Use Them
- MongoDB GeoLocation Query Examples
- Introduction to MongoDB Transactions
- MongoDB Indexes: Top Index Types & How to Manage Them
- Top MongoDB Commands You Need to Know
- MongoDB Replication: A Complete Introduction
- How to Setup a MongoDB Cluster
- MongoDB Sharding: Concepts, Examples & Tutorials
- MongoDB Atlas: Setting Up & Using Managed MongoDB
- How To Run MongoDB as a Docker Container
- MongoDB Memory Usage, Management, & Requirements
- How To Use mongodump for MongoDB Backups
- Mongorestore Examples for Restoring MongoDB Backups
- How To Use PyMongo
- How to Use Mongoose for MongoDB & NodeJS
- MongoDB Cheat Sheet
- MongoDB vs Cassandra: NoSQL Databases Compared
- PostgreSQL vs MongoDB: Comparing Databases
- MySQL vs MongoDB: Comparing Databases
- MongoDB Certifications: An Introduction
These postings are my own and do not necessarily represent BMC's position, strategies, or opinion.