環境准備
Linux環境
主機 | OS | 備注 |
192.168.32.13 | CentOS6.3 64位 | 普通PC |
192.168.71.43 | CentOS6.2 64位 | 服務器,NUMA CPU架構 |
MongoDB版本:mongodb-linux-x86_64-2.4.1,下載地址:www.mongodb.org/downloads.
MongoDB安裝:分別在兩台機器上安裝好mongodb 2.4.1,安裝路徑都為/url/local/mongodb-2.4.1/
cd /usr/local/src/ wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.1.tgz tar -zxvf mongodb-linux-x86_64-2.4.1.tgz cp -r mongodb-linux-x86_64-2.4.1 /usr/local/mongodb-2.4.1 cd /usr/local/mongodb-2.4.1/bin/ ll
可以看到mongodb安裝成功有如下模塊:
mongodb啟動和關閉等在后面集群搭建中有詳細說明,在此不再贅述。
Sharding集群搭建
Mongodb一共有三種集群搭建的方式:Replica Set(副本集)、Sharding(切片)和Master-Slaver(主從)。下面要搭建的是Sharding,Sharding集群也是三種集群中最復雜的。
配置服務器啟動(192.168.32.13:10000):
1. ./bin/mongod --fork --dbpath data/config/ --logpath log/config.log –port 10000
路由服務器啟動(192.168.32.13:20000):
1. ./bin/mongos --port 20000 --configdb 192.168.32.13:10000 --logpath log/mongos.log --fork
注意1:配置--conigdb的時候ip地址不能填localhost或127.0.0.1否則添加分片時會返回如下錯誤信息:
1. { 2. "ok" : 0, 3. "errmsg" : "can't use localhost as a shard since all shards need to communicate. either use all shards and configdbs in localhost or all in actual IPs host: 192.168.71.43:27017 isLocalHost:0" 4. }
啟動分片1(192.168.32.13:27019):
1. ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log --fork --port 27019
啟動分片2(192.168.32.13:27020):
1. ./bin/mongod --dbpath data/shard3/ --logpath log/shard3.log --fork --port 27020
啟動分片3(192.168.71.43:27017):
1. numactl --interleave=all ./bin/mongod --dbpath data/shard1/ --logpath log/shard1.log --fork --port 27017
啟動分片4(192.168.71.43:27018):
1. numactl --interleave=all ./bin/mongod --dbpath data/shard2/ --logpath log/shard2.log --fork --port 27018
說明:關於這里為什么加numactl --interleave=all,后面有詳細說明。
Note:在生產環境可以將啟動的配置信息寫入文件,然后啟動的時候指定配置文件,這樣便於管理:
1. vi conf/config.conf 2. bpath=data/config/ 3. logpath=log/config.log 4. port=10000 5. fork=true 6. ./bin/mongod -f conf/config.conf
添加分片1(192.168.32.13:27019):
1. ./bin/mongo --port 20000 2. mongos> use admin 3. switched to db admin 4. mongos> db.runCommand({addshard:"192.168.32.13:27019",allowLocal:true })
注意2:同樣這里的192.168.32.13不能用localhost或127.0.0.1代替,並且當路由進程和分片在同一台機器上要指定allowLocal為true,因為MongoDB盡量避免錯誤的配置,將集群配置在本地,所以這個配置指明當前僅僅是用於開發。
添加分片3(192.168.71.43:27017):
1. mongos> db.runCommand({addshard:"192.168.71.43:27017" })
類似的添加分片2,4。
分片添加成功返回類似下面的信息(當前mongodb版本為2.4.1):
1. { "shardAdded" : "shard0000", "ok" : 1 }
刪除分片:如果要刪除分片的話可以removeshard命令:
1. mongos> use admin 2. switched to db admin 3. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 4. { 5. "msg" : "draining started successfully", 6. "state" : "started", 7. "shard" : "shard0001", 8. "note" : "you need to drop or movePrimary these databases", 9. "dbsToMove" : [ 10. "test3" 11. ], 12. "ok" : 1 13. }
移除分片需要一個過程,MongoDB會把移除的片上的數據(塊)挪到其他片上,移動中會顯示進度:
1. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 2. { 3. "msg" : "draining ongoing", 4. "state" : "ongoing", 5. "remaining" : { 6. "chunks" : NumberLong(0), 7. "dbs" : NumberLong(1) 8. }, 9. "note" : "you need to drop or movePrimary these databases", 10. "dbsToMove" : [ 11. "test3" 12. ], 13. "ok" : 1 14. }
注意:如果刪除的片是數據庫的大本營(基片),必須手動移動或刪除數據庫,用moveprimary命令,上面的示例中就提示192.168.32.13:27020是test3庫的大本營(primary),這個信息可以通過查看config.databases看到:
1. mongos> use config 2. switched to db config 3. mongos> db.databases.find() 4. { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" }
這里shard0001就是192.168.32.13:27020,下面通過moveprimary命令移除test3:
1. mongos> use admin 2. switched to db admin 3. mongos> db.runCommand({"moveprimary":"test3","to":"192.168.32.13:27019"}) 4. { "primary " : "shard0000:192.168.32.13:27019", "ok" : 1 }
這時再查看config.databases會發現test3的大本營變成了shard0000(192.168.32.13:27019)
這時再執行removeshard命令則能成功移除分片了:
1. mongos> db.runCommand({"removeshard":"192.168.32.13:27020"}) 2. { 3. "msg" : "removeshard completed successfully", 4. "state" : "completed", 5. "shard" : "shard0001", 6. "ok" : 1 7. }
管理分片
進入mongos進程config庫可以看到目前分片的情況:
1. ./bin/mongo –port 20000 2. use config 3. db.shards.find() 1. mongos> use config 2. switched to db config 3. mongos> db.shards.find() 4. { "_id" : "shard0000", "host" : "192.168.32.13:27019" } 5. { "_id" : "shard0001", "host" : "192.168.71.43:27017" } 6. { "_id" : "shard0002", "host" : "192.168.71.43:27018" }
注意3:如果配置過程中發生過上面注意1中出現的情況,即配置configdb的時候用了localhost或127.0.0.1,則運行db.shards.find()可能會出現如下錯誤:
1. mongos> db.shards.find() 2. error: { 3. "$err" : "could not initialize sharding on connection 192.168.32.13:10000 :: caused by :: mongos specified a different config database string : stored : localhost:10000 vs given : 192.168.32.13:10000", 4. "code" : 15907 5. }
解決方法是重新啟動config進程。
查看分片后的數據庫:
1. ./bin/mongo –port 20000 2. use test 3. db.test.user.insert({“test”: “test”}) 4. …… 5. use config 6. db.databases.find() 7. { "_id" : "admin", "partitioned" : false, "primary" : "config" } 8. { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } 9. { "_id" : "test2", "partitioned" : false, "primary" : "shard0000" } 10. { "_id" : "test3", "partitioned" : false, "primary" : "shard0001" }
“_id”,字符串。表示數據庫名。
“partioned”,布爾型。如果為true則表示開啟了分片功能。
“primary”,字符串,這個值與“_id”對應表示這個數據庫的“大本營“在哪里,不論分片與否,數據庫總是會有個“大本營”,創建數據庫時會隨機選擇一個片,也就是說大本營是開始創建數據庫文件的位置。雖然分片的時候數據庫也會用到很多別的服務器,但是從這分片開始。
至此整個mongodb分片集群基本搭建完成,但是想讓分片正常、高效、穩定的運行還有很多工作要做,下一節將在此基礎上做一些簡單的測試。