您的位置:首页 > 数据库 > Mongodb

mongodb 3.0 版本分片部署步骤

2016-10-08 16:59 453 查看

# linux 网络优化

1. 文件中/etc/sysctl.conf, 加入
net.core.somaxconn = 2048
fs.file-max = 2000000
fs.nr_open = 2000000
net.ipv4.ip_local_port_range = 1024 65535


2. 文件/etc/security/limits.conf中加入:
* hard nofile 1000000
* soft nofile 1000000

* hard nproc 1000000
* hard nproc 1000000

3. mongo部分的优化
echo never >/sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag


# disable senux
/etc/selinux/config
SELINUX=disabled

# 防火墙部分, 暂时停止firewalld
firewall:
systemctl start firewalld.service #启动firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

# mongodb 安装
如果是解压安装, 默认放到 /tools
1. 解压到
/root/tools/mongodb
cd /
ln -s /root/tools

2. 加入PATH
vim /etc/profile
export PATH=$PATH:/tools/mongodb/bin

对于rpm包, 运行下面命令
rpm -ivh *.rpm

 

# 配置mongodb
mkdir /data/mongodb
cd /data/mongodb
mkdir db log

常见配置文件, 并启动
mongod --config configdb.conf


=====================================
https://docs.mongodb.org/v3.0/tutorial/deploy-shard-cluster/

# 配置说明
234机器上:
shard0 192.168.1.60:27017
shard1 192.168.1.63:27017


configsrv需要三个:
configsrv 192.168.1.60:30001 /data/mongodb/cfgsrv1
configsrv 192.168.1.63:30001 /data/mongodb/cfgsrv1
configsrv 192.168.1.234:30001 /data/mongodb/cfgsrv1

# 配置 configserver
1. 文件配置的例子
sharding:
clusterRole: configsvr
net:
port: <port>
storage:
dbpath: <path>

2. 启动
mongod --config configsrv1.conf &
mongod --config configsrv2.conf &
mongod --config configsrv3.conf &
非文件方式
mongod --configsvr --replSet configReplSet --port <port> --dbpath <path>

# start mongos
//mongos --configdb configReplSet/192.168.1.55:30001,192.168.1.234:30001 --port 37017&
mongos --configdb 192.168.1.63:30001,192.168.1.63:30002,192.168.1.63:30003 --port 37017 --logappend --logpath /data/mongodb/log/route.log&
mongos --configdb 192.168.1.234:30001 --port 37017 --logappend --logpath /data/mongodb/log/route.log&

# connect to mongos
mongo --host 192.168.1.234 --port 37017

# add sharding
1. 建立实例
mkdir db2 db3
修改对应的配置文件
Note: 这个地方需要设置最大内存
ulimit -v 10000000 修改最大虚拟地址空间为10G
--wiredTigerCacheSizeGB 5

2. 加入分片
// sh.addShard( "rs1/192.168.1.234:27018" ) // add a shard for a replica set named rs1
sh.addShard( "192.168.1.63:27017" )
sh.addShard( "192.168.1.60:27017" )

3. 激活分片
sh.enableSharding("<database>") // db.runCommand( { enableSharding: <database> } )
sh.enableSharding("mydb")

4. 查看状态
sh.status()

5. 删除分片
db.runCommand( { removeshard: "shard0000" } )

 

#########################
插入前的准备
sh.enableSharding("<database>") // db.runCommand( { enableSharding: <database> } )

1. 建立索引
use gwgps
db.location.ensureIndex({"hostid":1})
db.location.ensureIndex({"posTime":1})
db.location.createIndex( { loc : "2dsphere" } )
db.location.getIndexes()

2. 开启分片
sh.enableSharding("gwgps")
sh.shardCollection("gwgps.location", { "hostid": 1})

sh.stopBalancer()
sh.getBalancerState()

3. AddTag
sh.addShardTag("shard0000", "TAG0")
sh.addShardTag("shard0001", "TAG1")

sh.addTagRange("gwgps.location", { hostid: "0000000" }, { hostid: "3100000" }, "TAG0")
sh.addTagRange("gwgps.location", { hostid: "3100000" }, { hostid: "9100000" }, "TAG1")

一些其他操作
sh.removeShardTag("shard0001", "TAG1")

4. add trunck
for ( var x=3000; x<3500; x++ ){
var prefix = String(x*1000);
sh.splitAt( "gwgps.location", { "hostid":prefix } )
}

##########################
加入两个分片操作
sh.addShard( "192.168.1.61:27017" )
sh.addShard( "192.168.1.62:27017" )

sh.addShardTag("shard0002", "TAG2")
sh.addShardTag("shard0003", "TAG3")

sh.removeTagRange( "gwgps.location", { hostid: "3100000" }, { hostid: "9100000" }, "TAG1")
sh.addTagRange("gwgps.location", { hostid: "3100000" }, { hostid: "3200000" }, "TAG1")
sh.addTagRange("gwgps.location", { hostid: "3200000" }, { hostid: "3300000" }, "TAG2")
sh.addTagRange("gwgps.location", { hostid: "3300000" }, { hostid: "9100000" }, "TAG3")

for ( var x=3300; x<3900; x++ ){
var prefix = String(x*1000);
sh.splitAt( "gwgps.location", { "hostid":prefix } )
}

===========================
234:
mkdir /data/mongodb/cfgsrv1
mkdir /data/mongodb/log
mongod --config /tools/percona-mongodb/conf/configsrv1.conf &
mongos --configdb 192.168.1.234:30001 --port 37017 --logappend --logpath /data/mongodb/log/route.log&

60-63:
/data/mongodb-perco/db
/data/mongodb-perco/log
mongod --config /tools/percona-mongodb/conf/mongod.conf &

sh.addShard( "192.168.1.60:27017" )
sh.addShard( "192.168.1.61:27017" )
sh.addShard( "192.168.1.62:27017" )
sh.addShard( "192.168.1.63:27017" )

sh.addShardTag("shard0000", "TAG0")
sh.addShardTag("shard0001", "TAG1")
sh.addShardTag("shard0002", "TAG2")
sh.addShardTag("shard0003", "TAG3")

db.runCommand({movePrimary:"gwgps",to:"shard0000"})

sh.addTagRange("gwgps.location", { hostid: "0000000" }, { hostid: "3100000" }, "TAG0")
sh.addTagRange("gwgps.location", { hostid: "3100000" }, { hostid: "3200000" }, "TAG1")
sh.addTagRange("gwgps.location", { hostid: "3200000" }, { hostid: "3400000" }, "TAG2")
--sh.addTagRange("gwgps.location", { hostid: "3300000" }, { hostid: "9100000" }, "TAG3")

手动建立空的chunk
for ( var x=300; x<340; x++ ){
var prefix = String(x*10000);
sh.splitAt( "gwgps.location", { "hostid":prefix } )
}

==shard0000
for ( var x=300; x<310; x++ ){
var prefix = String(x*10000);
sh.moveChunk( "gwgps.location", { "hostid":prefix }, "shard0000" )
}

==shard0001 310 - 320
for ( var x=310; x<320; x++ ){
var prefix = String(x*10000);
sh.moveChunk( "gwgps.location", { "hostid":prefix }, "shard0001" )
}

==shard0002 320 - 340
for ( var x=320; x<340; x++ ){
var prefix = String(x*10000);
sh.moveChunk( "gwgps.location", { "hostid":prefix }, "shard0002" )
}


索引建立
use gwgps
db.location.ensureIndex({"hostid":1})
db.location.ensureIndex({"hostno":1})
db.location.ensureIndex({"posTime":1})
db.location.ensureIndex( { "loc" : "2dsphere" } )
db.location.ensureIndex({"hostno":1,"posTime":1})
db.location.ensureIndex({"hostno":1,"posTime":1, "loc" : "2dsphere"})


db.location.getIndexes()

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: