Mongodb Cluster Construction under linux: Fragmentation + Replica Set

Posted by ja_blackburn on Wed, 15 May 2019 03:03:21 +0200

Three servers 192.168.1.40/41/42

Installation package mongodb-linux-x86_64-amazon 2-4.0.1.tgz

Service planning
Server 40 Server 41 Server 42
 mongos  mongos  mongos
 config server  config server  config server
Shard server 1 master node Shard server 1 secondary node Shard server 1 arbitration
Shard server 2 arbitration Shard server 2 master node Shard server 2 secondary node
Shard server 3 secondary node Shard server 3 arbitration Shard server 3 master node

 

 

 

 

 

 

Port allocation:

mongos: 28000

config:28001

shard1:28011

shard2:28012

shard3:28013

 

Main modules and configuration files

1. config server configuration server

 

vi /usr/local/mongodb/conf/config.conf
40 Server Profile
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend = true bind_ip = 192.168.29.40 port = 28001 fork = true #Running MongoDB as a daemon to create server processes #declare this is a config db of a cluster; configsvr = true #Copy Set Name replSet=configs #Set the maximum number of connections maxConns=20000

41 Server Profile
pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend = true bind_ip = 192.168.29.41 port = 28001 fork = true #Running MongoDB as a daemon to create server processes #declare this is a config db of a cluster; configsvr = true #Copy Set Name replSet=configs #Set the maximum number of connections maxConns=20000
42 Server Profile

pidfilepath = /usr/local/mongodb/config/log/configsrv.pid dbpath = /mydata/mongodb/config/data logpath = /usr/local/mongodb/config/log/congigsrv.log logappend = true bind_ip = 192.168.29.42 port = 28001 fork = true #Running MongoDB as a daemon to create server processes #declare this is a config db of a cluster; configsvr = true #Copy Set Name replSet=configs #Set the maximum number of connections maxConns=20000
Start the config server of three servers
mongod -f /usr/local/mongodb/conf/config.conf

Log on to any configuration server and initialize the configuration replica set
 Tie up
mongo --port 21000
 # config variable
config = {
... _id : "configs",
... members : [
... {_id : 0, host : "192.168.1.40:28001" },
... {_id : 1, host : "192.168.1.41:28001" },
... {_id : 2, host : "192.168.1.42:28001" }
... ]
... }

# Initialize replica sets
rs.initiate(config)

2. Configure a fragmented replica set (three machines)

configuration file
vi /usr/local/mongodb/conf/shard1.conf

#Configuration file content
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 192.168.29.40
port = 28011
fork = true

#Open web Monitoring
#httpinterface=true
#rest=true

#Copy Set Name
replSet=shard1

#declare this is a shard db of a cluster;
shardsvr = true

#Set the maximum number of connections
maxConns=20000
#Configuration file content
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 192.168.29.41
port = 28011
fork = true

#Open web Monitoring
#httpinterface=true
#rest=true

#Copy Set Name
replSet=shard1

#declare this is a shard db of a cluster;
shardsvr = true

#Set the maximum number of connections
maxConns=20000

#Configuration file content
#——————————————–
pidfilepath = /usr/local/mongodb/shard1/log/shard1.pid
dbpath = /mydata/mongodb/shard1/data
logpath = /usr/local/mongodb/shard1/log/shard1.log
logappend = true

bind_ip = 192.168.29.42
port = 28011
fork = true

#Open web Monitoring
#httpinterface=true
#rest=true

#Copy Set Name
replSet=shard1

#declare this is a shard db of a cluster;
shardsvr = true

#Set the maximum number of connections
maxConns=20000
 

vi /usr/local/mongodb/conf/shard2.conf

Vi/usr/local/mongodb/conf/shard2.conf

3. Configuring Routing Server mongos

Start the configuration server and slice server first, then start the routing instance: (three machines)
vi /usr/local/mongodb/conf/mongos.conf

#content
pidfilepath = /usr/local/mongodb/mongos/log/mongos.pid
logpath = /usr/local/mongodb/mongos/log/mongos.log
logappend = true

bind_ip = 0.0.0.0
port = 28000
fork = true

#Configuration servers monitored can only have one or three configs as the name of the replica set of configuration servers
configdb = configs/192.168.1.40:28001,192.168.1.41:28001,192.168.1.42:28001

#Set the maximum number of connections
maxConns=20000

 

mongos server to start three servers
mongos -f /usr/local/mongodb/conf/mongos.conf

4. Enabling Fragmentation

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.
Landing any mongos
mongo --port 28000
# Using admin database
use admin
# Series Routing Server and Allocation of Copy Sets
sh.addShard("shard1/192.168.1.40:28011,192.168.1.41:28011,192.168.1.42:28011")
sh.addShard("shard2/192.168.1.40:28012,192.168.1.41:28012,192.168.1.42:28012")
sh.addShard("shard3/192.168.1.40:28013,192.168.1.41:28013,192.168.1.42:28013")
# View cluster status
sh.status()

5, test

At present, configuration services, routing services, fragmentation services and replica set services are all connected in series, but our goal is to insert data and automatically fragment data. Connect to mongos, ready for the specified database, specified collection fragmentation to take effect.
# Specify testdb fragmentation to take effect
db.runCommand( { enablesharding :"testdb"});
# Specify the collection and key to be fragmented in the database
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )

We need to slice the table1 table of testdb, and automatically slice it to shard1, shard2 and shard3 according to id. This is because not all mongodb databases and tables need to be sliced!
Test Fragmentation Configuration Results
mongo  127.0.0.1:28000
 # Using testdb
use  testdb;
# Insert test data
for (var i = 1; i <= 100000; i++)
db.table1.save({id:i,"test1":"testval1"});
# View the fragmentation as follows, some irrelevant information is omitted
db.table1.stats();

{
        "sharded" : true,
        "ns" : "testdb.table1",
        "count" : 100000,
        "numExtents" : 13,
        "size" : 5600000,
        "storageSize" : 22372352,
        "totalIndexSize" : 6213760,
        "indexSizes" : {
                "_id_" : 3335808,
                "id_1" : 2877952
        },
        "avgObjSize" : 56,
        "nindexes" : 2,
        "nchunks" : 3,
        "shards" : {
                "shard1" : {
                        "ns" : "testdb.table1",
                        "count" : 42183,
                        "size" : 0,
                        ...
                        "ok" : 1
                },
                "shard2" : {
                        "ns" : "testdb.table1",
                        "count" : 38937,
                        "size" : 2180472,
                        ...
                        "ok" : 1
                },
                "shard3" : {
                        "ns" : "testdb.table1",
                        "count" :18880,
                        "size" : 3419528,
                        ...
                        "ok" : 1
                }
        },
        "ok" : 1
}

6. Later Operation and Maintenance

Startup and shutdown
The starting order of mongodb is to start the configuration server, start the fragmentation, and finally start mongos.
mongod -f /usr/local/mongodb/conf/config.conf
mongod -f /usr/local/mongodb/conf/shard1.conf
mongod -f /usr/local/mongodb/conf/shard2.conf
mongod -f /usr/local/mongodb/conf/shard3.conf
mongod -f /usr/local/mongodb/conf/mongos.conf

When closed, kill all processes directly
killall mongod
killall mongos

Topics: Linux MongoDB REST Database