Skip to content

Mongo Start

coder4869 edited this page Aug 29, 2016 · 3 revisions

This page shows how to build the basic MongoDB environment as well as some configure.

[1-4] refer to https://github.com/coder4869/mongo/blob/master/mongo_install.sh
[6] refer to https://github.com/coder4869/mongo/blob/master/mongo_master_slave.sh

1. MongoDB Install

[1].download mongodb (http://www.mongodb.org/downloads)
    $ curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.6.tgz
[2].unzip and copy it to "/usr/local/":
    $ sudo cp -r /home/<username>/Desktop/mongodb3.2.6 /usr/local/
[3].Make Path Config: (".bashrc" for linux and ".bashrc_profile" for mac)
    $ vim ~/.bashrc
    add "export PATH=$PATH:/usr/local/mongodb3.2.6/bin" to end of file ".bashrc"
    save and exit, then run "$ source ~/.bashrc"
[4].review result
    “$ echo ${PATH} ” or “$ mongo”

2.Set db and log location

    create dir db:$ mkdir -p /usr/local/mongodb3.2.6/data/db
    create dir log:$ mkdir /usr/local/mongodb3.2.6/log
    create file mongodb.log:$ touch /usr/local/mongodb3.2.6/log/mongodb.log

3.mongodb start config

$ touch /usr/local/mongodb3.2.6/mongodb.conf
set content of mongodb.conf as:
    port=10001
    dbpath=/usr/local/mongodb3.2.6/data/db  # absolute path
    logpath=/usr/local/mongodb3.2.6/log/mongodb.log  # absolute path
    fork = true 
    oplogSize=2048 #similar to mysql’s log scroll,unit is MB

4.run & use mongodb

  run mongodb : $mongod -f /usr/local/mongodb3.2.6/mongodb.conf
  link mongodb : $ mongo 127.0.0.1:10001
  create db : > use goweb_test
  create user : > db.createUser({"user":"goweb_test", "pwd":"goweb_test", roles:[]})

5. change maxConns (for example 3000)

Mac OS : 
    Step 1. adding "maxConns=3000" to file 'mongodb.conf'
    Step 2. running cmd "$ ulimit -n 4000 && mongod" (the number in cmd must over than number in step1, here 4000 > 3000)
    Step 3. restart mongodb, link it and use cmd "db.serverStatus().connections;" review result (following 3000=156+2844):
            { "current" : 156, "available" : 2844, "totalCreated" : NumberLong(582) }
Notice: if you use a number in step2 (like 3001), which very close to number in step1, the config maybe not active.

6. master-slave

master: adding following content to file "mongodb.conf"
    master=true
slave: adding following content to file "mongodb.conf"
    slave=true  
    source=127.0.0.1:10001  #set master server
    slavedelay=10  #time differ for delaying copy from master, backup once for each 10s
    autoresync=true  #if data not latest, send sync request to master server
Clone this wiki locally