mongdb分片集群搭建
时间: 2023-12-04 19:40:05 浏览: 132
以下是MongoDB分片集群搭建的步骤:
1.启动config server
```shell
mongod --configsvr --replSet configReplSet --dbpath /data/configdb --port 27019
```
2.初始化config server副本集
```shell
mongo --port 27019
config = {
"_id" : "configReplSet",
"members" : [
{
"_id" : 0,
"host" : "localhost:27019"
}
]
}
rs.initiate(config)
```
3.启动mongos
```shell
mongos --configdb configReplSet/localhost:27019 --port 27017
```
4.启动shard1
```shell
mongod --shardsvr --replSet shard1ReplSet --dbpath /data/shard1 --port 27018
```
5.初始化shard1副本集
```shell
mongo --port 27018
config = {
"_id" : "shard1ReplSet",
"members" : [
{
"_id" : 0,
"host" : "localhost:27018"
}
]
}
rs.initiate(config)
```
6.启动shard2
```shell
mongod --shardsvr --replSet shard2ReplSet --dbpath /data/shard2 --port 27019
```
7.初始化shard2副本集
```shell
mongo --port 27019
config = {
"_id" : "shard2ReplSet",
"members" : [
{
"_id" : 0,
"host" : "localhost:27019"
}
]
}
rs.initiate(config)
```
8.将shard1和shard2添加到mongos中
```shell
mongo --port 27017
sh.addShard("shard1ReplSet/localhost:27018")
sh.addShard("shard2ReplSet/localhost:27019")
```
9.创建分片的数据库和集合
```shell
use test
sh.enableSharding("test")
db.createCollection("testCollection")
sh.shardCollection("test.testCollection", {"_id": "hashed"})
```
阅读全文