MongoDB分片实战(一):集群搭建

随笔- 71 文章- 9 评论- 143

MongoDB分片实战(一):集群搭建

环境准备

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.html

MongoDB安装:分别在两台机器上安装好mongodb 2.4.1,安装路径都为/url/local/mongodb-2.4.1/linux

 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

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):ide

 1.    ./bin/mongos --port 20000 --configdb 192.168.32.13:10000 --logpath log/mongos.log  --fork 

注意1:配置--conigdb的时候ip地址不能填localhost或127.0.0.1不然添加分片时会返回以下错误信息:post

 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):url

 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分片集群基本搭建完成,可是想让分片正常、高效、稳定的运行还有不少工做要作,下一节将在此基础上作一些简单的测试。

相关文章
相关标签/搜索