Overview-Machines
Three MONGO data node machines (default port 27107, where a machine is stored for multiple copies, hence the new port):
10.13.2.51: 10001/10002/10003
10.13.2.52: 10001/10002/10003
10.13.2.151: 10001/10002/10003
A MONGOs machine:
10.13.2.112: 27107
Three config machines:
10.13.2.91: 27109
10.13.2.92: 27109
10.13.2.102: 27109
Architecture:The data is spread across 3 machines, with 3 Shard, and each shard
copy set (simple comprehension, master preparation)configuration, which is the following
shard1/10.13.2.151:10001,10.13.2.51:10001,10.13.2.52:10001
shard2/10.13.2.151:10002,10.13.2.51:10002,10.13.2.52:10002
shard3/10.13.2.151:10003,10.13.2.51:10003,10.13.2.52:10003
Prerequisite Work:
Useradd MongoDB # Recommended for MongoDB users echo never >/sys/kernel/mm/transparent_hugepage/enabled #mongodb建议为neverecho never & Gt /sys/kernel/mm/transparent_hugepage/defrag #mongodb建议为never关闭防火墙
Create the first replica set
1. Three data nodes are executed separately
1. mkdir Log db db/shard1 db/shard2 Db/shard3 # Create the necessary folder 2. Bin/mongod--dbpath/home/data/mongodb/db/shard1--port 10001--replset shard1--logpath log/mongodb1.log--logappend-- Fork # Background Start MongoDB service
2. Connect to MONGO configuration replica set
1. Bin/mongo 10.13.2.151:100012. Config = {_id: "Shard1", Members: [{_id:0, Host: "10.13.2.151:10001"}, {_id:1, Host: "10.13.2.51:10001"}, {_i D:2, Host: "10.13.2.52:10001"}]}; 3. rs.initiate (config)
Config machine deployment
1. The three config machines are executed separately
1. mkdir log configdb2. Bin/mongod--configsvr--dbpath/home/data/mongodb/configdb--logpath log/mongodb.log--logappend--fork
MONGOs Machine Deployment
1. Execute on MONGOs machine
1. mkdir log2. Bin/mongos--configdb 10.13.2.91:27019,10.13.2.92:27019,10.13.2.102:27019--logpath log/mongodb.log--logappend-- Fork
Adding the first shard to a cluster
1. Connect MONGOs, join Shard1
1. Bin/mongo 10.13.2.112:27017/admin2. Sh.addshard ("shard1/10.13.2.151:10001,10.13.2.51:10001,10.13.2.52:10001")
Add a second Shard
First create the SHARD2, ditto "create first replica set"
1. Three data nodes are executed separately
1. Bin/mongod--dbpath/home/data/mongodb/db/shard2--port 10002--replset shard2--logpath log/mongodb2.log--logappend --fork # Background Start MongoDB service
2. Connect to MONGO configuration replica set
1. Bin/mongo 10.13.2.151:100022. Config = {_id: "Shard2", Members: [{_id:0, Host: "10.13.2.151:10002"}, {_id:1, Host: "10.13.2.51:10002"}, {_i D:2, Host: "10.13.2.52:10002"}]}; 3. rs.initiate (config)
Add Shard2 When you are finished creating
1. Bin/mongo 10.13.2.112:27017/admin2. Sh.addshard ("shard2/10.13.2.151:10002,10.13.2.51:10002,10.13.2.52:10002")
Add a third Shard
First create the Shard3, ditto "create first replica set"
1. Three data nodes are executed separately
1. Bin/mongod--dbpath/home/data/mongodb/db/shard3--port 10003--replset shard3--logpath log/mongodb3.log--logappend --fork # Background Start MongoDB service
2. Connect to MONGO configuration replica set
1. Bin/mongo 10.13.2.151:100032. Config = {_id: "Shard2", Members: [{_id:0, Host: "10.13.2.151:10003"}, {_id:1, Host: "10.13.2.51:10003"}, {_i D:2, Host: "10.13.2.52:10003"}]}; 3. rs.initiate (config)
Add Shard3 When you are finished creating
1. Bin/mongo 10.13.2.112:27017/admin2. Sh.addshard ("shard3/10.13.2.151:10003,10.13.2.51:10003,10.13.2.52:10003")
The above configuration is done, but the database and table shards need to be opened
The following is an example of test
1. Connecting MONGOs
Bin/mongo 10.13.2.112:27017/admin
2. Turn on database sharding
Sh.enablesharding ("Test")
3. Create the Chip key-(here the chip key is very important, about the data how to shard, generally take itself _id as the chip key, business-related according to the business of their own combination of chip key)
1. Use Test2. Db.test_collection.createIndex ({number:1}) # Here is the tablet key for the number in the Test_collection table
4. Open the table shard below the database
1. Use Test2. Sh.shardcollection ("Test.test_collection", {"number": 1})
5. View shard status, data balancing situation
1. Use Test2. Db.stats () 3. Db.printshardingstatus ()
over!
MongoDB replica set + Shard installation