Installing Mac Mongodb

Installing Mac Mongodb

by John Vincent


Posted on April 13, 2017


Installing and Configuring MongoDB

This article refers only to Mac

Installation of MongoDB Version 4.4.2

Install MongoDB Enterprise on macOS

MongoDB Configuration File Options

From MongoDB, downloaded version 4.4.2

Copied to: /Users/jv/Desktop/OtherTools/mongodb

Created data directory

sudo mkdir -p /usr/local/var/mongodb

Created log directory

sudo mkdir -p /usr/local/var/log/mongodb

Set permissions

sudo chown my_mongodb_user /usr/local/var/mongodb
sudo chown my_mongodb_user /usr/local/var/log/mongodb

MACOS PREVENTS MONGOD FROM OPENING

macOS may prevent mongod from running after installation. If you receive a security error when starting mongod indicating that the developer could not be identified or verified, do the following to grant mongod access to run:

  • Open System Preferences
  • Select the Security and Privacy pane.
  • Under the General tab, click the button to the right of the message about mongod, labelled either Open Anyway or Allow Anyway depending on your version of macOS.

mongodb.conf

systemLog:
  destination: file
  path: "/usr/local/var/log/mongodb/mongod.log"
  logAppend: true
storage:
  dbPath: "/usr/local/var/mongodb"
  journal:
    enabled: true
processManagement:
 fork: false
net:
  bindIp: 127.0.0.1
  port: 27017
setParameter:
  enableLocalhostAuthBypass: false

start-database

#!/bin/sh
#
# script to start mongodb server
#
# Start Mongodb
#
MONGODB_CONFIG_FILE=mongodb.conf
echo "MongoDB Data: $MONGODB_DATA"
echo "MongoDB Config File: $MONGODB_CONFIG_FILE"

VERBOSITY=-vvvv
VERBOSITY=
echo "VERBOSITY $VERBOSITY"
#
echo Starting mongodb
mongod --config $MONGODB_CONFIG_FILE

Start MongoDB

./start-database

Installation of MongoDB Version 3.2.11

Planning to use:

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/db

Create 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: false

Notice 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_FILE

Create file start-console

#!/bin/sh
#
# script to start mongo command line
#
# Start Mongo
#
echo Starting mongo command line
mongo

Edit .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:$PATH

Manage MongoDB

Start MongoDB

cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-database

Stop MongoDB

Ctrl-C the task

Mongo Console

cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-console

Import into MongoDB

mongoimport --db tempTestDb --collection restaurants --drop --file primer-dataset.json

System Limits

The default open file limit of 256 is too low for Mongo. Let's increase that.

Check current limits

ulimit -a
sudo launchctl limit

Create file

sudo vi /Library/LaunchDaemons/limit.maxfiles.plist

insert

<?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.plist

insert

<?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.plist

Reload the launcher

sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxproc.plist

Open a new terminal session and verify the limits

ulimit -a

Verify open files >= 2560

Restart Mongo

cd /Users/jv/Desktop/MyDevelopment/repo_shell_scripts/mac/mongo
./start-database

Some 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.documents
  • A document has a primary key field _id added by default.

Other

For a 100MB Json file

https://github.com/seductiveapps/largeJSON

A mongo example database:

https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json

Using mLab for Remote MongoDB Deployments

Get an account at mLab

Note:

Errors

“mongod” cannot be opened because the developer cannot be verified.

Solution is to select mongod in the Finder, Open the file.