Fork me on GitHub

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.

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

posted @ 2013-04-10 21:52  zhanjindong  阅读(27990)  评论(1编辑  收藏  举报
TOP