首先我们计划启动了三个mongo服务:master,salve,arbiter
1.准备工作
新建文件夹如图(每个文件夹下面有db和configdb文件夹):
生成认证文件并修改权限
openssl rand -base64 100 > /root/docker/mongo/keyfile0 --文件内容采base64编码,一共100个字符
chmod 600 /root/docker/mongo/keyfile0
手动拷贝keyfile0到configdb目录, 然后验证权限:
chmod 600 /root/docker/mongo/master/configdb/keyfile0
chmod 600 /root/docker/mongo/salve/configdb/keyfile0
chmod 600 /root/docker/mongo/arbiter/configdb/keyfile0
2.启动3个mongodb:
docker run -d --name mongo-m -v /root/docker/mongo/master/db/:/data/db -v /root/docker/mongo/master/configdb/:/data/configdb --restart=always -p 27017:27017 mongo:latest mongod --dbpath /data/db --replSet mongoreplset --oplogSize 128 --auth --keyFile=/data/configdb/keyfile0
docker run -d --name mongo-s -v /root/docker/mongo/salve/db/:/data/db -v /root/docker/mongo/salve/configdb/:/data/configdb --restart=always -p 27018:27017 mongo:latest mongod --dbpath /data/db --replSet mongoreplset --oplogSize 128 --auth --keyFile=/data/configdb/keyfile0
docker run -d --name mongo-a -v /root/docker/mongo/arbiter/db/:/data/db -v /root/docker/mongo/arbiter/configdb/:/data/configdb --restart=always -p 27019:27017 mongo:latest mongod --dbpath /data/db --replSet mongoreplset --oplogSize 128 --smallfiles --auth --keyFile=/data/configdb/keyfile0
启动后如图:
3.集群配置
docker exec -it mongo-m mongo
config = {_id:"mongoreplset", version:1, members:[{_id:0, host:"192.168.100.3:27017", priority:5}, {_id:1, host:"192.168.100.3:27018", priority:2}, {_id:2, host:"192.168.100.3:27019", priority:3}]}
rs.initiate(config)
效果如图:
4.添加用户
use admin
db.createUser( { user: "root",pwd: "root",roles: [ "clusterAdmin","userAdminAnyDatabase","dbAdminAnyDatabase","readWriteAnyDatabase" ] } )
效果如图:
在主数据库插入数据
查看从库
分片:
创建配置服务复制集
docker run --name mongoc1 -d mongo:latest --configsvr --replSet "rs_configsvr" --bind_ip_all
docker run --name mongoc2 -d mongo:latest --configsvr --replSet "rs_configsvr" --bind_ip_all
通过 docker inspect 找到三个配置服务实例的 IP。比如 docker inspect mongoc1 | grep IPAddress,又由于--configsvr 的默认端口为 27019。所以配置服务的地址为
mongoc1:172.17.0.9
mongoc2:172.17.0.10
初始化
docker exec -it mongoc1 mongo --host 172.17.0.9 --port 27019 use admin rs.initiate( { _id: "rs_configsvr", configsvr: true, members: [ { _id : 0, host : "172.17.0.9:27019" }, { _id : 1, host : "172.17.0.10:27019" } ] } )
连接字符串为
mongodb://172.17.0.9:27019,172.17.0.10:27019/test?replicaSet=rs_configsvr
创建分片复制集
与上面同理,不再赘述,直接上命令行。
docker run --name mongors00 -d mongo:latest --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name mongors01 -d mongo:latest --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name mongors02 -d mongo:latest --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name mongors10 -d mongo:latest --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
docker run --name mongors11 -d mongo:latest --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
docker run --name mongors12 -d mongo:latest --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
mongors00:172.17.0.8
mongors01:172.17.0.11
mongors02:172.17.0.12
mongors10:172.17.0.13
mongors11:172.17.0.14
mongors12:172.17.0.15
docker exec -it mongoc1 mongo --host 172.17.0.8 --port 27018 use admin rs.initiate( { _id : "rs_shardsvr0", members: [ { _id : 0, host : "172.17.0.8:27018" }, { _id : 1, host : "172.17.0.11:27018" }, { _id : 2, host : "172.17.0.12:27018" } ] } )
docker exec -it mongoc1 mongo --host 172.17.0.13 --port 27018 use admin rs.initiate( { _id : "rs_shardsvr1", members: [ { _id : 0, host : "172.17.0.13:27018" }, { _id : 1, host : "172.17.0.14:27018" }, { _id : 2, host : "172.17.0.15:27018" } ] } )
连接字符串为
mongodb://172.17.0.8:27018,172.17.0.11:27018,172.17.0.12:27018/test?replicaSet=rs_shardsvr0
mongodb://172.17.0.13:27018172.17.0.14:27018,172.17.0.15:27018/test?replicaSet=rs_shardsvr1
连接 mongos 到分片集群
由于镜像的默认入口是 mongod,所以要通过 --entrypoint "mongos" 将其改为 mongos:
docker run --name mongos0 -d --entrypoint "mongos" mongo:latest --configdb rs_configsvr/172.17.0.9:27019,172.17.0.10:27019 --bind_ip_all
地址为 172.17.0.16:27017
增加分片到集群&数据库启用分片
docker exec -it mongoc1 mongo --host 172.17.0.16 --port 27017 use admin sh.addShard("rs_shardsvr0/172.17.0.8:27018,172.17.0.11:27018,172.17.0.12:27018") sh.addShard("rs_shardsvr1/172.17.0.13:27018,172.17.0.14:27018,172.17.0.15:27018") sh.enableSharding("test")
如图:
分片 Collection
对 test.order 的 _id 字段进行哈希分片&插入数据:
sh.shardCollection("test.order", {"_id": "hashed" })
use test
for (i = 1; i <= 1000; i=i+1){
db.order.insert({'price': 1})
}
查看数据分布
mongos> db.order.find().count()
1000
rs_shardsvr0:PRIMARY> db.order.find().count()
527
rs_shardsvr1:PRIMARY> db.order.find().count()
473
参考文章:
https://www.jianshu.com/p/c3811263fd3a
https://www.cnblogs.com/silentjesse/p/4676440.html
https://www.cnblogs.com/hehexiaoxia/p/6192796.html
https://www.cnblogs.com/jay54520/p/8444218.html