https://www.mongodb.com/download-center#community
下载后解压 三台服务器都配置
mkdir conf data log pid
cat conf/shard11.conf
dbpath=/usr/local/mongodb/data/shard11
logpath=/usr/local/mongodb/log/shard11.log
pidfilepath=/usr/local/mongodb/pid/shard11.pid
directoryperdb=true
logappend=true
replSet=shard1
port=10011
fork=true
shardsvr=true
journal=true
cat conf/shard21.conf
dbpath=/usr/local/mongodb/data/shard21
logpath=/usr/local/mongodb/log/shard21.log
pidfilepath=/usr/local/mongodb/pid/shard21.pid
directoryperdb=true
logappend=true
replSet=shard2
port=10021
fork=true
shardsvr=true
journal=true
cat conf/config.conf
dbpath=/usr/local/mongodb/data/config
logpath=/usr/local/mongodb/log/config.log
pidfilepath=/usr/local/mongodb/pid/config.pid
directoryperdb=true
logappend=true
port=10031
fork=true
configsvr=true
replSet=configRS
journal=true
cat conf/route.conf
configdb=configRS/192.168.1.5:10031,192.168.1.6:10031,192.168.1.2:10031
pidfilepath=/usr/local/mongodb/pid/route.pid
port=10040
logpath=/usr/local/mongodb/log/route.log
logappend=true
fork=true
在每一台服务器分别启动配置服务器
./bin/mongod -f conf/config.conf
连接到任意一台配置服务器上
./bin/mongo --port 10031 --host 127.0.0.1
创建配置服务器副本集 host为其他服务器的 config角色
rs.initiate({_id:"configRS",configsvr:true,members:[{_id:0,host:"192.168.1.5:10031"},
{_id:1,host:"192.168.1.6:10031"},{_id:2,host:"192.168.1.2:10031"}]})
在每一台服务器分别启动分片及副本集 可以有多个shard分片
./bin/mongod -f conf/shard11.conf
./bin/mongod -f conf/shard21.conf
连接任意一台分片服务器
shard1
./bin/mongo --port 10011 --host 127.0.0.1
运行 rs.initiate({_id:"shard1",members:[{_id:0,host:"192.168.1.5:10011"},{_id:1,host:"192.168.1.6:10011"},
{_id:2,host:"192.168.1.2:10011"}]}) arbiterOnly:true 参数为角色是仲裁节点
shard2
./bin/mongo --port 10021 --host 127.0.0.1
运行 rs.initiate({_id:"shard2",members:[{_id:0,host:"192.168.1.5:10021"},{_id:1,host:"192.168.1.6:10021"},
{_id:2,host:"192.168.1.2:10021"}]})
在每一台服务器分别启动路由服务
./bin/mongos -f conf/route.conf
./bin/mongos --host 127.0.0.1--port 10040
use admin
db.runCommand({addshard:"shard1/192.168.1.5:10011,192.168.1.6:10011,192.168.1.2:10011"})
db.runCommand({addshard:"shard2/192.168.1.5:10021,192.168.1.6:10021,192.168.1.2:10021"})
db.runCommand({ listshards:1 }) 查看配置是否生效(仲裁不被列出 )
db.runCommand({enablesharding:"friends"}); # friends 库开启shard
db.runCommand( { shardcollection : "friends.user",key : {id: 1},unique : true } ) # user 表来做分片,
片键为 id 且唯一 #1个分片 保存了1条,另一个分片保存了剩下的所有记录
db.runCommand( { shardcollection : "friends.user",key : {id: "hashed"} } ) #保证数据分布均衡
插入数据测试
use runoob 建立库
db.runoob.insert({"name":"菜鸟教程"})
show dbs
db.runoob.find(); 列出数据
rs.status() shard config 执行
db.status() route执行