目录
一、分片群集搭建
1.1 安装MongoDB 3.2
上传mongodb-linux-x86_64-3.2.1.tgz软件包到/opt目录下
[root@localhost ~]# yum -y install openssl-devel
[root@localhost ~]# cd /opt
[root@localhost opt]# tar zxvf mongodb-linux-x86_64-3.2.1.tgz
[root@localhost opt]# mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb
[root@localhost opt]# cd /usr/local/mongodb
[root@localhost mongodb]# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
[root@localhost mongodb]# cd bin/
[root@localhost bin]# ls
bsondump mongod mongoexport mongoimport mongoperf mongos mongotop
mongo mongodump mongofiles mongooplog mongorestore mongostat
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
[root@localhost bin]# mkdir -p /data/mongodb/mongodb{1,2,3,4}
[root@localhost bin]# cd /data/mongodb/
[root@localhost mongodb]# ls
mongodb1 mongodb2 mongodb3 mongodb4
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# ls
logs mongodb1 mongodb2 mongodb3 mongodb4
[root@localhost mongodb]# cd logs/
[root@localhost logs]# touch mongodb{1,2,3,4}.log
[root@localhost logs]# ls
mongodb1.log mongodb2.log mongodb3.log mongodb4.log
*[root@localhost logs]# chmod 777 .log
[root@localhost logs]# ls
mongodb1.log mongodb2.log mongodb3.log mongodb4.log
[root@localhost logs]# ulimit -n 25000
[root@localhost logs]# ulimit -u 25000
[root@localhost logs]# cd /usr/local/mongodb/bin/
[root@localhost bin]# ls
bsondump mongod mongoexport mongoimport mongoperf mongos mongotop
mongo mongodump mongofiles mongooplog mongorestore mongostat
[root@localhost bin]# vim mongodb1.conf
port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/mongodb/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true //指定配置服务器模式
1.2 某节点内存不足时,从其他节点分配内存
[root@localhost bin]# sysctl -w vm.zone_reclaim_mode=0
vm.zone_reclaim_mode = 0
[root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[root@localhost bin]# ls
mongo mongodump mongoimport mongorestore mongotop
mongod mongoexport mongooplog mongos
mongodb1.conf mongofiles mongoperf mongostat
[root@localhost bin]# mongod -f /usr/local/mongodb/bin/mongodb1.conf
[root@localhost bin]# mongo --port 37017 //登录数据库
1.3 分片服务器搭建
[root@localhost bin]# cp -p mongodb1.conf mongodb2.conf
[root@localhost bin]# ls
mongodb1.conf mongodb2.conf
[root@localhost bin]# vi mongodb2.conf
port=47017
dbpath=/data/mongodb/mongodb2
logpath=/data/mongodb/logs/mongodb2.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
[root@localhost bin]# cp mongodb2.conf mongodb3.conf
[root@localhost bin]# vi mongodb3.conf
port=47018
dbpath=/data/mongodb/mongodb3
logpath=/data/mongodb/logs/mongodb3.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
[root@localhost bin]# mongod -f mongodb2.conf
[root@localhost bin]# mongod -f mongodb3.conf
[root@localhost bin]# mongo --port 47017
[root@localhost bin]# mongo --port 47018
1.4 启动路由服务器
[root@localhost bin]# ./mongos --help
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 20.0.0.21:37017 --chunkSize 1
//–port:指定对方连接入口;–fork:在后台运行;–logpath:指定日志文件存储路径;–configdb:指定配置服务器;–chunkSize:指定数据块大小
1.5 启用分片服务器
[root@localhost bin]# mongo
mongos> show dbs
config 0.031GB
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“5fa56ee276a709f5dccfc4e2”)
}
shards: //为空,没有分片服务器
active mongoses:
“3.2.1” : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
mongos> sh.addShard(“20.0.0.21:47017”)
mongos> sh.addShard(“20.0.0.21:47018”) //添加节点服务器
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“5fa56ee276a709f5dccfc4e2”)
}
shards:
{ “_id” : “shard0000”, “host” : “20.0.0.21:47017” }
{ “_id” : “shard0001”, “host” : “20.0.0.21:47018” } //分片添加成功
active mongoses:
“3.2.1” : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
1.6 分片功能
mongos> show dbs
mongos> use kgc
mongos> db.users.insert({“id”:1,“name”:“zhangsan”})
mongos> show dbs
config 0.031GB
kgc 0.078GB
###./mongoimport -d kgc -c users --file /opt/testdb.txt //导入,也可以写入大量数据###
mongos> for(var i=2;i<=10000;i++)db.users.insert({“id”:i,“name”:“jack”+i})
mongos> db.users.find().limit(5) //查看前5条数据
mongos> sh.status() //此时还没有分片处理
mongos> sh.enableSharding(“kgc”) //启用数据库分片
mongos> sh.status() //启用了分片处理
databases:
{ “_id” : “kgc”, “primary” : “shard0000”, “partitioned” : true }
mongos> db.users.createIndex({“id”:1}) //对users表创建索引
mongos> sh.shardCollection(“kgc.users”,{“id”:1}) //表分片,shardCollection:分片集合
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“5fa7523522cf59cad07aa64e”)
}
shards:
{ “_id” : “shard0000”, “host” : “20.0.0.22:47017” }
{ “_id” : “shard0001”, “host” : “20.0.0.22:47018” }
active mongoses:
“3.2.1” : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{ “_id” : “kgc”, “primary” : “shard0000”, “partitioned” : true }
kgc.users
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 1
{ “id” : { “$minKey” : 1 } } -->> { “id” : 4682 } on : shard0001 Timestamp(2, 0)
{ “id” : 4682 } -->> { “id” : 9364 } on : shard0000 Timestamp(2, 1)
{ “id” : 9364 } -->> { “id” : { " $maxKey" : 1 } } on : shard0000 Timestamp(1, 2)
//进行了分片
二、分片管理
2.1 分片
mongos> use kgc
mongos> for(var i=1;i<=50;i++)db.users2.insert({“id”:i,“name”:“jack”+i})
mongos> db.users2.count() //统计数量
mongos> db.users2.find().limit(10) //查看前十行
mongos> db.users2.createIndex({“id”:1})
mongos> sh.shardCollection(“kgc.users2”,{“id”:1})
mongos> sh.status()
— Sharding Status —
sharding version: {
“_id” : 1,
“minCompatibleVersion” : 5,
“currentVersion” : 6,
“clusterId” : ObjectId(“5fa7523522cf59cad07aa64e”)
}
shards:
{ “_id” : “shard0000”, “host” : “20.0.0.22:47017” }
{ “_id” : “shard0001”, “host” : “20.0.0.22:47018” }
active mongoses:
“3.2.1” : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
6 : Success
databases:
{ “_id” : “kgc”, “primary” : “shard0000”, “partitioned” : true }
kgc.users
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 1
{ “id” : { “KaTeX parse error: Expected 'EOF', got '}' at position 13: minKey" : 1 }̲ } -->> { "id" …maxKey” : 1 } } on : shard0000 Timestamp(1, 2)
kgc.users2
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 6
shard0001 5
{ “id” : { “KaTeX parse error: Expected 'EOF', got '}' at position 13: minKey" : 1 }̲ } -->> { "id" …maxKey” : 1 } } on : shard0000 Timestamp(1, 10)
//进行了分片
2.2 添加标签
mongos> sh.addShardTag(“shard0000”,“sales00”)
mongos> sh.addShardTag(“shard0001”,“sales01”)
2.3 连接配置服务器
[root@localhost bin]# mongo --port 37017
configsvr> use config
configsvr> show collections
configsvr> db.databases.find()
configsvr> db.chunks.find() //查看分片
2.4 添加分片服务器
[root@localhost bin]# cp -p mongodb3.conf mongodb4.conf
[root@localhost bin]# vim mongodb4.conf
port=47019
dbpath=/data/mongodb/mongodb4
logpath=/data/mongodb/logs/mongodb4.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
[root@localhost bin]# mongod -f mongodb4.conf
[root@localhost bin]# mongo
mongos> sh.addShard(“20.0.0.21:47019”) //添加分片
mongos> sh.status() //添加后自动在原有的基础上重新进行分片,无需人为干涉
。。。。。。。。。。。。。。。。。。。。。。。。。
shards:
{ “_id” : “shard0000”, “host” : “20.0.0.21:47017”, “tags” : [ “sales00” ] }
{ “_id” : “shard0001”, “host” : “20.0.0.21:47018”, “tags” : [ “sales01” ] }
{ “_id” : “shard0002”, “host” : “20.0.0.21:47019” }
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
databases:
{ “_id” : “kgc”, “primary” : “shard0000”, “partitioned” : true }
kgc.users
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 1
shard0001 1
shard0002 1
{ “id” : { “KaTeX parse error: Expected 'EOF', got '}' at position 13: minKey" : 1 }̲ } -->> { "id" …maxKey” : 1 } } on : shard0000 Timestamp(3, 1)
kgc.users2
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 4
shard0001 4
shard0002 3
2.5 删除分片节点
mongos> use admin
mongos> db.runCommand({“removeshard”:“20.0.0.21:47019”})
mongos> sh.status() //又会重新分片
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
databases:
{ “_id” : “kgc”, “primary” : “shard0000”, “partitioned” : true }
kgc.users
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 1
{ “id” : { “KaTeX parse error: Expected 'EOF', got '}' at position 13: minKey" : 1 }̲ } -->> { "id" …maxKey” : 1 } } on : shard0000 Timestamp(3, 1)
kgc.users2
shard key: { “id” : 1 }
unique: false
balancing: true
chunks:
shard0000 6
shard0001 5
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。