Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save macfire/c0fbdc37403880fe71c0682543f1b1e4 to your computer and use it in GitHub Desktop.

Select an option

Save macfire/c0fbdc37403880fe71c0682543f1b1e4 to your computer and use it in GitHub Desktop.

Revisions

  1. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -241,8 +241,8 @@ $ ps aux | grep mongod
    $ mongo
    `````
    [Fix soft rlimits warnings](https://gist.github.com/tamitutor/6a1e41eec0ce021a9718)
    [Fix Access control is not enabled for the database](https://stackoverflow.com/questions/41615574/mongodb-server-has-startup-warnings-access-control-is-not-enabled-for-the-dat)
    - [Fix soft rlimits warnings](https://gist.github.com/tamitutor/6a1e41eec0ce021a9718)
    - [Fix Access control is not enabled for the database](https://stackoverflow.com/questions/41615574/mongodb-server-has-startup-warnings-access-control-is-not-enabled-for-the-dat)
    ## Debug
  2. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -234,6 +234,16 @@ $ ps aux | grep mongod
    # should now show `mongod` process running
    ````

    ## Match a Production environment

    ````
    # Mongo should bring some error messages to your attention which you can fix
    $ mongo
    `````
    [Fix soft rlimits warnings](https://gist.github.com/tamitutor/6a1e41eec0ce021a9718)
    [Fix Access control is not enabled for the database](https://stackoverflow.com/questions/41615574/mongodb-server-has-startup-warnings-access-control-is-not-enabled-for-the-dat)
    ## Debug
    The following are useful debug commands if needed.
  3. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -13,13 +13,13 @@ $ sudo mv mongodb /usr/local
    ## 2. Configuration

    ````sh
    # Create folder for log file
    # Create folder for log files
    $ sudo mkdir -p /var/log/mongodb/
    # Create folder for database path
    # Create folder for database files
    $ sudo mkdir -p /var/lib/mongodb/
    # Create folder for conf file
    # Create folder for conf files
    $ sudo mkdir -p /etc/mongodb/conf/
    # Create conf file
    # Create main conf file
    $ sudo vi /etc/mongodb/conf/mongod.conf
    ````

  4. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,12 @@ $ sudo mv mongodb /usr/local
    ````sh
    # Create folder for log file
    $ sudo mkdir -p /var/log/mongodb/
    # Create folder for database path
    $ sudo mkdir -p /var/lib/mongodb/
    # Create folder for conf file
    $ sudo mkdir -p /usr/local/mongodb/conf/
    $ sudo mkdir -p /etc/mongodb/conf/
    # Create conf file
    $ sudo vi /usr/local/mongodb/conf/mongod.conf
    $ sudo vi /etc/mongodb/conf/mongod.conf
    ````

    Enter the following
    @@ -120,7 +122,6 @@ Add MongoDB `/bin` folder to your path.
    export PATH="/usr/local/mongodb/bin:$PATH"
    ````


    ## 4. Permissions

    We never want to run as super user but create a special daemon `_mongod` for this.
    @@ -162,12 +163,13 @@ Finally, permissions:
    ````
    $ sudo chown -R _mongod:_mongod /var/lib/mongodb/
    $ sudo chown -R _mongod:_mongod /var/log/mongodb/
    $ sudo chown -R _mongod:_mongod /etc/mongodb/
    ````
    We can now open new terminal and test it works:

    ````sh
    # /we run the command as _mongod to see if it works
    $ sudo -u _mongod mongod --config /usr/local/etc/mongod.conf
    $ sudo -u _mongod mongod --config /etc/mongodb/conf/mongod.conf
    # show program is running
    $ ps aux | grep mongod
    # (should show program `mongod` running by `_mongod` daemon user)
    @@ -196,7 +198,7 @@ Enter the following:
    <array>
    <string>/usr/local/mongodb/bin/mongod</string>
    <string>--config</string>
    <string>/usr/local/mongodb/conf/mongod.conf</string>
    <string>/etc/mongodb/conf/mongod.conf</string>
    </array>

    <key>RunAtLoad</key>
  5. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -232,7 +232,9 @@ $ ps aux | grep mongod
    # should now show `mongod` process running
    ````

    If this does not work, simply debug with the following commands
    ## Debug

    The following are useful debug commands if needed.

    ````sh
    # Unload plist file
  6. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -140,7 +140,8 @@ $ dscl . list /Groups PrimaryGroupID | tr -s ' ' | sort -n -t ' ' -k2,2
    ````

    Therefore, here we will use 400 (make sure it is not taken).
    All daemon users are prefixed with an underscore, such as `_daemon`.

    Please note, on OS X, by convention, all daemon users are prefixed with an underscore, such as `_daemon`.
    Before creating a user, we create a group choosing an unused group id:

    ````
  7. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -137,7 +137,7 @@ $ dscl . list /Groups PrimaryGroupID
    # Get list of groups sorted by ID
    $ dscl . list /Groups PrimaryGroupID | tr -s ' ' | sort -n -t ' ' -k2,2
    ...
    ```
    ````

    Therefore, here we will use 400 (make sure it is not taken).
    All daemon users are prefixed with an underscore, such as `_daemon`.
  8. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 55 additions and 19 deletions.
    74 changes: 55 additions & 19 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -120,13 +120,59 @@ Add MongoDB `/bin` folder to your path.
    export PATH="/usr/local/mongodb/bin:$PATH"
    ````

    You can now open new terminal and test it works:

    ## 4. Permissions

    We never want to run as super user but create a special daemon `_mongod` for this.

    Get the last used GID:

    ````
    # Get the last used GID
    $ dscacheutil -q group | grep gid | tail -n 1
    gid: 399
    # Get list of groups
    $ dscl . list /Groups PrimaryGroupID
    ...
    # Get list of groups sorted by ID
    $ dscl . list /Groups PrimaryGroupID | tr -s ' ' | sort -n -t ' ' -k2,2
    ...
    ```
    Therefore, here we will use 400 (make sure it is not taken).
    All daemon users are prefixed with an underscore, such as `_daemon`.
    Before creating a user, we create a group choosing an unused group id:
    ````
    # Create _mongod group
    $ sudo dscl . -create /Groups/_mongod
    $ sudo dscl . -create /Groups/_mongod PrimaryGroupID 400
    ````
    Once done, we create a new user (we use the same id as we did for the group) - no shell is needed for daemon users:
    ````
    $ sudo dscl . -create /Users/_mongod UniqueID 400
    $ sudo dscl . -create /Users/_mongod PrimaryGroupID 400
    $ sudo dscl . -create /Users/_mongod UserShell /usr/bin/false
    ````
    Finally, permissions:
    ````
    $ sudo chown -R _mongod:_mongod /var/lib/mongodb/
    $ sudo chown -R _mongod:_mongod /var/log/mongodb/
    ````
    We can now open new terminal and test it works:
    ````sh
    mongod --config /usr/local/etc/mongod.conf
    # /we run the command as _mongod to see if it works
    $ sudo -u _mongod mongod --config /usr/local/etc/mongod.conf
    # show program is running
    $ ps aux | grep mongod
    # (should show program `mongod` running by `_mongod` daemon user)
    ````

    ## 4. Create start script
    ## 5. Create starting script

    We can create a launchctl start script to start `mongod` automatically when starting OS X.

    @@ -157,6 +203,12 @@ Enter the following:

    <key>KeepAlive</key>
    <true/>

    <key>GroupName</key>
    <string>_mongod</string>

    <key>UserName</key>
    <string>_mongod</string>

    <key>WorkingDirectory</key>
    <string>/usr/local/mongodb</string>
    @@ -214,20 +266,4 @@ $ tail -f /var/log/system.log
    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
    https://serverfault.com/questions/183589/how-do-i-activate-launchd-logging-on-os-x
    https://github.com/mongodb/mongo/blob/v2.4/debian/mongodb.conf

    ## Note

    This can be improved by creating a spedial user `mongod` to run `mongod` the Linux way.

    In plist, we could add:
    ````
    <key>GroupName</key>
    <string>nobody</string>
    <key>UserName</key>
    <string>nobody</string>
    ````



    https://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5
  9. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -219,4 +219,15 @@ https://github.com/mongodb/mongo/blob/v2.4/debian/mongodb.conf

    This can be improved by creating a spedial user `mongod` to run `mongod` the Linux way.

    In plist, we could add:
    ````
    <key>GroupName</key>
    <string>nobody</string>
    <key>UserName</key>
    <string>nobody</string>
    ````



    https://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5
  10. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -209,4 +209,14 @@ $ cat /var/log/mongodb/output.log
    $ tail -f /var/log/system.log
    ````

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
    ## References

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
    https://serverfault.com/questions/183589/how-do-i-activate-launchd-logging-on-os-x
    https://github.com/mongodb/mongo/blob/v2.4/debian/mongodb.conf

    ## Note

    This can be improved by creating a spedial user `mongod` to run `mongod` the Linux way.

    https://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5
  11. @Sydney-o9 Sydney-o9 renamed this gist Jul 11, 2017. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions gistfile1.md → install-mongodb-mac-os-x.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@

    ## Download latest source
    ## 1. Download latest source

    ````sh
    # Get latest from MongoDB website
    @@ -10,7 +10,7 @@ $ cp -R -n mongodb-osx-x86_64-3.4.6/ mongodb
    $ sudo mv mongodb /usr/local
    ````

    ## Configuration
    ## 2. Configuration

    ````sh
    # Create folder for log file
    @@ -111,7 +111,7 @@ logappend=true
    # replSet = setname
    ````

    ## Edit Path
    ## 3. Add MongoDB to your Path

    Add MongoDB `/bin` folder to your path.

    @@ -126,7 +126,7 @@ You can now open new terminal and test it works:
    mongod --config /usr/local/etc/mongod.conf
    ````

    ## Create start script
    ## 4. Create start script

    We can create a launchctl start script to start `mongod` automatically when starting OS X.

    @@ -175,6 +175,8 @@ Load launchctl script

    ````sh
    $ sudo launchctl load -w /Library/LaunchDaemons/mongod.plist
    $ ps aux | grep mongod
    # should now show `mongod` process running
    ````

    If this does not work, simply debug with the following commands
    @@ -205,7 +207,6 @@ $ cat /var/log/mongodb/output.log

    # View System log while launchctl file is loaded and unloaded
    $ tail -f /var/log/system.log

    ````

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
  12. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. 1 changed file with 94 additions and 0 deletions.
    94 changes: 94 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -111,7 +111,101 @@ logappend=true
    # replSet = setname
    ````

    ## Edit Path

    Add MongoDB `/bin` folder to your path.

    ````sh
    # MongoDB
    export PATH="/usr/local/mongodb/bin:$PATH"
    ````

    You can now open new terminal and test it works:

    ````sh
    mongod --config /usr/local/etc/mongod.conf
    ````

    ## Create start script

    We can create a launchctl start script to start `mongod` automatically when starting OS X.

    ````sh
    # MongoDB
    sudo vi /Library/LaunchDaemons/mongod.plist
    ````
    Enter the following:

    ````xml
    <?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>mongodb</string>

    <key>ProgramArguments</key>
    <array>
    <string>/usr/local/mongodb/bin/mongod</string>
    <string>--config</string>
    <string>/usr/local/mongodb/conf/mongod.conf</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

    <key>WorkingDirectory</key>
    <string>/usr/local/mongodb</string>

    <key>StandardErrorPath</key>
    <string>/var/log/mongodb/error.log</string>

    <key>StandardOutPath</key>
    <string>/var/log/mongodb/output.log</string>

    </dict>
    </plist>
    ````

    Load launchctl script

    ````sh
    $ sudo launchctl load -w /Library/LaunchDaemons/mongod.plist
    ````

    If this does not work, simply debug with the following commands

    ````sh
    # Unload plist file
    $ sudo launchctl unload /Library/LaunchDaemons/mongod.plist

    # Load plist file
    $ sudo launchctl load -w /Library/LaunchDaemons/mongod.plist

    # See if plist file is in the list
    $ sudo launchctl list | grep mongodb

    # Start Mongodb
    $ sudo launchctl start mongodb

    # Stop Mongodb
    $ sudo launchctl stop mongodb

    # See if mongod is running
    $ ps aux | grep mongod

    # See log files
    $ cat /var/log/mongodb/mongodb.log
    $ cat /var/log/mongodb/error.log
    $ cat /var/log/mongodb/output.log

    # View System log while launchctl file is loaded and unloaded
    $ tail -f /var/log/system.log

    ````

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
  13. @Sydney-o9 Sydney-o9 revised this gist Jul 11, 2017. No changes.
  14. @Sydney-o9 Sydney-o9 renamed this gist Jul 11, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  15. @Sydney-o9 Sydney-o9 created this gist Jul 11, 2017.
    117 changes: 117 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,117 @@

    ## Download latest source

    ````sh
    # Get latest from MongoDB website
    $ curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.6.tgz
    $ tar -zxvf mongodb-osx-x86_64-3.4.6.tgz
    $ mkdir -p mongodb
    $ cp -R -n mongodb-osx-x86_64-3.4.6/ mongodb
    $ sudo mv mongodb /usr/local
    ````

    ## Configuration

    ````sh
    # Create folder for log file
    $ sudo mkdir -p /var/log/mongodb/
    # Create folder for conf file
    $ sudo mkdir -p /usr/local/mongodb/conf/
    # Create conf file
    $ sudo vi /usr/local/mongodb/conf/mongod.conf
    ````

    Enter the following
    ````txt
    # mongodb.conf

    # Where to store the data.

    # Note: if you run mongodb as a non-root user (recommended) you may
    # need to create and set permissions for this directory manually,
    # e.g., if the parent directory isn't mutable by the mongodb user.
    dbpath=/var/lib/mongodb

    #where to log
    logpath=/var/log/mongodb/mongodb.log

    logappend=true

    #port = 27017

    # Disables write-ahead journaling
    # nojournal = true

    # Enables periodic logging of CPU utilization and I/O wait
    #cpu = true

    # Turn on/off security. Off is currently the default
    #noauth = true
    #auth = true

    # Verbose logging output.
    #verbose = true

    # Inspect all client data for validity on receipt (useful for
    # developing drivers)
    #objcheck = true

    # Enable db quota management
    #quota = true

    # Set oplogging level where n is
    # 0=off (default)
    # 1=W
    # 2=R
    # 3=both
    # 7=W+some reads
    #diaglog = 0

    # Ignore query hints
    #nohints = true

    # Disable the HTTP interface (Defaults to localhost:28017).
    #nohttpinterface = true

    # Turns off server-side scripting. This will result in greatly limited
    # functionality
    #noscripting = true

    # Turns off table scans. Any query that would do a table scan fails.
    #notablescan = true

    # Disable data file preallocation.
    #noprealloc = true

    # Specify .ns file size for new databases.
    # nssize = <size>

    # Accout token for Mongo monitoring server.
    #mms-token = <token>

    # Server name for Mongo monitoring server.
    #mms-name = <server-name>

    # Ping interval for Mongo monitoring server.
    #mms-interval = <seconds>

    # Replication Options

    # in master/slave replicated mongo databases, specify here whether
    # this is a slave or master
    #slave = true
    #source = master.example.com
    # Slave only: specify a single database to replicate
    #only = master.example.com
    # or
    #master = true
    #source = slave.example.com

    # in replica set configuration, specify the name of the replica set
    # replSet = setname
    ````




    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/