Skip the theoretical things and go to the web page to introduce automatic sharding.
Or test with million data
Insert million pieces of data to a database without sharding.
Data storage:
- Performance_^ [root @:/usr/local/mongodb/data/mongodb/test] # ls-lh
- Total 4.0 GB
- -Rw ------- 1 root 64 M Jul 25 :25 test.0
- -Rw ------- 1 root 128 M Jul 25 :22 test.1
- -Rw ------- 1 root 256 M Jul 25 :23 test.2
- -Rw ------- 1 root 512 M Jul 25 :25 test.3
- -Rw ------- 1 root 1.0G Jul 25 :25 test.4
- -Rw ------- 1 root 2.0G Jul 25 :24 test.5
- -Rw ------- 1 root 16 M Jul 25 :25 test. ns
Perform automatic sharding configuration as follows:
Here we select two servers for testing.
Test Machine IP Address:
10. X. X.163
10. X. X.228
Start the Shardsvr process on the 163,228 server.
- Pai_^ [root @:/usr/local/mongodb/bin] # cat runServerShard. sh
- ./Mongod -- shardsvr-dbpath = ../data/mongodb -- logpath = ../data/shardsvr_logs.txt -- logappend -- fork
Start the configsvr and mongos routing processes on the 163 server (the following 27019 is the ip address of configsvr, And the ip address of mongos can be viewed in the log)
- [Root @ localhost bin] # cat runServerConfig. sh
- ./Mongod -- configsvr -- dbpath = ../data/mongodb/config_db/-- logpath = ../data/mongodb/log/configsvr_logs.txt -- logappend -- fork
- [Root @ localhost bin] # cat runServerMongos. sh
- ./Mongos -- configdb 10.10.21.163: 27019 -- logpath = ../data/mongodb/log/s_s_logs.txt -- logappend -- fork
Check whether all startup is successful:
- [Root @ localhost bin] # ps-ef | grep'Mongo'
- Root 17939 1 2 :47? 00:01:26./mongod -- shardsvr-dbpath = ../data/mongodb -- logpath = ../data/shardsvr_logs.txt -- logappend -- fork
- Root 18250 1 0 :05? 00:00:00./mongod -- configsvr -- dbpath = ../data/mongodb/config_db/-- logpath = ../data/mongodb/log/configsvr_logs.txt -- logappend -- fork
- Root 18290 1 12 19: 08? 00:03:35./mongos -- configdb 10.10.21.163: 27019 -- logpath = ../data/mongodb/log/s_s_logs.txt -- logappend -- fork
It can be seen that all three services have been started successfully.
Configure Automatic parts as follows:
Use mongo to connect to the mongos process (see the log) for configuration management:
- > Use admin
- Switched to db admin
- > Db. runCommand ({addshard:"10.10.21.163: 27018"});
- {"OK": 0,"Errmsg":"Host already used"}
- > Db. runCommand ({addshard:"10.7.3.228: 27018"});
- {"ShardAdded":"Shard0001","OK": 1}
- > Db. runCommand ({enableSharding:"Test"})
- {"OK": 1}
- > Db. runCommand ({shardcollection:"Test. users", Key: {_ id: 1 }})
- {"Collectionsharded":"Test. users","OK": 1}
Insert million data into the mongos Process
Let's see the data size:
163:
- Pai_^ [root @:/usr/local/mongodb/data/mongodb] # ls-lh
- Total 2.0 GB
- -Rw-r -- 1 root 1011 Jul 25 :33 mongodb. log
- -Rwxr-xr-x 1 root 6 Jul 25 12:34 mongod. lock
- Drwxr-xr-x 3 root 4.0 K Jul 25 moveChunk
- -Rw ------- 1 root 64 M Jul 25 :42 test.0
- -Rw ------- 1 root 128 M Jul 25 :40 test.1
- -Rw ------- 1 root 256 M Jul 25 :41 test.2
- -Rw ------- 1 root 512 M Jul 25 12:42 test.3
- -Rw ------- 1 root 1.0G Jul 25 :41 test.4
- -Rw ------- 1 root 16 M Jul 25 :42 test. ns
- Drwxr-xr-x 2 root 4.0 K Jul 25 _ tmp
228:
- [Root @ localhost mongodb] # ls-lh
- Total 2.0 GB
- Drwxr-xr-x 3 root 4.0 K 07-25 19:39 config_db
- Drwxr-xr-x 2 root 4.0 K 07-25 18:51 config_repair_db
- Drwxr-xr-x 2 root 4.0 K 07-25 log
- -Rwxr-xr-x 1 root 6 07-25 19:39 mongod. lock
- Drwxr-xr-x 3 root 4.0 K 07-25 19: 13 movechun
- -Rw ------- 1 root 64 M 07-25 20:38 test.0
- -Rw ------- 1 root 128 M 07-25 20:38 test.1
- -Rw ------- 1 root 256 M 07-25 20:40 test.2
- -Rw ------- 1 root 512 M 07-25 20:41 test.3
- -Rw ------- 1 root 1.0G 07-25 20:42 test.4
- -Rw ------- 1 root 16 M 07-25 20:38 test. ns
- Drwxr-xr-x 2 root 4.0 K 07-25 20:38 _ tmp
Auto-sharding into every 2 GB.