mongodb distributed cluster construction

1.Mongo installation steps

Architecture: There are three machines in total, node1, node2, node3, and the version of mongdb is mongodb-linux-x86_64-enterprise-rhel62-3.4.13.tgz, which can be downloaded from the official website:

2. Role assignment:

node1: shard01, shard01 copy 1, copy 2, configServer01

node2: shard02, shard02 copy 1, copy 2, configSever02, configServer03

node3:mongos

3. mongodb shard configuration on node1:

[root@node1 data]# ll /etc/mongo*
-rw-r--r-- 1 root root 151 Apr 12 15:11 /etc/mongo1.conf
-rw-r--r-- 1 root root 151 Apr 12 15:11 /etc/mongo2.conf
-rw-r--r-- 1 root root 151 Apr 12 15:13 /etc/mongo3.conf
-rw-r--r-- 1 root root 327 Apr 12 15:40 /etc/mongo_config1.conf

The /etc/mongo1.conf configuration file is as follows

---------------------------------------------------------------------------------------------------------------------
port=47027
dbpath=/data/mongodb1/mongo/
logpath=/data/mongodb1/log/47027.log
fork=true
shardsvr=true
directoryperdb=true
replSet=shard01
journal=true

------------------------------------------------------------------------------------------------------------------------

Start:
/opt/mongodb-linux-x86_64-enterprise-rhel62-3.4.13/bin/mongod -f /etc/mongo1.cnf --fork &

access:

mongo --port 47027

rs.initiate({_id:"shard01", members: [{_id: 1, host:"node1:47027"},{_id: 2, host:"node1:47028"},{_id: 3, host:"node1:47029","arbiterOnly":true}]}); ========分片初始化

Do the same on node2:
---------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------

rs.initiate({_id:"shard02", members: [{_id: 1, host:"node2:47027"},{_id: 2, host:"node2:47028"},{_id: 3, host:"node2:47029","arbiterOnly":true}]}); ========分片初始化

Four.configserver configuration:

[root@node1 data]# cat /etc/mongo_config1.conf

--------------------------------------------------------------------------------------------------------------------------------------
pidfilepath = /data/mongodb1/configsrv.pid
dbpath = /data/mongodb1/config/data
logpath = /data/mongodb1/config/log/congigsrv.log
logappend = true

bind_ip = 0.0.0.0
port = 47013
fork = true

#declare this is a config db of a cluster;
configsvr = true

#Replica set name
replSet=configs #Set

the maximum number of connections
maxConns=20000

--------------------------------------------------------------------------------------------------------------------------------------

启动configServer:
/opt/mongodb-3.4.13/bin/mongod -f /etc/mongo_config1.conf --fork &

mongo --port 47013 Go in and add config cluster information. The main purpose is to configure your CFSVR name and add three config information. After the configuration is completed, one master and two slaves will be displayed automatically. The configuration here is to specify the config for the subsequent mongos startup.

rs.initiate({_id : "configs",members : [{_id : 0, host : "node1:47013" },{_id : 1, host : "node2:47014" },{_id : 2, host : "node2:47015" }]})  =======config server初始化

 

Five. mongos configuration:

[root@node3 data]# cat /etc/mongos.conf

----------------------------------------------------------------------------------------------------------
logpath = /data/mongodb/log/27017.log
logappend = true
port = 27017
fork = true

#Listening configuration server, there can only be 1 or 3 configs for the replica set name of the configuration server
configdb = configs/node1:47013, node2:47014, node3:47015

#Set the maximum number of connections
maxConns=20000

--------------------------------------------------------------------------------------------------------------

Start:
/opt/mongodb-linux-x86_64-enterprise-rhel62-3.4.13/bin/mongos -f /usr/mongos.conf

mongo --port 27017 login add

On node1:
sh.addShard("shard01/node1:47027,node1:47028,node1:47029") =======Adding shards is done on each corresponding machine

在node2上:
sh.addShard("shard02/node2:47027,node2:47028,node2:47029")

Note: When ADD is used, ARB also ADDs in. There is no need to specify arbitration here, because SHARD has already been specified.

 

 

So far, the mongo distributed cluster has been built:

Six follow-up:

Use JS to write data in batches:
for (var i = 1; i <= 25000; i++) db.test2.insert( { name : i+"mongoDB" } )

sh.enableSharding("testsharding") =============Enable sharding for the library

sh.shardCollection("testsharding.test2",{_id:'hashed'}) =============分片分键

db.test2.ensureIndex({"_id":1}) ===================================================================================================================================================================

rs.slaveOk()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324496783&siteId=291194637