MongoDB分片集搭建
>s随着业务的发展,数据量的递增,单表的数据量越来越大。查询速度越来越慢。
MongoDB方面,可以做动态的扩容,再增加一个复制集,做分片集群。
将数据量大的集合,根据特定字段进行分片。由于MongoDB分片是基于表,所以同一库内,只需要分表就可以,无需分库。
### 1.创建并编辑配置文件
在服务器初始搭建的时候,已经将所有配置文件建好`(csvr.conf)`,此处直接进行编辑。
```
vim etc/csvr.conf
systemLog:
destination: file
path: "/root/mongodb/data/logs/csvr.log"
logAppend: true
storage:
journal:
enabled: true
dbPath: "/root/mongodb/data/db/csvr"
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
indexConfig:
prefixCompression: true
net:
bindIp: 0.0.0.0
port: 27019
replication:
oplogSizeMB: 2048
replSetName: csvr
sharding:
clusterRole: configsvr
processManagement:
fork: true
press ESC
:wq #退出编辑
```
### 2.启动配置
```
/root/mongodb/bin/mongod -f /root/mongodb/data/etc/csvr.conf 执行文件
```
**上述配置文件,在每台服务器上编辑并运行。**
### 3.添加分片集
任意服务器切换到27019(配置文件中的端口)节点初始化。
```
rs.initiate({
_id: “csvr”,
configsvr: true,
members:[
{_id: 0, host: “101.200.85.138:27019”},
{_id: 1, host: “47.98.208.3:27019”},
{_id: 2, host: “8.129.36.127:27019”},
..... // 有多少个节点,配置多少个
]
});
```