Skip the theory. I have introduced automatic sharding on the internet... directly go to the configuration. Please leave a message if you have any questions.
Or test with million data
Insert million pieces of data to a database without sharding.
Data storage:
^_^[root@:/usr/local/mongodb/data/mongodb/test]#ls -lhtotal 4.0G-rw------- 1 root root 64M Jul 25 11:25 test.0-rw------- 1 root root 128M Jul 25 11:22 test.1-rw------- 1 root root 256M Jul 25 11:23 test.2-rw------- 1 root root 512M Jul 25 11:25 test.3-rw------- 1 root root 1.0G Jul 25 11:25 test.4-rw------- 1 root root 2.0G Jul 25 11:24 test.5-rw------- 1 root root 16M Jul 25 11: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.
^_^[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/mongos_logs.txt --logappend --fork
Check whether all startup is successful:
[root@localhost bin]# ps -ef | grep 'mongo'root 17939 1 2 18:47 ? 00:01:26 ./mongod --shardsvr -dbpath=../data/mongodb --logpath=../data/shardsvr_logs.txt --logappend --forkroot 18250 1 0 19:05 ? 00:00:00 ./mongod --configsvr --dbpath=../data/mongodb/config_db/ --logpath=../data/mongodb/log/configsvr_logs.txt --logappend --forkroot 18290 1 12 19:08 ? 00:03:35 ./mongos --configdb 10.10.21.163:27019 --logpath=../data/mongodb/log/mongos_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 adminswitched 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:
^_^[root@:/usr/local/mongodb/data/mongodb]#ls -lhtotal 2.0G-rw-r--r-- 1 root root 1011 Jul 25 12:33 mongodb.log-rwxr-xr-x 1 root root 6 Jul 25 12:34 mongod.lockdrwxr-xr-x 3 root root 4.0K Jul 25 12:39 moveChunk-rw------- 1 root root 64M Jul 25 12:42 test.0-rw------- 1 root root 128M Jul 25 12:40 test.1-rw------- 1 root root 256M Jul 25 12:41 test.2-rw------- 1 root root 512M Jul 25 12:42 test.3-rw------- 1 root root 1.0G Jul 25 12:41 test.4-rw------- 1 root root 16M Jul 25 12:42 test.nsdrwxr-xr-x 2 root root 4.0K Jul 25 12:38 _tmp
228:
[Root @ localhost MongoDB] # ls-lH total 2.0gdrwxr-XR-x 3 Root 4.0 K 07-25 config_dbdrwxr-xr-x 2 root Root 4.0 K 07-25 config_repair_dbdrwxr-xr-x 2 root Root 4.0 k 07-25 19:39 log-rwxr-XR-x 1 Root 6 07-25 19:39 mongod. lockdrwxr-XR-x 3 Root 4.0 K 07-25 movechunk-RW ------- 1 Root 64 M 07-25 test.0-RW ------- 1 Root 128 M 07-25 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. nsdrwxr-XR-x 2 root Root 4.0 K 07-25 20:38 _ TMP
Auto-sharding into every 2 GB.