Friday, December 16, 2016

Mongo 3.4 installation


  1. https://www.mongodb.com/collateral/mongodb-performance-best-practices
  2. https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-2?_ga=1.58772142.2085632332.1453495037
  3. https://blog.engineyard.com/2011/mongodb-best-practices
  4. http://highscalability.com/blog/2014/3/5/10-things-you-should-know-about-running-mongodb-at-scale.html
  5. https://www.mongodb.com/mongodb-architecture
  6. https://www.mongodb.com/collateral/rdbms-mongodb-migration-guide
  7. https://dzone.com/articles/mongodb-atlas-best-practices-part-1
  8. https://www.mongodb.com/blog/post/whats-new-mongodb-30-part-3-performance-efficiency-gains-new-storage-architecture
  9. https://www.mongodb.com/blog/post/tiered-storage-models-in-mongodb-optimizing


Someone asked me how to setup a replica set and shards on mongo, and its easy. I decided to do it with mongo 3.4 which is a lot more sensitive in the configuration files.

I must have deleted the steps for mongo 3.0 installation. This one will use 3 computers, on different ports, 2 data replica sets, 1 config set, 1 mongos set. So if one server goes down its okay. the config set are in a repica too. And the 2 data replica sets will be sharded.

So 3 times, install mongo 3.4 as from the website, using rpm or ubuntu or debian packages.
Install configs for mongo data replica set 1. at /etc/mongod1.conf

dbpath=/var/lib/mongodb1
logpath=/var/log/mongodb/mongod1.log
logappend=true
port = 27117
replSet=r1


directoryperdb = 1
profile = 1
smallfiles = 1
oplogSize = 16
noprealloc = 1
#not sure if this should be 16
nssize = 1
shardsvr=1
-----------------------------------------------------------
one per server, make this config file at /etc/mongo2.conf

dbpath=/var/lib/mongodb2
logpath=/var/log/mongodb/mongod2.log
logappend=true
port = 27118
replSet=r2


directoryperdb = 1
profile = 1
smallfiles = 1
oplogSize = 16
noprealloc = 1
#not sure if this should be 16
nssize = 1
shardsvr=1
-------------------------------------------
one per server, the config file for the config servers at /etc/mongod_config1.conf

dbpath=/var/lib/mongodb_config1
logpath=/var/log/mongodb/mongod_config1.log
logappend=true
#bind_ip = 127.0.0.1
port = 27217
replSet=c1

configsvr = 1
----------------------------------------------------

Now  commands to make the directories and stuff

# cd /var/lib; rm -rf mongo*

mkdir -p /var/log/mongodb/
mkdir -p /var/lib/mongodb_config1
mkdir -p /var/lib/mongodb1
mkdir -p /var/lib/mongodb2
chown -R mongodb.mongodb /var/log/mongodb /var/lib/mongo*

------------------------------------------------------------
Now the rest of the files are in /etc/init

The mongod start files, mongo config, and mongos.
These are just modifications to the files that come with normal mongo installation. 
------------------------------------------

# monogdb1
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 32000 32000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
  DAEMONUSER=${DAEMONUSER:-mongodb}
  if [ ! -d /var/lib/mongodb1 ]; then
    mkdir -p /var/lib/mongodb1 && chown mongodb:mongodb /var/lib/mongodb1
  fi
  if [ ! -d /var/log/mongodb1 ]; then
    mkdir -p /var/log/mongodb1 && chown mongodb:mongodb /var/log/mongodb1
  fi
  touch /var/run/mongodb1.pid
  chown $DAEMONUSER /var/run/mongodb1.pid
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGOD="yes"
  CONF=/etc/mongod1.conf
  DAEMON=/usr/bin/mongod
  DAEMONUSER=${DAEMONUSER:-mongodb}

  if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi

  # Handle NUMA access to CPUs (SERVER-3574)
  # This verifies the existence of numactl as well as testing that the command works
  NUMACTL_ARGS="--interleave=all"
  if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  then
    NUMACTL="$(which numactl) -- $NUMACTL_ARGS"
    DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  else
    NUMACTL=""
    DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  fi

  if [ "x$ENABLE_MONGOD" = "xyes" ]
  then
    exec start-stop-daemon --start \
        --chuid $DAEMONUSER \
        --pidfile /var/run/mongodb1.pid \
        --make-pidfile \
        --exec $NUMACTL $DAEMON $DAEMON_OPTS
  fi
end script
------------------------------------------------------------
# monogdb2
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 32000 32000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
  DAEMONUSER=${DAEMONUSER:-mongodb}
  if [ ! -d /var/lib/mongodb2 ]; then
    mkdir -p /var/lib/mongodb2 && chown mongodb:mongodb /var/lib/mongodb2
  fi
  if [ ! -d /var/log/mongodb2 ]; then
    mkdir -p /var/log/mongodb2 && chown mongodb:mongodb /var/log/mongodb2
  fi
  touch /var/run/mongodb2.pid
  chown $DAEMONUSER /var/run/mongodb2.pid
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGOD="yes"
  CONF=/etc/mongod2.conf
  DAEMON=/usr/bin/mongod
  DAEMONUSER=${DAEMONUSER:-mongodb}

  if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi

  # Handle NUMA access to CPUs (SERVER-3574)
  # This verifies the existence of numactl as well as testing that the command works
  NUMACTL_ARGS="--interleave=all"
  if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  then
    NUMACTL="$(which numactl) -- $NUMACTL_ARGS"
    DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  else
    NUMACTL=""
    DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  fi

  if [ "x$ENABLE_MONGOD" = "xyes" ]
  then
    exec start-stop-daemon --start \
        --chuid $DAEMONUSER \
        --pidfile /var/run/mongodb2.pid \
        --make-pidfile \
        --exec $NUMACTL $DAEMON $DAEMON_OPTS
  fi
end script

#---------------------------
#mongo config
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 32000 32000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
  DAEMONUSER=${DAEMONUSER:-mongodb}
  if [ ! -d /var/lib/mongodb_config1 ]; then
    mkdir -p /var/lib/mongodb_config1 && chown mongodb:mongodb /var/lib/mongodb_config1
  fi
  if [ ! -d /var/log/mongodb ]; then
    mkdir -p /var/log/mongodb && chown mongodb:mongodb /var/log/mongodb
  fi
  touch /var/run/mongodb_config1.pid
  chown $DAEMONUSER /var/run/mongodb_config1.pid
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGOD="yes"
  CONF=/etc/mongod_config1.conf
  DAEMON=/usr/bin/mongod
  DAEMONUSER=${DAEMONUSER:-mongodb}

  if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi

  # Handle NUMA access to CPUs (SERVER-3574)
  # This verifies the existence of numactl as well as testing that the command works
  NUMACTL_ARGS="--interleave=all"
  if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  then
    NUMACTL="$(which numactl) -- $NUMACTL_ARGS"
    DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  else
    NUMACTL=""
    DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  fi

  if [ "x$ENABLE_MONGOD" = "xyes" ]
  then
    exec start-stop-daemon --start \
        --chuid $DAEMONUSER \
        --pidfile /var/run/mongodb_config1.pid \
        --make-pidfile \
        --exec $NUMACTL $DAEMON $DAEMON_OPTS
  fi
end script

#---------------------------------------------------------------------
# mongos
# Change [IP] to your 3 ip addresses for the mongo config servers with ports, 
limit fsize unlimited unlimited
limit cpu unlimited unlimited
limit as unlimited unlimited
limit nofile 64000 64000
limit rss unlimited unlimited
limit nproc 32000 32000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
  DAEMONUSER=${DAEMONUSER:-mongodb}
  touch /var/run/mongos1.pid
  chown $DAEMONUSER /var/run/mongos1.pid
end script

start on runlevel [2345]
stop on runlevel [06]


script
  CONFIG=" c1/[IP]:27217,[IP]:27217,[IP]:27217"
  DAEMON=/usr/bin/mongos
  DAEMONUSER=${DAEMONUSER:-mongodb}

  start-stop-daemon --start \
        --chuid $DAEMONUSER \
        --pidfile /var/run/mongos1.pid \
        --make-pidfile \
        --exec $DAEMON -- --configdb $CONFIG --port 27017  --logpath  /var/log/mongodb/mongos1.log

end script


#-----------------------------------


0. Copy all of it to the 3 servers.
1. Setup replica set  r1.
2. Setup replica set  r2
3. Setup config servers in replica set.
4. Setup mongos --- each server should have a mongos.

#--------------------------------------------------------------------------
On all three servers:
1. start mongodb1
2. start mongodb1
3. start mongodb_config1

#------------------------------
1. connect to mongodb1: mongo --port  27117
2. Setup replication
rs.initiate(
   {
      _id: "r1",
      version: 1,
      members: [
         { _id: 0, host : "IP1:27117" },
         { _id: 1, host : "IP2:27117" },
         { _id: 2, host : "IP3:27117" }
      ]
   }
)
3. After a minute it should make itself primary. Do rs.config() to see if the other servers connected.

#-------------------------------------------------------
4. connect to mongodb1: mongo --port  27118
5. Setup replication
rs.initiate(
   {
      _id: "r2",
      version: 1,
      members: [
         { _id: 0, host : "IP1:27118" },
         { _id: 1, host : "IP2:27118" },
         { _id: 2, host : "IP3:27118" }
      ]
   }
)
6. After a minute it should make itself primary. Do rs.config() to see if the other servers connected.

#-----------------------------------------------------
# now the config servers.
7. connect to mongodb1: mongo --port  27118
8. Setup replication
rs.initiate(
   {
      _id: "r2",
      version: 1,
      members: [
         { _id: 0, host : "IP1:27217" },
         { _id: 1, host : "IP2:27217" },
         { _id: 2, host : "IP3:27217" }
      ]
   }
)


9. After a minute it should make itself primary. Do rs.config() to see if the other servers connected.

#---------------------------------------------------------------
# At this point, I would connet to each replica set, 3 of them, do rs.conf() and make sure everything in running. Also, ps auwx | grep mongo helps to see if you have 3 mongo processes running. Check the log files too.

1. Now setup mongos on each server the same way.
2. You should had already installed the mongos init files, just start it.
3. start mongos
4. connect to mongo: mongo
5. Now setup the sharding. If you had problems connecting to mongos, there should be a log file for it.

#---------------------------------------------
1. Connected to mongos using mongo client, do the following commands.

sh.addShard( "r1/[IP]:27117")
sh.addShard( "r2/[IP]:27118")

use testsharddb1
db.testsharddoc1.insert( { _id: 11, junk : "aaa" } )
# You should have been able to insert and make a document. Now look at the sharding setup.
sh.status()
# You should see our database is not sharded yet.
# It should say somewhere: {  "_id" : "testsharddb1",  "primary" : "r2",  "partitioned" : false }

sh.enableSharding("testsharddb1")

# Put we have to specify documents to shard too.
sh.shardCollection("testsharddb1.testsharddoc1", { _id : 1 } )

# Nowif you so sh.status() you should see the database and document sharded and the document should have a range for the key on one of the replica sets. 

No comments:

Post a Comment