本例只有一个mongod进程,一个mongos服务器,一个分片服务器(且为单成员副本集)
1.创建文件夹 我们需首先创建存放源数据的文件夹configsvr、存放分片数据的文件夹shardsvr、存放日志的文件夹logs
/usr/local/mongodb4.4.1/datashard$ mkdir configsvr
/usr/local/mongodb4.4.1/datashard$ mkdir logs
/usr/local/mongodb4.4.1/datashard$ mkdir shardsvr
将所有文件夹修改用户组
作者的用户名为tarena
命令如下:
/usr/local/mongodb4.4.1/datashard$ sudo chown -R tarena:tarena configsvr
/usr/local/mongodb4.4.1/datashard$ sudo chown -R tarena:tarena logs
/usr/local/mongodb4.4.1/datashard$ sudo chown -R tarena:tarena shardsvr
2.创建对应的配置文件
/usr/local/mongodb4.4.1/conf/svr$ vim configsvr.conf
/usr/local/mongodb4.4.1/conf/svr$ vim mongos.conf
/usr/local/mongodb4.4.1/conf/svr$ vim shardsvr.conf
配置文件内容如下: configsvr.conf
storage:
dbPath: /usr/local/mongodb4.4.1/datashard/configsvr
journal:
enabled: true
systemLog:
destination: file
path: /usr/local/mongodb4.4.1/datashard/logs/configsvr.log
logAppend: true
net:
bindIp: localhost
port: 27023
processManagement:
fork: true
sharding:
clusterRole: configsvr
replication:
replSetName: configsvr_rs
mongos.conf
systemLog:
destination: file
path: /usr/local/mongodb4.4.1/datashard/logs/mongos.log
logAppend: true
net:
bindIp: localhost
port: 27024
processManagement:
fork: true
sharding:
configDB: configsvr_rs/localhost:27023
shardsvr.conf
storage:
dbPath: /usr/local/mongodb4.4.1/datashard/shardsvr
journal:
enabled: true
systemLog:
destination: file
path: /usr/local/mongodb4.4.1/datashard/logs/shardsvr.log
logAppend: true
net:
bindIp: localhost
port: 27025
processManagement:
fork: true
sharding:
clusterRole: shardsvr
replication:
replSetName: shardsvr_rs1
3.开启服务 开启config服务器
/usr/local/mongodb4.4.1/conf/svr$ sudo mongod --config configsvr.conf
~$ mongo --host localhost --port 27023
单成员副本集初始化(配置集群)
> rs.initiate({
... _id:"configsvr_rs",
... configsvr: true,
... members:[{_id:0,host:"localhost:27023"}]
... })
开启mongos服务器
/usr/local/mongodb4.4.1/conf/svr$ sudo mongos --config mongos.conf //显示警告配置服务器少于三个,不用担心
~$ mongo --host localhost --port 27024
开启分片服务器
/usr/local/mongodb4.4.1/conf/svr$ sudo mongod --config shardsvr.conf
~$ mongo --host localhost --port 27025
单成员副本集初始化(分片集群)
> rs.initiate({
... _id:"shardsvr_rs1",members:[{_id:0,host:"localhost:27025"}]
... })
在mongos的shell中,添加分片服务器
mongos> sh.addShard("shardsvr_rs1/localhost:27025")
4.数据库,集合启动分片 在mongos shell中创建数据库
mongos> use people; //创建数据库
mongos> sh.enableSharding("people") //为people数据库启用分片
mongos> db.createCollection("records") //创建集合
mongos> db.records.insertMany([{name:"jack",age:23},{name:"lucy",age:20}]) //插入数据
mongos> db.records.createIndex({"name":1}) //创建索引
mongos> sh.shardCollection("people.records",{"name":1}) //以name为分片键,进行范围分片
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("619b8388e3f4442ccd1be4f6")
}
shards:
{ "_id" : "shardsvr_rs1", "host" : "shardsvr_rs1/localhost:27025", "state" : 1 }
active mongoses:
"4.4.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
······
到分片服务器中查看数据
shardsvr_rs1:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
people 0.000GB
shardsvr_rs1:PRIMARY> use people
switched to db people
shardsvr_rs1:PRIMARY> show tables
records
shardsvr_rs1:PRIMARY> db.records.find()
{ "_id" : ObjectId("619b8a7ddc36a9ffe38c91dc"), "name" : "jack", "age" : 23 }
{ "_id" : ObjectId("619b8a7ddc36a9ffe38c91dd"), "name" : "lucy", "age" : 20 }
5.可选:再添加分片到集群 创建文件夹
/usr/local/mongodb4.4.1/datashard$ mkdir shardsvr2
/usr/local/mongodb4.4.1/datashard$ sudo chown -R tarena:tarena shardsvr
创建配置文件
/usr/local/mongodb4.4.1/conf/svr$ vim shardsvr.conf
storage:
dbPath: /usr/local/mongodb4.4.1/datashard/shardsvr2
journal:
enabled: true
systemLog:
destination: file
path: /usr/local/mongodb4.4.1/datashard/logs/shardsvr.log
logAppend: true
net:
bindIp: localhost
port: 27026
processManagement:
fork: true
sharding:
clusterRole: shardsvr
replication:
replSetName: shardsvr_rs2
启动分片服务器
/usr/local/mongodb4.4.1/conf/svr$ sudo mongod --config shardsvr2.conf
~$ mongo --host localhost --port 27026
单成员副本集初始化(分片集群)
> rs.initiate({_id:"shardsvr_rs2",members:[{_id:0,host:"localhost:27026"}]
... })
添加到集群
mongos> sh.addShard("shardsvr_rs2/localhost:27026")
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("619b8388e3f4442ccd1be4f6")
}
shards:
{ "_id" : "shardsvr_rs1", "host" : "shardsvr_rs1/localhost:27025", "state" : 1 }
{ "_id" : "shardsvr_rs2", "host" : "shardsvr_rs2/localhost:27026", "state" : 1 }
······
退出集群
shardsvr_rs1:PRIMARY> use admin
switched to db admin
shardsvr_rs1:PRIMARY> db.shutdownServer()
server should be down...
> quit()
|