MongoDB

Installation

To install the latest version of MongoDB:

  • Type this in Terminal:
    brew install mongodb
    

To install a specific version of MongoDB:

  • Type the followings in Terminal instead:
    brew search mongodb
    
  • You should see the output like these:
    mongodb
    homebrew/php/php54-mongodb                                 homebrew/versions/mongodb30
    homebrew/php/php55-mongodb                                 Caskroom/cask/mongodb-compass
    homebrew/php/php56-mongodb                                 Caskroom/cask/mongodb
    homebrew/php/php70-mongodb                                 Caskroom/cask/mongodbpreferencepane
    homebrew/php/php71-mongodb                                 Caskroom/cask/orelord-mongodb
    homebrew/versions/mongodb26                                Caskroom/versions/mongodb-compass-beta
    
  • Copy the version you want to install, e.g. homebrew/versions/mongodb30
  • And run brew install like this:
    brew install homebrew/versions/mongodb30
    

  • To check the installed MongoDB daemon version:

    mongod --version
    
  • To check the the Mongo Shell version:

    mongo --version
    

Start/Restart/Stop MongoDB

  • To start the MongoDB, type this in Terminal:

    brew services start mongodb
    
  • If you get an error of Formula mongodb is not installed, it may because you chose to install specific version of MongoDB.

    • Try this to find the correct name of the service:

      brew services list
      
    • Then start MongoDB with the correct service name for example:

      brew services start mongodb30
      
  • To restart:

    brew services restart mongodb
    
  • To stop: (or prevent MongoDB from started again when the Mac is reboot)

    brew services stop mongodb
    
  • Notes: If brew services list showing the service status is started, the service will become started again when the Mac is reboot.

Troubleshooting

  • If you see this error while using brew services command:
    Error: Unknown command: services
    
  • It is because brew services has not been tapped.
  • Go back to the Homebrew section under Core Setup.

Replica Set mode

  • Skip this part if you don't need to run MongoDB in both standalone and replica set modes at the same time.

  • This is to create a replica set with 3 nodes running on ports 27117, 27118, 27119

  • Determine the folder to store the LaunchAgent plist

    brew services list
    
  • If the service name is mongodb, the LaunchAgent plist will be stored in /usr/local/opt/mongodb

  • Type this to confirm:

    ls -la /usr/local/opt/mongodb
    
  • If you install older version of MongoDB, the service name may be something else, e.g. mongodb30

  • Then the LaunchAgent plist will be stored in /usr/local/opt/mongodb30/ instead

  • After you find the folder, do this to ease the upcoming steps:

    export mongodb_service_name="mongodb"
    
  • If you install older version of MongoDB: (replace the service name)

    export mongodb_service_name="mongodb30"
    
  • Create log folders

    mkdir -p /usr/local/var/log/mongodb_rs1_node1
    mkdir -p /usr/local/var/log/mongodb_rs1_node2
    mkdir -p /usr/local/var/log/mongodb_rs1_node3
    
  • Create data folders
    mkdir -p /usr/local/var/mongodb_rs1_node1
    mkdir -p /usr/local/var/mongodb_rs1_node2
    mkdir -p /usr/local/var/mongodb_rs1_node3
    
  • Create config file for the 1st node /usr/local/etc/mongodb_rs1_node1.conf
    systemLog:
      destination: file
      path: "/usr/local/var/log/mongodb_rs1_node1/mongo.log"
      quiet: true
      logAppend: true
    storage:
      journal:
          enabled: true
      dbPath: "/usr/local/var/mongodb_rs1_node1"
      engine: "wiredTiger"
    processManagement:
      fork: true
    net:
      port: 27117
    replication:
      replSetName: "rs1"
    
  • Create config file for the 2nd node /usr/local/etc/mongodb_rs1_node2.conf
    systemLog:
      destination: file
      path: "/usr/local/var/log/mongodb_rs1_node2/mongo.log"
      quiet: true
      logAppend: true
    storage:
      journal:
          enabled: true
      dbPath: "/usr/local/var/mongodb_rs1_node2"
      engine: "wiredTiger"
    processManagement:
      fork: true
    net:
      port: 27118
    replication:
      replSetName: "rs1"
    
  • Create config file for the 3rd node /usr/local/etc/mongodb_rs1_node3.conf
    systemLog:
      destination: file
      path: "/usr/local/var/log/mongodb_rs1_node3/mongo.log"
      quiet: true
      logAppend: true
    storage:
      journal:
          enabled: true
      dbPath: "/usr/local/var/mongodb_rs1_node3"
      engine: "wiredTiger"
    processManagement:
      fork: true
    net:
      port: 27119
    replication:
      replSetName: "rs1"
    
  • Create LaunchAgent for the 1st node /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node1.plist
    <?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>homebrew.mxcl.mongodb_rs1_node1</string>
      <key>ProgramArguments</key>
      <array>
         <string>/usr/local/bin/mongod</string>
         <string>--config</string>
         <string>/usr/local/etc/mongodb_rs1_node1.conf</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>KeepAlive</key>
      <false/>
      <key>WorkingDirectory</key>
      <string>/usr/local</string>
      <key>StandardErrorPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node1/output.log</string>
      <key>StandardOutPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node1/output.log</string>
      <key>HardResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
      <key>SoftResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
    </dict>
    </plist>
    
  • Create LaunchAgent for the 2nd node /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node2.plist
    <?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>homebrew.mxcl.mongodb_rs1_node2</string>
      <key>ProgramArguments</key>
      <array>
         <string>/usr/local/bin/mongod</string>
         <string>--config</string>
         <string>/usr/local/etc/mongodb_rs1_node2.conf</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>KeepAlive</key>
      <false/>
      <key>WorkingDirectory</key>
      <string>/usr/local</string>
      <key>StandardErrorPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node2/output.log</string>
      <key>StandardOutPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node2/output.log</string>
      <key>HardResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
      <key>SoftResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
    </dict>
    </plist>
    
  • Create LaunchAgent for the 3rd node /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node3.plist
    <?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>homebrew.mxcl.mongodb_rs1_node3</string>
      <key>ProgramArguments</key>
      <array>
         <string>/usr/local/bin/mongod</string>
         <string>--config</string>
         <string>/usr/local/etc/mongodb_rs1_node3.conf</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>KeepAlive</key>
      <false/>
      <key>WorkingDirectory</key>
      <string>/usr/local</string>
      <key>StandardErrorPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node3/output.log</string>
      <key>StandardOutPath</key>
      <string>/usr/local/var/log/mongodb_rs1_node3/output.log</string>
      <key>HardResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
      <key>SoftResourceLimits</key>
      <dict>
         <key>NumberOfFiles</key>
         <integer>1024</integer>
      </dict>
    </dict>
    </plist>
    
  • Enable these 3 LaunchAgents
    cd ~/Library/LaunchAgents
    ln -s /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node1.plist homebrew.mxcl.mongodb_rs1_node1.plist
    ln -s /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node2.plist homebrew.mxcl.mongodb_rs1_node2.plist
    ln -s /usr/local/opt/$mongodb_service_name/homebrew.mxcl.mongodb_rs1_node3.plist homebrew.mxcl.mongodb_rs1_node3.plist
    launchctl load homebrew.mxcl.mongodb_rs1_node?.plist
    
  • Verify if the 3 MongoDB nodes are running properly
    mongo --port 27117
    mongo --port 27118
    mongo --port 27119
    
  • Initialize Replica Set
    mongo --port 27117
    
  • In Mongo shell, type:
    rsconf = {
             _id: "rs1",
             members: [
                        {
                         _id: 0,
                         host: "localhost:27117"
                        }
                      ]
           }
    rs.initiate( rsconf )
    rs.add("localhost:27118")
    rs.add("localhost:27119")
    
  • To verify:
    mongo --port 27117
    
  • Then in Mongo shell:
    rs.status()
    
  • You should see output like these:
    {
      "set" : "rs1",
      "date" : ISODate("2016-12-25T17:46:47.411Z"),
      "myState" : 1,
      "members" : [
          {
              "_id" : 0,
              "name" : "localhost:27117",
              "health" : 1,
              "state" : 1,
              "stateStr" : "PRIMARY",
              "uptime" : 54,
              "optime" : Timestamp(1482687993, 1),
              "optimeDate" : ISODate("2016-12-25T17:46:33Z"),
              "electionTime" : Timestamp(1482687992, 2),
              "electionDate" : ISODate("2016-12-25T17:46:32Z"),
              "configVersion" : 3,
              "self" : true
          },
          {
              "_id" : 1,
              "name" : "localhost:27118",
              "health" : 1,
              "state" : 2,
              "stateStr" : "SECONDARY",
              "uptime" : 14,
              "optime" : Timestamp(1482687993, 1),
              "optimeDate" : ISODate("2016-12-25T17:46:33Z"),
              "lastHeartbeat" : ISODate("2016-12-25T17:46:45.865Z"),
              "lastHeartbeatRecv" : ISODate("2016-12-25T17:46:45.864Z"),
              "pingMs" : 0,
              "configVersion" : 3
          },
          {
              "_id" : 2,
              "name" : "localhost:27119",
              "health" : 1,
              "state" : 2,
              "stateStr" : "SECONDARY",
              "uptime" : 13,
              "optime" : Timestamp(1482687993, 1),
              "optimeDate" : ISODate("2016-12-25T17:46:33Z"),
              "lastHeartbeat" : ISODate("2016-12-25T17:46:45.865Z"),
              "lastHeartbeatRecv" : ISODate("2016-12-25T17:46:45.865Z"),
              "pingMs" : 0,
              "configVersion" : 3
          }
      ],
      "ok" : 1
    }
    

results matching ""

    No results matching ""