1、环境描述
172.18.62.23 node1 shard服务器
172.18.62.24 node2 shard服务器
172.18.62.25 node3 配置服务器
172.18.62.26 node4 配置服务器
172.18.62.27 node5 路由服务器
2、启动容器
bs create mongodb
bs ps
3、进入容器
docker exec -it mongodb_node1_1 bash
docker exec -it mongodb_node2_1 bash
docker exec -it mongodb_node3_1 bash
docker exec -it mongodb_node4_1 bash
docker exec -it mongodb_node5_1 bash
4、分别在容器安装mongodb
apt update
apt upgrade
apt install mongodb -y
5、修改配置文件
node1
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
shardsvr=true
journal=true
node2
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
shardsvr=true
journal=true
启动服务
/etc/init.d/mongodb start
node3
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
replSet = configsrv/172.18.62.26:27017
configsvr=true
journal=true
node4
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
replSet = configsrv/172.18.62.25:27017
configsvr=true
journal=true
启动服务
/etc/init.d/mongodb start
初始化副本集
>config =
{
_id: 'configsrv',
members:
[
{
"_id":1,
"host":"172.18.62.25:27017",
priority: 3
},
{
"_id":2,
"host":"172.18.62.26:27017",
priority: 2
}
]
}
node5
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
configdb=configsrv/172.18.62.25:27017,172.18.62.26:27017
启动服务
mongos -f /etc/mongodb.conf &
root@mongodb-node5:~# netstat -antp |grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1187/mongos
tcp 0 0 172.18.62.27:39072 172.18.62.25:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:39062 172.18.62.25:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:39074 172.18.62.25:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:44610 172.18.62.26:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:44616 172.18.62.26:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:44620 172.18.62.26:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:44614 172.18.62.26:27017 ESTABLISHED 1187/mongos
tcp 0 0 172.18.62.27:39068 172.18.62.25:27017 ESTABLISHED 1187/mongos
5、登录路由服务器,添加分片服务器
use admin
db.runCommand({addshard: "172.18.62.23:27017",allowLocal:true})
db.runCommand({addshard: "172.18.62.24:27017",allowLocal:true})
6、在路由服务器上设置针对哪个数据库进行分片
db.runCommand({enablesharding: "testdb"})
db.runCommand({shardcollection: "testdb.tb01",key:{id:1}})
7、测试结果,连接路由服务器存储数据
use testdb
function add(){
for(var i=0;i<20000;i++){
db.tb01.insert({"id":i+10,"name":"jim"})
}
}
add()
8、查看分片
use testdb
sh.status()
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("60a387f5b498bbf740fe514e")
}
shards:
{ "_id" : "shard0000", "host" : "172.18.62.23:27017", "state" : 1 }
{ "_id" : "shard0001", "host" : "172.18.62.24:27017", "state" : 1 }
active mongoses:
"3.6.8" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
1 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard0000 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
{ "_id" : "db02", "primary" : "shard0001", "partitioned" : true }
db02.tb01
shard key: { "id" : "hashed" }
unique: false
balancing: true
chunks:
shard0000 2
shard0001 2
{ "id" : { "$minKey" : 1 } } -->> { "id" : NumberLong("-4611686018427387902") } on : shard0000 Timestamp(2, 2)
{ "id" : NumberLong("-4611686018427387902") } -->> { "id" : NumberLong(0) } on : shard0000 Timestamp(2, 3)
{ "id" : NumberLong(0) } -->> { "id" : NumberLong("4611686018427387902") } on : shard0001 Timestamp(2, 4)
{ "id" : NumberLong("4611686018427387902") } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(2, 5)
{ "_id" : "testdb", "primary" : "shard0000", "partitioned" : true }
testdb.tb01
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)