MongoDB note


# Install 3.2 for centos/6
& amazon linux

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/

```/etc/yum.repos.d/mongodb.repo
[monbodb]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/6/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
```

## Disable THP
https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/

```bash:/etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    re='^[0-1]+$'
    if [[ $(cat ${thp_path}/khugepaged/defrag) =~ $re ]]
    then
      # RHEL 7
      echo 0  > ${thp_path}/khugepaged/defrag
    else
      # RHEL 6
      echo 'no' > ${thp_path}/khugepaged/defrag
    fi

    unset re
    unset thp_path
    ;;
esac
```

Non systemd!

## ulimit
https://docs.mongodb.com/manual/reference/ulimit/

- cent6/amazon

```/etc/security/limits.d/99-mongodb-nproc.conf
mongod     soft    nproc     64000
```

# Deploy cluster ver 3.2
https://docs.mongodb.com/v3.2/tutorial/deploy-shard-cluster/

## Build config server
```/etc/mongod.conf
replication:
  replSetName: replconfig

sharding:
  clusterRole: configsvr
```
Start mongod server.
And build Config Server Replica Set (maybe from ver 3.2)

```bash:
$ mongod
rs.initiate(
  {
    _id : "replconfig",
    members: [
      { _id : 0, host : "mongoc1:27017" },
      { _id : 1, host : "mongoc2:27017" },
      { _id : 2, host : "mongoc3:27017" }
    ]
  }
)
```

## Build ReplicaSet
__Shard-0 (sh00)__

```/etc/mongod.conf
replication:
  replSetName: sh00

sharding:
  clusterRole: shardsvr
```

```bash:
$ mongod
rs.initiate(
  {
    _id : "sh00",
    members: [
      { _id : 0, host : "mongod1:27017" },
      { _id : 1, host : "mongod2:27017" },
      { _id : 2, host : "mongod3:27017" }
    ]
  }
)
```

__Shard-1 (sh01)__
port 27117

```/etc/mongod-s1.conf
replication:
  replSetName: sh01

sharding:
  clusterRole: shardsvr
```

```bash:
$ mongod
rs.initiate(
  {
    _id : "sh01",
    members: [
      { _id : 0, host : "mongod1:27117" },
      { _id : 1, host : "mongod2:27117" },
      { _id : 2, host : "mongod3:27117" }
    ]
  }
)
```
## Add Shards

```
sh.addShard( "sh00/mongod1:27017")
sh.addShard( "sh01/mongod1:27117")
```

`[replName]/primaryNode:port`
We must set primaryNode!


## And more...
sh.enableSharding("<database>")

If the collection already contains data, you must create an index on the shard key using the db.collection.createIndex() method before using shardCollection().

If the collection is empty, MongoDB creates the index as part of sh.shardCollection().

The following operation shards the target collection:

`sh.shardCollection("<database>.<collection>", { <key> : <direction> } )`


# start on mac
Install via brew

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

# mongoimport
Support 2 type json
http://zaiste.net/2012/08/importing_json_into_mongodb/

```json:
{ name: "Widget 1", desc: "This is Widget 1" }
{ name: "Widget 2", desc: "This is Widget 2" }
```

or 

```json:
[
    { name: "Widget 1", desc: "This is Widget 1" },
    { name: "Widget 2", desc: "This is Widget 2" }
]
```
with `--jsonArray` option.