Installing Mac Mongodb
by John Vincent
Posted on April 13, 2017
Installing and Configuring MongoDB
This article refers only to Mac
Installation
Planning to use:
- MongoDB https://www.mongodb.com/ locally and
- mLab https://mlab.com/ remotely.
They must use the same version of MongoDB.
As mLab is using MongoDB 3.2 at this time, I will install MongoDB 3.2 locally.
Install, Start and Stop MongoDB
From MongoDB, downloaded version 3.2.11.
Copied to:
/Users/jv/Desktop/OtherTools/mongodb
cd /Users/jv/Desktop/OtherTools/mongodb
mkdir data/dbCreate Mongo Shell Scripts
cd ~/repo_shell_scripts/mac/mongodb
Create file mongodb.conf
systemLog:
destination: file
path: "/Users/jv/Desktop/OtherTools/mongodb/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: false
net:
bindIp: 127.0.0.1
port: 27017
setParameter:
enableLocalhostAuthBypass: falseNotice port is specified here. The default is 27017
Create file start-database
#!/bin/sh
#
# script to start mongodb server
#
# Start Mongodb
#
echo Starting mongodb
mongod --dbpath $MONGODB_DATA --config $MONGODB_CONFIG_FILECreate file start-console
#!/bin/sh
#
# script to start mongo command line
#
# Start Mongo
#
echo Starting mongo command line
mongoEdit .bash_profile
#
# add MongoDB
#
MONGODB_HOME=/Users/jv/Desktop/OtherTools/mongodb
MONGODB_DATA=$MONGODB_HOME/data/db
MONGODB_CONFIG_FILE=mongodb.conf
echo "MongoDB Home: $MONGODB_HOME"
echo "MongoDB Data: $MONGODB_DATA"
echo "MongoDB Config File: $MONGODB_CONFIG_FILE"
export PATH=$MONGODB_HOME/bin:$PATHManage MongoDB
Start MongoDB
cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-databaseStop MongoDB
Ctrl-C the taskMongo Console
cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-consoleImport into MongoDB
mongoimport --db tempTestDb --collection restaurants --drop --file primer-dataset.jsonSystem Limits
The default open file limit of 256 is too low for Mongo. Let's increase that.
Check current limits
ulimit -a
sudo launchctl limitCreate file
sudo vi /Library/LaunchDaemons/limit.maxfiles.plistinsert
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>200000</string>
<string>200000</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>Create file
sudo vi /Library/LaunchDaemons/limit.maxproc.plistinsert
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true />
<key>ServiceIPC</key>
<false />
</dict>
</plist>Set owner and permissions
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
sudo chown root:wheel /Library/LaunchDaemons/limit.maxproc.plist
sudo chmod 644 /Library/LaunchDaemons/limit.maxproc.plistReload the launcher
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxproc.plistOpen a new terminal session and verify the limits
ulimit -aVerify open files >= 2560
Restart Mongo
cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-databaseSome MongoDB Console Commands
db (show current database)
show dbs (show databases)
use jv1 (switch to database jv1)
show collections (show tables)
db.restaurants.find() (query collection “restaurants")MongoDB Notes
Mongo database is not created until it has object(s) added.
Collection is a table
Documents are rows
Field is a column.
Documents are
org.son.documentsA document has a primary key field
_idadded by default.
Other
For a 100MB Json file
https://github.com/seductiveapps/largeJSONA mongo example database:
https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.jsonUsing mLab for Remote MongoDB Deployments
Get an account at mLab
Note:
Driver examples: http://docs.mlab.com/languages
Quickly get data into your MongoDB: http://blog.mlab.com/?p=2479
Free MongoDB University classes: https://education.mongodb.com/
Errors
“mongod” cannot be opened because the developer cannot be verified.Solution is to select mongod in the Finder, Open the file.