Construction of MongoDB Fragmented Cluster

Keywords: Database MongoDB Fragment

  • System Architecture Diagram
  • Prepare 12 machines
    10.202.12.178 (Main 1)
    10.202.12.179 (spare 1)
    10.202.12.180 (Main 2)
    10.202.12.181 (spare 2)
    10.202.12.182 (Main 3)
    10.202.12.183 (spare 3)
    10.202.12.184 (Main 4)
    10.202.12.185 (preparation 4)
    10.202.12.186 (Arbitration 1, Routing 1, Configuration 1)
    10.202.12.189 (Arbitration 2, Routing 2, Configuration 2)
    10.202.12.192 (Arbitration 3, Routing 3, Configuration 3)
    10.202.12.194 (Arbitration 4)
  • Create mongodb fragmentation corresponding test folders on each machine
Create folders on 10.202.12.178 
mkdir -p /app/mongo/shard1/master/{data,log,conf}
Create folders on 10.202.12.179 
mkdir -p /app/mongo/shard1/slave/{data,log,conf}

Create folders on 10.202.12.180 
mkdir -p /app/mongo/shard2/master/{data,log,conf}
Create folders on 10.202.12.181 
mkdir -p /app/mongo/shard2/slave/{data,log,conf}

Create folders on 10.202.12.182 
mkdir -p /app/mongo/shard3/master/{data,log,conf}
Create folders on 10.202.12.183 
mkdir -p /app/mongo/shard3/slave/{data,log,conf}

Create folders on 10.202.12.184 
mkdir -p /app/mongo/shard4/master/{data,log,conf}
Create folders on 10.202.12.185 
mkdir -p /app/mongo/shard4/slave/{data,log,conf}

Create folders on 10.202.12.186, 10.202.12.189 and 10.202.12.192 respectively
mkdir -p /app/mongo/shard/mongos/{data,log,conf}
mkdir -p /app/mongo/shard/config/{data,log,conf}
mkdir -p /app/mongo/shard/arbiter/{data,log,conf}

Create folders on 10.202.12.194
mkdir -p /app/mongo/shard/arbiter/{data,log,conf}
  • Start configuration servers on 10.202.12.186, 10.202.12.189 and 10.202.12.192 respectively
./mongod  --configsvr  --dbpath  /app/mongo/shard/config/data 
--port 27019  --logpath  /app/mongo/shard/config/log/config.log  --fork
  • Start mongos servers on 10.202.12.186, 10.202.12.189 and 10.202.12.192 respectively
./mongos  --configdb
10.202.12.186:27019,10.202.12.189:27019,10.202.12.192:27019
--port  27018   --logpath   /app/mongo/shard/mongos/log/mongos.log  --fork
  • Configure replica sets for each fragment
vi /app/mongo/shard1/master/conf/mongod.conf(10.202.12.178)
#Data directory
dbpath=/app/mongo/shard1/master/data
#Log directory
logpath=/app/mongo/shard1/master/log/mongod.log
#Logs are added as additions
logappend=true
#Create folders for each database according to its name
directoryperdb=true 
# Open Background Process Running
fork = true
#IP
bind_ip=10.202.12.178
#port
port = 27017
#Copy Set Name
replSet=shard1  
vi /app/mongo/shard1/slave/conf/mongod.conf(10.202.12.179)
#Data directory
dbpath=/app/mongo/shard1/slave/data
#Log directory
logpath=/app/mongo/shard1/slave/log/mongod.log
#Logs are added as additions
logappend=true
#Create folders for each database according to its name
directoryperdb=true 
# Open Background Process Running
fork = true
#IP
bind_ip=10.202.12.179
#port
port = 27017
#Copy Set Name
replSet=shard1
vi /app/mongo/shard/arbiter/conf/mongod.conf(10.202.12.186)
#Data directory
dbpath=/app/mongo/shard/arbiter/data
#Log directory
logpath=/app/mongo/shard/arbiter/log/mongod.log
#Logs are added as additions
logappend=true
#Create folders for each database according to its name
directoryperdb=true 
# Open Background Process Running
fork = true
#IP
bind_ip=10.202.12.186
#port
port = 27017
#Copy Set Name
replSet=shard1

The other three slices have similar configurations

  • Start mongodb on each machine
 ./mongod -f /app/mongo/shard1/master/conf/mongod.conf
Land10.202.12.178,Connect mongodb
#Setting up the first fragmented replica set
./mongo  10.202.12.178
#Using admin database
use admin
#Define replica set configuration
config = { _id:"shard1", members:[
                     {_id:0,host:"10.202.12.178:27017"},
                     {_id:1,host:"10.202.12.179:27017"},
                     {_id:2,host:"10.202.12.186:27017",arbiterOnly:true}
                ]
         }
#Initialize replica set configuration
rs.initiate(config);

The other three pieces are similar.

  • Configuring Routing Server
    At present, mongodb configuration server, routing server and each piecewise server have been set up. However, the application connecting to mongos routing server can not use the piecewise mechanism. It also needs to set up the piecewise configuration in the program to make the piecewise effective.
    # Connect to mongos
./mongo  10.202.12.186:27018
#Using admin database
user admin
#Series Routing Server and Distribution Replica Set 1
db.runCommand( { addshard : "shard1/10.202.12.178:27017,10.202.12.179:27017,10.202.12.186:27017"});
#Series Routing Server and Distribution Copy Set 2
db.runCommand( { addshard : "shard2/10.202.12.180:27017,10.202.12.181:27017,10.202.12.189:27017"});
#Series Routing Server and Distribution Copy Set 3
db.runCommand( { addshard : "shard3/10.202.12.182:27017,10.202.12.183:27017,10.202.12.192:27017"});
#Series Routing Server and Distribution Replica Set 4
db.runCommand( { addshard : "shard4/10.202.12.184:27017,10.202.12.185:27017,10.202.12.194:27017"});
#View the configuration of the sharding server
db.runCommand( { listshards : 1 } );
  • Configuration fragmentation rules
    At present, configuration services, routing services, fragmentation services and replica set services are all connected in series, but our purpose is to insert data, which can be automatically fragmented and connected to mongos, ready for the specified database and fragmentation of the specified collection to take effect.
Specify countly database fragmentation to take effect
db.runCommand( { enablesharding :"countly"});
Specify the collection and key to be fragmented in the database
db.runCommand( { shardcollection : "countly.user",key : {id: 1} } )

Posted by dibyendrah on Mon, 25 Mar 2019 14:03:28 -0700