docker run -p 27017:27017 -v /Users/mac/Documents/docker/mongo/shard:/opt/ansible/ansible/shard_cluster -itd --privileged --name centos-mongo mongo:shard /usr/sbin/init
var cfg ={"_id":“configsvr”,
“members”:[
{"_id":1,“host”:“172.17.0.3:17017”},
{"_id":2,“host”:“172.17.0.3:17018”},
{"_id":3,“host”:“172.17.0.3:17019”}]
};
dbpath=shard/shard1/shard1-37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=shard/shard1/shard1-37017.log
replSet=shard1
shardsvr=true
var cfg ={"_id":“shard1”, “protocolVersion” : 1,
“members”:[
{"_id":1,“host”:“172.17.0.3:37017”},
{"_id":2,“host”:“172.17.0.3:37018”},
{"_id":3,“host”:“172.17.0.3:37019”} ]
};
dbpath=shard/shard2/shard2-47017
bind_ip=0.0.0.0
port=47017
fork=true
logpath=shard/shard2/logs/shard2-47017.log
replSet=shard2
shardsvr=true
var cfg ={"_id":“shard2”, “protocolVersion” : 1,
“members”:[
{"_id":1,“host”:“172.17.0.3:47017”},
{"_id":2,“host”:“172.17.0.3:47018”},
{"_id":3,“host”:“172.17.0.3:47019”} ]
};
var cfg ={"_id":“shard3”, “protocolVersion” : 1,
“members”:[
{"_id":1,“host”:“172.17.0.3:57017”},
{"_id":2,“host”:“172.17.0.3:57018”},
{"_id":3,“host”:“172.17.0.3:57019”} ]
};
var cfg ={"_id":“shard4”, “protocolVersion” : 1,
“members”:[
{"_id":1,“host”:“172.17.0.3:58017”},
{"_id":2,“host”:“172.17.0.3:58018”},
{"_id":3,“host”:“172.17.0.3:58019”} ]
};
rs.initiate(cfg)
port=27017
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/172.17.0.3:17017,172.17.0.3:17018,172.17.0.3:17019
sh.addShard(“shard1/172.17.0.3:37017,172.17.0.3:37018,172.17.0.3:37019”);
sh.addShard(“shard2/172.17.0.3:47017,172.17.0.3:47018,172.17.0.3:47019”);
sh.addShard(“shard3/172.17.0.3:57017,172.17.0.3:57018,172.17.0.3:57019”);
sh.addShard(“shard4/172.17.0.3:58017,172.17.0.3:58018,172.17.0.3:58019”);
为数据库开启分片功能
sh.enableSharding(“lg_resume”)
为指定集合开启分片功能
sh.shardCollection(“lg_resume.lg_resume_datas”,{“name”:“hashed”})
for(var i=1;i<= 1000;i++){ db.lg_resume_datas.insert({“name”:“test”+i, salary:(Math.random()*20000).toFixed(2)}); }
db.dropUser(“root”)
db.createUser( {user:“root”, pwd:“123456”, roles:[{role:“root”,db:“admin”}] })
db.createUser({user:“lagou_gx”,pwd:“abc321”,roles:[{role:“readWrite”,db:“lagou_resume”}]})
db.createUser({user:“lagou_gx”,pwd:“abc321”,roles:[{role:“readWrite”,db:“lg_resume”}]})
db.auth(“root”,“123456”)
db.auth(“lagou_gx”,“abc321”)
openssl rand -base64 756 > data/mongodb/testKeyFile.file
chmod 600 data/mongodb/testKeyFile.file
/opt/ansible/ansible/shard_cluster