MongoDB分片实战
Posted 小小程序员的梦想
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB分片实战相关的知识,希望对你有一定的参考价值。
准备目录
/home/mongodb/shard/s0 /home/mongodb/shard/s1 /home/mongodb/config0 /home/mongodb/config1 /home/mongodb/log
启动shard
/usr/local/mongodb/bin/mongod --port 3000 --dbpath=/home/mongodb/shard/s0 --logpath=/home/mongodb/log/s0.log --logappend --fork --shardsvr /usr/local/mongodb/bin/mongod --port 3001 --dbpath=/home/mongodb/shard/s1 --logpath=/home/mongodb/log/s1.log --logappend --fork --shardsvr
启动config
/usr/local/mongodb/bin/mongod --port 3999 --dbpath=/home/mongodb/config/c0 --logpath=/home/mongodb/log/config0.log --logappend --fork --configsvr --replSet=config /usr/local/mongodb/bin/mongod --port 39990 --dbpath=/home/mongodb/config/c1 --logpath=/home/mongodb/log/config1.log --logappend --fork --configsvr --replSet=config
复制集conf配置
/usr/local/mongodb/bin/mongo localhost:3999 rs.initiate({_id: ‘config‘, members: [{_id: 0, host: ‘localhost:3999‘}, {_id: 1, host: ‘localhost:39990‘}]}) rs.isMaster() #查看主从关系
创建Route
/usr/local/mongodb/bin/mongos --port 40000 --bind_ip 0.0.0.0 --configdb config/localhost:3999,localhost:39990 --logpath=/home/mongodb/log/route.log --fork --logappend
设置分片
/usr/local/mongodb/bin/mongo admin --port 40000 use admin db.runCommand({ addshard: ‘localhost:3000‘}) db.runCommand({ addshard: ‘localhost:3001‘}) db.runCommand({ enablesharding: ‘ship_position‘}) db.runCommand({ shardcollection: ‘ship_position.ais‘, key: {receiveTimeLong: 1}}) db.runCommand({ shardcollection: ‘ship_position.gps‘, key: {receiveTimeLong: 1}}) db.runCommand({ shardcollection: ‘ship_position.rfid‘, key: {receiveTimeLong: 1}})
创建用户
use admin db.createUser( { user: "root", pwd: "QWERpoiu1234", roles: [ { role: "readWrite", db: "config" }, "clusterAdmin" ] } )
打开端口
firewall-cmd --zone=public --add-port=40000/tcp --permanent
第二次启动服务
/usr/local/mongodb/bin/mongod --port 3000 --dbpath=/home/mongodb/shard/s0 --logpath=/home/mongodb/log/s0.log --logappend --fork --shardsvr /usr/local/mongodb/bin/mongod --port 3001 --dbpath=/home/mongodb/shard/s1 --logpath=/home/mongodb/log/s1.log --logappend --fork --shardsvr /usr/local/mongodb/bin/mongod --port 3999 --dbpath=/home/mongodb/config/c0 --logpath=/home/mongodb/log/config0.log --logappend --fork --configsvr --replSet=config /usr/local/mongodb/bin/mongod --port 39990 --dbpath=/home/mongodb/config/c1 --logpath=/home/mongodb/log/config1.log --logappend --fork --configsvr --replSet=config /usr/local/mongodb/bin/mongos --port 40000 --bind_ip 0.0.0.0 --configdb config/localhost:3999,localhost:39990 --logpath=/home/mongodb/log/route.log --fork --logappend
以上是关于MongoDB分片实战的主要内容,如果未能解决你的问题,请参考以下文章
2020年MongoDB 企业应用实战 基础 复制集 分片集群