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

 

转载于:https://www.cnblogs.com/song-wentao/p/10716571.html

你可能感兴趣的:(MongoDB分片实战)