Friday, June 8, 2018

Upgrade Storage Engine - mmapv1 to wiredTiger – Outline


mmapv1 to wiredTiger

This Outline is based on MongoDB documentation.

Procedure

Shards

1) Shut down the secondary member.

# db.shutdownServer()

2) Prepare a data directory or delete all datas for the new mongod running with WiredTiger.

3) Start mongod with WiredTiger.

storage:
  engine: wiredTiger
(or)
delete the storage.engine field

4) Repeat the procedure for other replica set secondaries you wish to upgrade.

Config servers

1) Disable the balancer.

# sh.disableBalancer()

2) Shutdown the third config server

Shutdown the third config server to ensure read-only metadata. - 3rd config server

# db.shutdownServer()

3) Export the data of the second config server

Export the data of the second config server with mongodump. - 2nd config server

# mongodump --out <exportDataDestination>

4) Second config server

For the second config server, create a new data directory for use with WiredTiger. - 2nd config server

5) Stop the second config server. - 2nd config server

# db.shutdownServer()

6) Start the second config server mongod

Start the second config server mongod with the WiredTiger storage engine option. - 2nd config server - with wiredTiger option

7) Upload the exported data in second config server

Upload the exported data using mongorestore to the second config server. - 2nd config server - 2nd completed

# mongorestore <exportDataDestination>

8) Shut down the second config server

Shut down the second config server to ensure read-only metadata. - 2nd config server

# db.shutdownServer()

9) Restart the third config server

Restart the third config server to prepare for its upgrade. - 3rd config server - without wiredTiger option

10) Export the data of the third config server

Export the data of the third config server with mongodump. - 3rd config server

# mongodump --out <exportDataDestination>

11) Third Config Server

For the third config server, create a new data directory for use with WiredTiger. - 3rd config server

12) Stop the third config server

Stop the third config server. - 3rd config server

# db.shutdownServer()

13) Start the third config server

Start the third config server with the WiredTiger storage engine option. - 3rd config server - with wiredTiger option

14) Upload the exported data in third config server

Upload the exported data using mongorestore to the third config server. - 3rd config server

15) Export data of the first config server

Export data of the first config server with mongodump. - 1st config server

# mongodump --out <exportDataDestination>

16) First Config Server

For the first config server, create a new data directory for use with WiredTiger. - 1st config server

17) Stop the first config server

Stop the first config server. - 1st config server

# db.shutdownServer()

18) Start the first config server

Start the first config server with the WiredTiger storage engine option. - 1st config server

19) Upload the exported data in first config server

Upload the exported data using mongorestore to the first config server. - 1st config server

# mongorestore <exportDataDestination>

20) Restart the second config server

Restart the second config server to enable writes to the sharded cluster’s metadata. - 2nd config server - with wiredTiger option

21) Re-enable the balancer.

# sh.startBalancer()

Upgrade Config Servers to Replica Set – Implementation


SCCC to CSRS

Config Servers

tommy1 – 27018 – 1st
tommy2 – 27018 – 2nd
tommy3 – 27018 – 3rd

Mongos server

tommongo

1) Disable the balancer - tommongo

> login to tommongo server
> connect to mongos
> mongo admin --port 27017
# mongos> sh.getBalancerState()
true
# mongos> sh.stopBalancer() or
# mongos> sh.disableBalancer()
# mongos> sh.getBalancerState()
false

2) Connect a mongo shell to the first config server - tommy1

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
> Initiate the single member replica set
# rs.initiate( {
   _id: "CSRS",
   configsvr: true,
   version: 1,
   members: [ { _id: 0, host: "tommy1:27018" } ]
} )

3) Restart this first config server as a single member replica set - tommy1

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# db.shutdownServer()
check the process again
# ps aux | grep mongo
> Make the necessary changes in the config file
# vi /data/usr/mongodb_3.2.16/conf/mongod_config.conf
sharding:
   clusterRole: configsvr
   configsvrMode: sccc
replication:
   replSetName: CSRS
net:
   port: <port>
storage:
   dbPath: <path>
   engine: <storageEngine>
# wq!
Start the server
# /data/usr/mongodb_3.2.16/bin/mongod -f /data/usr/mongodb_3.2.16/conf/mongod_config.conf
check the process again
# ps aux | grep mongo

4) Start the new mongod instances to add to the replica set.

tommy2 – 2nd

login to tommy2 server
check the process
# ps aux | grep mongo
Start another mongo instance with new dbpath
> Make the necessary changes in the config file
# vi /data/usr/mongodb_3.2.16/conf/mongod_config.conf
sharding:
   clusterRole: configsvr
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
# wq!
Start the server
# /data/usr/mongodb_3.2.16/bin/mongod -f /data/usr/mongodb_3.2.16/conf/mongod_config.conf
check the process again
# ps aux | grep mongo 

tommy3 – 3rd

login to tommy3 server
check the process
# ps aux | grep mongo
Start another mongo instance with new dbpath
> Make the necessary changes in the config file
# vi /data/usr/mongodb_3.2.16/conf/mongod_config.conf
sharding:
   clusterRole: configsvr
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
# wq!
Start the server
# /data/usr/mongodb_3.2.16/bin/mongod -f /data/usr/mongodb_3.2.16/conf/mongod_config.conf
check the process again
# ps aux | grep mongo

5) Connect to first config server, add the new mongod instances as non-voting, priority 0 members

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# rs.add( { host: tommy2:27018, priority: 0, votes: 0 } )
# rs.add( { host: tommy3:27018, priority: 0, votes: 0 } )

6) Wait till new nodes have completed the initial sync and have reached SECONDARY state.

To check
login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# rs.status()

7) Shut down one of the other non-replica set config servers - tommy2 – 2nd

login to tommy2 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# db.shutdownServer()
check the process again
# ps aux | grep mongo

8) Reconfigure the replica set - tommy1 – 1st

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
var cfg = rs.conf();
cfg.members[0].priority = 1;
cfg.members[1].priority = 1;
cfg.members[2].priority = 1;
cfg.members[3].priority = 1;
cfg.members[0].votes = 1;
cfg.members[1].votes = 1;
cfg.members[2].votes = 1;
cfg.members[3].votes = 1;
rs.reconfig(cfg);

9) Step down the first config server  - tommy1 – 1st

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# rs.stepDown(600)

10) Shut down the first config server - tommy1 – 1st

login to tommy1 server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27018
# db.shutdownServer()
check the process again
# ps aux | grep mongo

11) Restart the first config server in config server replica set (CSRS) mode - tommy1 – 1st

> Make the necessary changes in the config file
# vi /data/usr/mongodb_3.2.16/conf/mongod_config.conf
sharding:
   clusterRole: configsvr
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
   engine: <storageEngine>
# wq!
Start the server
# /data/usr/mongodb_3.2.16/bin/mongod -f /data/usr/mongodb_3.2.16/conf/mongod_config.conf
check the process again
# ps aux | grep mongo

12) Restart mongos instances with updated config files - tommongo – mongos

login to tommongo server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27017
# db.shutdownServer()
check the process again
# ps aux | grep mongo
> Make the necessary changes in the config file
# vi /data/usr/mongodb_3.2.16/conf/mongos.conf
sharding:
  configDB: tommy1:27018,tommy2:27018,tommy3:27018

change to

sharding:
  configDB: csReplSet/tommy1:27018,tommy2:27018,tommy3:27018

# wq!
Start the server
# /data/usr/mongodb_3.2.16/bin/mongos -f /data/usr/mongodb_3.2.16/conf/mongos.conf
check the process again
# ps aux | grep mongo

13) Verify that the restarted mongos instances are aware of the protocol change- tommongo – mongos

login to tommongo server
check the process
# ps aux | grep mongo
connect to mongo
# mongo admin --port 27017
# use config
# db.mongos.find()
- The ping value for the mongos instances should indicate some time after the restart.

14) Shut down the remaining non-replica set config server.

15) Re-enable the balancer- tommongo – mongos

- login to tommongo server
- connect to mongo
# mongo --port 27017
# mongos> sh.getBalancerState()
false
# mongos> sh.startBalancer()
# mongos> sh.getBalancerState()
true




Upgrade Config Servers to Replica Set – Outline


SCCC to CSRS

This Outline is based on MongoDB documentation.

Prerequisites 
  • All binaries in the sharded clusters must be at least version 3.2.4
  • The existing config servers must be in sync 

Procedure

1) Disable the balancer

connect to mongos

# sh.getBalancerState()
# sh.stopBalancer() or
# sh.disableBalancer()

2) Connect a mongo shell to the first config server

Connect a mongo shell to the first config server listed in the configDB setting of the mongos and run rs.initiate() to initiate the single member replica set.

rs.initiate( {
   _id: "csReplSet",
   configsvr: true,
   version: 1,
   members: [ { _id: 0, host: "<host>:<port>" } ]
} )

3) Restart this config server as a single member replica set

sharding:
   clusterRole: configsvr
   configsvrMode: sccc
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
   engine: <storageEngine>

4) Start the new mongod instances to add to the replica set. 
  • These instances must use the WiredTiger storage engine.
  • Starting in 3.2, the default storage engine is WiredTiger for new mongod instances with new data paths.
  • Do not add existing config servers to the replica set.
  • Use new dbpaths for the new instances. 

sharding:
   clusterRole: configsvr
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
  
5) Add the new mongod instances

Using the mongo shell connected to the replica set config server, add the new mongod instances as non-voting, priority 0 members:

rs.add( { host: <host:port>, priority: 0, votes: 0 } )
rs.add( { host: <host:port>, priority: 0, votes: 0 } )

6) Ensure that the new nodes have completed the initial sync 
  • Once all the new members have been added as non-voting, priority 0 members, ensure that the new nodes have completed the initial sync and have reached SECONDARY state. 
  • To check the state of the replica set members, run rs.status() in the mongo shell: 

# rs.status()

7) Shut down one of the other non-replica set config servers 
  • Shut down one of the other non-replica set config servers; i.e. either the second and third config server listed in the configDB setting of the mongos.
  • At this point the config servers will go read-only, meaning certain operations - such as creating and dropping databases and sharded collections - will not be available. 

8) Reconfigure the replica set

Reconfigure the replica set to allow all members to vote and have default priority of 1.

var cfg = rs.conf();

cfg.members[0].priority = 1;
cfg.members[1].priority = 1;
cfg.members[2].priority = 1;
cfg.members[3].priority = 1;
cfg.members[0].votes = 1;
cfg.members[1].votes = 1;
cfg.members[2].votes = 1;
cfg.members[3].votes = 1;

rs.reconfig(cfg);

9) Step down the first config server

Step down the first config server, i.e. the server started with --configsvrMode=sccc.

# rs.stepDown(600)

10) Shut down the first config server.

# use admin
# db.shutdownServer()

11) Restart the first config server

Restart the first config server in config server replica set (CSRS) mode; i.e. restart without the --configsvrMode=sccc option:

sharding:
   clusterRole: configsvr
replication:
   replSetName: csReplSet
net:
   port: <port>
storage:
   dbPath: <path>
   engine: <storageEngine>


12) Restart mongos instances 
  • Restart mongos instances with updated --configdb or sharding.configDB setting.
  • For the updated --configdb or sharding.configDB setting, specify the replica set name for the config servers and the members in the replica set. 

sharding:
  configDB: <rsconfigsver1:port1>,<rsconfigsver2:port2>,<rsconfigsver3:port3>

change to

sharding:
  configDB: csReplSet/ <rsconfigsver1:port1>,<rsconfigsver2:port2>,<rsconfigsver3:port3> 

13) Verify that the restarted mongos instances 
  • Verify that the restarted mongos instances are aware of the protocol change. 
  • Connect a mongo shell to a mongos instance and check the mongos collection in the config database: 

# use config
# db.mongos.find()

The ping value for the mongos instances should indicate some time after the restart.

14) Connect a mongo shell to the current primary 
  • If the first config server uses the MMAPv1 storage engine, remove the member from the replica set. Connect a mongo shell to the current primary and use rs.remove():
  • Only if the config server uses the MMAPv1 storage engine. 

# rs.remove("<hostname>:<port>")

15) Shut down the remaining non-replica set config server.

16) Re-enable the balancer

Connect to mongo

# sh.getBalancerState()
# sh.startBalancer()


Tuesday, May 29, 2018

Security Basic Setup – Sharded Cluster


Deploy Sharded Cluster with Keyfile Access Control

Overview

This example involves creating a new sharded cluster that consists of a mongos, the config server replica set, and two shard replica sets with Keyfile Access Control

Servers:

Config Servers:

Server1 - 27019
Server2 - 27019
Server3 - 27019

Shard 1:

Server4 - 27019
Server5 - 27019
Server6 - 27020

Shard 2:

Server7 - 27018
Server8 - 27018
Server9 - 27018

Mongos:

Server10 – 27017

Create the Keyfile

# openssl rand -base64 756 > /data/sand/keyfile
# chmod 400 /data/sand/keyfile

[tommy@Server1 ~]$ openssl rand -base64 756 > /data/sand/keyfile
[tommy@Server1 ~]$ chmod 400 /data/sand/keyfile
[tommy@Server1 ~]$ cat /data/sand/keyfile
icUb7bOy67wUIwoYdJCvOqK+Dbzyc61NEGMMm6Dcus/UrcIttN+zdKPzc1ZW9jjE
lz3sCXEP6RykLUrfELABTNzP/SrRd24pdlxi+YG3ueOT5MQj8Xu9CBu9WWBC3tWW
XQGTKWH6FFjk6QHj8sqsTSYpz2FXyQ3M+fIDQW08yw5BBnRKH20vmOHnPs7APZGY
5fa7RieAik1eBSOTjvAScA3Z2kpttsxH/xtJ7zLOljts/SbW1VFfajLmoBpUTHbX
siuVqFZyka1THLVIP5Dp0FWB28oRsV6geJ8pfn0TfjS6AmqFdF/daa4WEu4js8qJ
Yah8+2giwxbXpJ+KQgWC2lBrfrTq5eUrgP4C13w1o63ZcRZiOMIERKPAb41LtiKF
KST272HoSEMvwMgp2xyFMS2b0HUQ7xV/cCBp7Xyv2E+0UojpURd0nLN2pw1lk+NW
gDzYQ21mopz2PxR3/k+zA5WSUOQakhMFsHIptVIY5DrYWrD1QZsZssV8YH7UkZF2
LYW9t3FNFkKDDz4RKJpKZwoNomR8FgI2KDA0Ej6RXwMPhSNqAkObCaWp+FNnRYvQ
6GwVPs5ugSrVF06vdzcAUTQW4zWzpi0CWiU2T30ugDoQ5CuHyKDcRz7mMMEYLBpJ
ApXZMSiJrI3E4IvNxhyMQyo+7tukVl+lxDIIFq28ZATAnmcy9X/0GNqxJcL5Wcc4
wYZVZPGqdjVC6oY1dwOxjENo702e5BCX2O0F8PUNx3kLLpYnR50nuyoAJ7HI8r85
LNtxmlwR5da5iX56JIYZYSdei6I3QVVWUvhXz3VwahmFfNRVF8Vu2WYXz60/iMvu
u3Ice35PhpGKitqBRuVeIL+gyA0Acsx6HYc0+uf2AXV4Hjidvervnm67s3nofKmx
Eb0xRanRXRS/QmUeOdIwYkZqqDzQpc2a31mJBkfJA74behMoHv+MM9ZBy8dqTceT
/RplhMYY8bpjaIMrTwWLdagKpWZOLCUkXiGrrvEs6ibZ4qL7
[tommy@Server1 ~]$

Distribute the Keyfile

Copy the key files to all the servers in the location /data/sand/keyfile

# vi /data/sand/keyfile
# :wq!

Config Files:

Config Servers:

net:
  port: 27019
processManagement:
  fork: true
sharding:
  clusterRole: configsvr
replication:
  replSetName: csrs
security:
  keyFile: /data/sand/keyfile
storage:
  dbPath: /data/sand/data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/sand/logs/mongod.log

Shard Servers:

net:
  port: 27019
processManagement:
  fork: true
sharding:
  clusterRole: shardsvr
replication:
  replSetName: rs_0
security:
  keyFile: /data/sand/keyfile
storage:
  dbPath: /data/sand/data
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /data/sand/logs/mongod.log

Mongos:

net:
  port: 27017
processManagement:
  fork: true
sharding:
  configDB: csrs/Server1:27019,Server2:27019,Server3:27019
security:
  keyFile: /data/sand/keyfile
systemLog:
  destination: file
  logAppend: true
  logRotate: rename
  path: /home/tommy/logs/mongos.log

To start the server, commands:

Shard Servers
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
Config Servers
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
Mongos Servers
/home/tommy/mongodb_3.4.10/bin/mongos -f /home/tommy/conf/mongos.conf

Deploy Sharded Cluster with security

1. Create the Config Server Replica Set

Start each member of the config server replica set

Configuration Options

sharding:
  clusterRole: configsvr
replication:
  replSetName: csrs
security:
  keyFile: /data/sand/keyfile

Config Servers:

Server1 - 27019
Server2 - 27019
Server3 - 27019

Start all the servers by using the command,

Server1
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf

[tommy@Server1 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 56110
child process started successfully, parent exiting
[tommy@Server1 ~]$

Server2
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf

[tommy@Server2  ~]$/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 63252
child process started successfully, parent exiting
[tommy@Server2  ~]$

Server3
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf

[tommy@Server3  ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod_config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 58635
child process started successfully, parent exiting
[tommy@Server3  ~]$

Connect to one of the config servers

Server1
mongo --port 27019

[tommy@Server1 ~]$ /data/sand/mongodb_3.4.10/bin/mongo --port 27019
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.10
> 

Initiate and add members to the Config Sever replica set

# rs.initiate()

rs.initiate(
  {
    _id: "csrs",
    configsvr: true,
    members: [
      { _id : 0, host : " Server1:27019"},
      { _id : 1, host : " Server2:27019"},
      { _id : 2, host : " Server3:27019"}
    ]
  }
)

> rs.initiate(
...   {
...     _id: "csrs",
...     configsvr: true,
...     members: [
...       { _id : 0, host : " Server1:27019"},
...       { _id : 1, host : " Server2:27019"},
...       { _id : 2, host : " Server3:27019"}
...     ]
...   }
... )
{ "ok" : 1 }
csrs:SECONDARY>
csrs:SECONDARY>
csrs:PRIMARY>

Once the config server replica set (CSRS) is initiated and up, proceed to creating the shard replica sets.

2. Create the Shard Replica Sets

Start each member of the shard replica set.

Configuration Options

sharding:
  clusterRole: shardsvr
replication:
  replSetName: rs_0
security:
  keyFile: /data/sand/keyfile

Shard 1:

Server4 - 27019
Server5 - 27019
Server6 – 27020

Start all the servers in Shard 1 by using the command,

Server4
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server4 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 12833
child process started successfully, parent exiting
[tommy@Server4 ~]$

Server5
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server5 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 24227
child process started successfully, parent exiting
[tommy@Server5 ~]$

Server6
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server6 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 39854
child process started successfully, parent exiting
[tommy@Server6 ~]$

Connect to a member of the shard replica set.

Server4
# mongo –port 27019

[tommy@Server4 ~]$ /data/sand/mongodb_3.4.10/bin/mongo --port 27019
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.4.10
> 

Initiate and add members to the replica set

# rs.initiate()

# rs.initiate(
  {
    _id: "rs_0",
    members: [
      { _id : 0, host : " Server4:27019"},
      { _id : 1, host : " Server5:27019"},
      { _id : 2, host : " Server6:27020"}
    ]
  }
)

> rs.initiate(
...   {
...     _id: "rs_0",
...     members: [
...       { _id : 0, host : " Server4:27019"},
...       { _id : 1, host : " Server5:27019"},
...       { _id : 2, host : " Server6:27020"}
...     ]
...   }
... )
{ "ok" : 1 }
rs_0:SECONDARY>
rs_0:PRIMARY>
rs_0:PRIMARY>

Shard 2:

Server7 - 27018
Server8 - 27018
Server9 – 27018

Configuration Options

sharding:
  clusterRole: shardsvr
replication:
  replSetName: rs_1
security:
  keyFile: /data/sand/keyfile

Start all the servers in Shard 2 by using the command,

Server7
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server7 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 13304
child process started successfully, parent exiting
[tommy@Server7 ~]$

Server8
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server8 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 54249
child process started successfully, parent exiting
[tommy@Server8 ~]$

Server9
/data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf

[tommy@Server9 ~]$ /data/sand/mongodb_3.4.10/bin/mongod -f /data/sand/conf/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 47531
child process started successfully, parent exiting
[tommy@Server9 ~]$

Connect to a member of the shard replica set.

Server7
# /data/sand/mongodb_3.4.10/bin/mongo --port 27018

[tommy@Server7 ~]$ /data/sand/mongodb_3.4.10/bin/mongo --port 27018
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.4.10
> 

Initiate and add members to the replica set

# rs.initiate()

# rs.initiate(
  {
    _id: "rs_1",
    members: [
      { _id : 0, host : "Server7:27018"},
      { _id : 1, host : "Server8:27018"},
      { _id : 2, host : "Server9:27018"}
    ]
  }
)

> rs.initiate(
...   {
...     _id: "rs_1",
...     members: [
...       { _id : 0, host : "Server7:27018"},
...       { _id : 1, host : "Server8:27018"},
...       { _id : 2, host : "Server9:27018"}
...     ]
...   }
... )
{ "ok" : 1 }
rs_1:SECONDARY>
rs_1:PRIMARY>
rs_1:PRIMARY>

3. Create the shard-local user administrator (optional)

Connect to all shard primaries and create a admin user,

# admin = db.getSiblingDB("admin")
# admin.createUser(
  {
    user: "fred",
    pwd: "changeme1",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

rs_1:PRIMARY> admin = db.getSiblingDB("admin")
admin
rs_1:PRIMARY> admin.createUser(
...   {
...     user: "fred",
...     pwd: "changeme1",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "fred",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
rs_1:PRIMARY>

4. Authenticate as the shard-local user administrator (optional).

Authenticate to the admin database.

# db.getSiblingDB("admin").auth("fred", "changeme1" )
# /data/sand/mongodb_3.4.10/bin/mongo --port 27018 -u "fred" -p "changeme1" --authenticationDatabase "admin"

rs_1:PRIMARY> db.getSiblingDB("admin").auth("fred", "changeme1" )
1
rs_1:PRIMARY> ^C
bye
[tommy@Server7 ~]$ /data/sand/mongodb_3.4.10/bin/mongo --port 27018 -u "fred" -p "changeme1" --authenticationDatabase "admin"
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.4.10
rs_1:PRIMARY>

5. Create the shard-local cluster administrator (optional).

Create a cluser admin users if required,

# db.getSiblingDB("admin").createUser(
  {
    "user" : "ravi",
    "pwd" : "changeme2",
    roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
  }
)

rs_1:PRIMARY> db.getSiblingDB("admin").createUser(
...   {
...     "user" : "ravi",
...     "pwd" : "changeme2",
...     roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "ravi",
        "roles" : [
                {
                        "role" : "clusterAdmin",
                        "db" : "admin"
                }
        ]
}
rs_1:PRIMARY>

6. Connect a mongos to the Sharded Cluster

Connect a mongos to the cluster

Configuration Options

sharding:
  configDB: csrs/Server1:27019,Server2:27019,Server3:27019
security:
  keyFile: /data/sand/keyfile

Mongos:

Server10 - 27017

Start the mongos server using the command,

Server10
# /home/tommy/mongodb_3.4.10/bin/mongos -f /home/tommy/conf/mongos.conf

[tommy@Server10 ~]$ /home/tommy/mongodb_3.4.10/bin/mongos -f /home/tommy/conf/mongos.conf
about to fork child process, waiting until server is ready for connections.
forked process: 47709
child process started successfully, parent exiting
[tommy@Server10 ~]$

Connect to the mongos.

# /home/tommy/mongodb_3.4.10/bin/mongo

[tommy@Server10 ~]$ /home/tommy/mongodb_3.4.10/bin/mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
mongos>

Create the user administrator.

Create a cluster level user admin,

# admin = db.getSiblingDB("admin")
# admin.createUser(
  {
    user: "fred",
    pwd: "changeme1",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

mongos> admin = db.getSiblingDB("admin")
admin
mongos> admin.createUser(
...   {
...     user: "fred",
...     pwd: "changeme1",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "fred",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}
mongos>

Create Administrative User for Cluster Management

To have replication and sharding operations, create a cluster admin user if required,

# db.getSiblingDB("admin").createUser(
  {
    "user" : "ravi",
    "pwd" : "changeme2",
    roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
  }
)

[tommy@Server10 ~]$ /home/tommy/mongodb_3.4.10/bin/mongo -u "fred" -p "changeme1" --authenticationDatabase "admin"
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
mongos> db.getSiblingDB("admin").createUser(
...   {
...     "user" : "ravi",
...     "pwd" : "changeme2",
...     roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
...   }
... )
Successfully added user: {
        "user" : "ravi",
        "roles" : [
                {
                        "role" : "clusterAdmin",
                        "db" : "admin"
                }
        ]
}
mongos>

Create additional users (Optional).

Create users to allow clients to connect and access the sharded cluster, if required.

7. Add Shards to the Cluster

Login as cluser admin user to add shards
Use the sh.addShard() method to add each shard to the cluster
If the shard is a replica set, specify the name of the replica set and specify a member of the set.

# sh.addShard("rs_0/Server4:27019")

[tommy@Server10 ~]$ /home/tommy/mongodb_3.4.10/bin/mongo -u "ravi" -p "changeme2" --authenticationDatabase "admin"
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
mongos> sh.addShard("rs_0/Server4:27019")
{ "shardAdded" : "rs_0", "ok" : 1 }
mongos>

# sh.addShard("rs_1/Server7:27018")

mongos> sh.addShard("rs_1/Server7:27018")
{ "shardAdded" : "rs_1", "ok" : 1 }
mongos>

To check the status of the Sharded Cluser

# sh.status()

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("5b0c78a7f436df87182714f3")
  }
  shards:
        {  "_id" : "rs_0",  "host" : "rs_0/Server4:27019,Server5:27019,Server6:27020",  "state" : 1 }
        {  "_id" : "rs_1",  "host" : "rs_1/Server9:27018,Server7:27018,Server8:27018",  "state" : 1 }
  active mongoses:
        "3.4.10" : 1
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
NaN
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours:
                No recent migrations
  databases:

mongos>

Done…