MongoDB分布式操作——分片操作
描述:
像其它分布式数据库一样,MongoDB同样支持分布式操作,且MongoDB将分布式已经集成到数据库中,其分布式体系如下图所示:
所谓的片,其实就是一个单独的MongoDB进程,它可以运行在不同的节点上,也可以运行在同一节点但监听不同的端口。同一个集合的数据可能在每个片中都存储的有数据,但它们在逻辑上整合成一个整体。分片的依据往往是根据表中的某一列或者是集合中的某一 组属性。且分片是系统自动的,但是分片的依据需要用户自己指定,分片的依据称之为“片键”。常见的片键选择为“随机片键”或者是“递增片键”。如果使用递增片键,数据的分布则会按照一定的顺序来写入数据,即当一个节点写满才会写入到下一个片键。而如果使用随机片键,数据则可能在任何一个片中都有数据。
对于分布式中片的管理(片与片之间的通信),MongoDB中通过mongos进程来进行管理,客户端对数据的操作,都由mongos进程来处理。对于节点、分片的管理,MongoDB由配置服务器来管理,配置服务器其实也是一个单独MongoDB进程。
那么,一般在什么时候使用分片呢?在单个节点的磁盘不足时会分片,另外,当单个mongod不能满足写数据的性能要求时,可以使用分片;当单台服务器内存太小,将大量的数据放到内存中速度会很慢,为了提高性能,我们可以使用分片操作。
以下是对于MongoDB分片操作的实验:
操作环境:
Win8 64位操作系统,虚拟机CentOS 32位操作系统。MongoDB 2.4.5
实验步骤:
(1)启动配置服务器(放置一些元数据):
[plain]
[root@h3 dbs]# mongod -dbpath /var/lib/mongo/dbs/config --nojournal --port 20000
Mon Aug 12 13:36:25.999 [initandlisten] MongoDB starting : pid=22951 port=20000 dbpath=/var/lib/mongo/dbs/config 32-bit host=h3
Mon Aug 12 13:36:26.001 [initandlisten]
Mon Aug 12 13:36:26.002 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary .
Mon Aug 12 13:36:26.003 [initandlisten] ** 32 bit builds are limited to le ss than 2GB of data (or less with --journal).
Mon Aug 12 13:36:26.004 [initandlisten] ** Note that journaling defaults t o off for 32 bit and is currently off.
Mon Aug 12 13:36:26.005 [initandlisten] ** See http://dochub.mongodb.org/c ore/32bit
Mon Aug 12 13:36:26.005 [initandlisten]
Mon Aug 12 13:36:26.006 [initandlisten] db version v2.4.5
Mon Aug 12 13:36:26.007 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840 383ec3506602b
Mon Aug 12 13:36:26.007 [initandlisten] build info: Linux bs-linux32.10gen.cc 2. 6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Mon Aug 12 13:36:26.008 [initandlisten] allocator: system
Mon Aug 12 13:36:26.009 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/c onfig", nojournal: true, port: 20000 }
Mon Aug 12 13:36:26.071 [FileAllocator] allocating new datafile /var/lib/mongo/d bs/config/local.ns, filling with zeroes...
Mon Aug 12 13:36:26.072 [FileAllocator] creating directory /var/lib/mongo/dbs/co nfig/_tmp
Mon Aug 12 13:36:26.582 [FileAllocator] done allocating datafile /var/lib/mongo/ dbs/config/local.ns, size: 16MB, took 0.504 secs
Mon Aug 12 13:36:26.585 [FileAllocator] allocating new datafile /var/lib/mongo/d bs/config/local.0, filling with zeroes...
Mon Aug 12 13:36:27.725 [FileAllocator] done allocating datafile /var/lib/mongo/ dbs/config/local.0, size: 16MB, took 1.138 secs
Mon Aug 12 13:36:27.757 [initandlisten] command local.$cmd command: { create: "s tartup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 1714ms
Mon Aug 12 13:36:27.764 [websvr] admin web console waiting for connections on po rt 21000
Mon Aug 12 13:36:27.771 [initandlisten] waiting for connections on port 20000
(2)启动mongos:
[plain]
[root@h3 ~]# mongos --port 30000 --configdb localhost:20000
Mon Aug 12 13:40:13.042 warning: running with 1 config server should be done only for testing purposes and is not recommended for production
Mon Aug 12 13:40:13.075 [mongosMain] MongoS version 2.4.5 starting: pid=22968 port=30000 32-bit host=h3 (--help for usage)
Mon Aug 12 13:40:13.076 [mongosMain] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b
Mon Aug 12 13:40:13.076 [mongosMain] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Mon Aug 12 13:40:13.076 [mongosMain] options: { configdb: "localhost:20000", port: 30000 }
Mon Aug 12 13:40:13.250 [LockPinger] creating distributed lock ping thread for localhost:20000 and process h3:30000:1376286013:1804289383 (sleeping for 30000ms)
Mon Aug 12 13:40:17.105 [mongosMain] distributed lock 'configUpgrade/h3:30000:1376286013:1804289383' acquired, ts : 52087541ff5991dd77979f2c
Mon Aug 12 13:40:17.126 [mongosMain] starting upgrade of config server from v0 to v4
Mon Aug 12 13:40:17.128 [mongosMain] starting next upgrade step from v0 to v4
Mon Aug 12 13:40:17.130 [mongosMain] about to log new metadata event: { _id: "h3-2013-08-12T05:40:17-52087541ff5991dd77979f2d", server: "h3", clientAddr: "N/A", time: new Date(1376286017130), what: "starting upgrade of config database", ns: "config.version", details: { from: 0, to: 4 } }
Mon Aug 12 13:40:17.138 [mongosMain] writing initial config version at v4
Mon Aug 12 13:40:17.232 [mongosMain] about to log new metadata event: { _id: "h3-2013-08-12T05:40:17-52087541ff5991dd77979f2f", server: "h3", clientAddr: "N/A", time: new Date(1376286017232), what: "finished upgrade of config database", ns: "config.version", details: { from: 0, to: 4 } }
Mon Aug 12 13:40:17.237 [mongosMain] upgrade of config server to v4 successful
Mon Aug 12 13:40:17.244 [mongosMain] distributed lock 'configUpgrade/h3:30000:1376286013:1804289383' unlocked.
Mon Aug 12 13:40:17.474 [websvr] admin web console waiting for connections on port 31000
Mon Aug 12 13:40:17.476 [Balancer] about to contact config servers and shards
Mon Aug 12 13:40:17.492 [Balancer] config servers and shards contacted successfully
Mon Aug 12 13:40:17.494 [Balancer] balancer id: h3:30000 started at Aug 12 13:40:17
Mon Aug 12 13:40:17.527 [mongosMain] waiting for connections on port 30000
(3)添加分片1:
[plain]
[root@h3 ~]# cd /var/lib/mongo/dbs/
[root@h3 dbs]# mkdir shard1
[root@h3 dbs]# mongod --dbpath /var/lib/mongo/dbs/shard1 --port 10000 --nojournal
Mon Aug 12 13:43:10.560 [initandlisten] MongoDB starting : pid=23016 port=10000
dbpath=/var/lib/mongo/dbs/shard1 32-bit host=h3