分片
mongos把分片的mongod管理起来
何时分片
单个节点磁盘不足
单个mongod不能满足写数据的性能要求
将大量数据放到内存中提高性能
什么是片键
选择递增(分布不均匀,查询比较好)还是随机片键
片键对操作和性能的影响
实施分片
启动配置服务器
启动mongos
添加mongod实例
对数据库启用分片
对集合进行分片
[root@localhost ~]# cd /var/lib/mongod
bash: cd: /var/lib/mongod: No such file or directory
[root@localhost ~]# cd /var/lib/mongo
[root@localhost mongo]# cd dbs
[root@localhost dbs]# mkdir config
[root@localhost dbs]# cd config
[root@localhost config]# pwd
/var/lib/mongo/dbs/config
[root@localhost config]# mongod --dbpath /var/lib/mongo/dbs/config --port 20000
mongos --port 30000 --configdb localhost:20000
[root@localhost ~]# ps -ef |grep mong
root 3350 3108 1 20:37 pts/3 00:00:04 mongod --dbpath /var/lib/mongo/dbs/config --port 20000
root 3399 3387 0 20:43 pts/1 00:00:00 mongos --port 30000 --configdb localhost:20000
[root@localhost ~]# cd /var/lib/mongo/dbs
[root@localhost dbs]# mkdir shard1
[root@localhost dbs]# ls
config master shard1 slave
[root@localhost dbs]# mongod --dbpath /var/lib/mongo/dbs/shard1 --port 10000 --nojournal
[root@localhost ~]# mongo localhost:30000/admin
MongoDB shell version: 2.6.11
connecting to: localhost:30000/admin
mongos>
db.runCommand({addshard:"localhost",allowLocal:true})
mongos> db.runCommand({addshard:"localhost",allowLocal:true})
{
"ok" : 0,
"errmsg" : "couldn't connect to new shard socket exception [CONNECT_ERROR] for localhost:27017"
}
mongos> db.runCommand({enablesharding:"foo"})数据库foo
db.runCommand({"sharecollection":"foo.bar","key":{"_id":1}})库foo下面的bar
查看所有的片
useconfig
db.shards.find();
db.databases.find();
chunks集合
db.chunks.find();
删除片
db.runCommand({"removeshard":"localhost:10000"});