準備基本環境
服務器node1(10.0.3.1) |
服務器node2(10.0.3.2) |
服務器node3(10.0.3.3) |
mongos 20000 |
mongos 20000 |
mongos 20000 |
config server 21000 |
config server 21000 |
config server 21000 |
shard server1 主節點27001 |
shard server1 副節點 27001 |
shard server1 仲裁27001 |
shard server2 仲裁27002 |
shard server2 主節點27002 |
shard server2 副節點27002 |
shard server3 副節點27003 |
shard server3 仲裁27003 |
shard server3 主節點27003 |
搭建步驟
三臺設備均操作
[mongod@mongodb-node-1 ~]$ cat /etc/hosts
10.0.3.1 mongodb-node-1
10.0.3.2 mongodb-node-2
10.0.3.3 mongodb-node-3
[mongod@mongodb-node-1 ~]$ mkdir -p /export/mongodb-4.4.18/cluster/{config,mongos,shard1,shard2,shard3}/{data,logs}
[mongod@mongodb-node-1 ~]$ yum install -y libcurl openssl xz-libs
[root@mongodb-node-1 ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.18.tgz
[root@mongodb-node-1 ~]# tar -xf mongodb-linux-x86_64-rhel70-4.4.18.tgz -C /export/
[root@mongodb-node-1 ~]# cd /export/
[root@mongodb-node-1 export]# mv mongodb-linux-x86_64-rhel70-4.4.18 mongodb-4.4.18
[root@mongodb-node-1 export]# cat /etc/profile.d/mongodb.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/export/mongodb-4.4.18/bin
source /etc/profile.d/mongodb.sh
mongo --version
[root@mongodb-node-1 ~]# useradd -d /export/mongod mongod
[root@mongodb-node-1 ~]# chown mongod:mongod /export/mongodb-4.4.18/ -R
第一臺設備 10.0.3.1 操作
# 準備configServer配置文件
[mongod@mongodb-node-1 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-1 config]$ cat config.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/config/logs/config.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/config/configsrv.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
# 準備 shard1分片配置文件
[mongod@mongodb-node-1 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-1 shard1]$ cat shard1.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
# 準備 shard2分片配置文件
[mongod@mongodb-node-1 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-1 shard2]$ cat shard2.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard2
# 準備shard3分片配置文件
[mongod@mongodb-node-1 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-1 shard3]$ cat shard3.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard3
# 準備 mongos-router配置文件,注意mongos沒有storage部分配置
[mongod@mongodb-node-1 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-1 mongos]$ cat mongos.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/mongos/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000
# 準備configServer配置文件
[mongod@mongodb-node-2 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-2 config]$ cat config.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/config/logs/config.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/config/configsrv.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
# 準備 shard1分片配置文件
[mongod@mongodb-node-2 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-2 shard1]$ cat shard1.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
# 準備 shard2分片配置文件
[mongod@mongodb-node-2 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-2 shard2]$ cat shard2.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard2
# 準備shard3分片配置文件
[mongod@mongodb-node-2 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-2 shard3]$ cat shard3.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard3
# 準備 mongos-router配置文件,注意mongos沒有storage部分配置
[mongod@mongodb-node-2 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-2 mongos]$ cat mongos.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/mongos/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000
# 準備configServer配置文件
[mongod@mongodb-node-3 config]$ cd /export/mongodb-4.4.18/cluster/config
[mongod@mongodb-node-3 config]$ cat config.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/config/logs/config.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/config/configsrv.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
# 準備 shard1分片配置文件
[mongod@mongodb-node-3 shard1]$ cd /export/mongodb-4.4.18/cluster/shard1
[mongod@mongodb-node-3 shard1]$ cat shard1.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
# 準備 shard2分片配置文件
[mongod@mongodb-node-2 shard2]$ cd /export/mongodb-4.4.18/cluster/shard2
[mongod@mongodb-node-2 shard2]$ cat shard2.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard2
# 準備shard3分片配置文件
[mongod@mongodb-node-2 shard3]$ cd /export/mongodb-4.4.18/cluster/shard3
[mongod@mongodb-node-2 shard3]$ cat shard3.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard3
# 準備 mongos-router配置文件,注意mongos沒有storage部分配置
[mongod@mongodb-node-2 mongos]$ cd /export/mongodb-4.4.18/cluster/mongos
[mongod@mongodb-node-2 mongos]$ cat mongos.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/mongos/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000
啓動服務並初始化
- 啓動三臺服務器的config server和配置副本集
# 連接3個節點執行以下命令分別啓動configServer
[mongod@mongodb-node-1 mongos]$ mongod -f /export/mongodb-4.4.18/cluster/config/config.conf
# 連接到第一個節點mongodb-node-1
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 21000
rs.initiate(
{
_id: "config",
configsvr: true,
members: [
{ _id : 0, host : "10.0.3.1:21000" },
{ _id : 1, host : "10.0.3.2:21000" },
{ _id : 2, host : "10.0.3.3:21000" }
]
}
)
# 查看節點狀態
rs.status()
- 三臺服務器啓動shard 分片並初始化shard分片
# 3個節點每個節點執行以下命令啓動3個shard分片副本集
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard1/shard1.conf
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard2/shard2.conf
[mongod@mongodb-node-1 ~]$ mongod -f /export/mongodb-4.4.18/cluster/shard3/shard3.conf
# 連接到第一個shard分片並初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27001
config = { _id : "shard1",members : [{_id : 0, host : "10.0.3.1:27001" ,priority: 2 },{_id : 1, host : "10.0.3.2:27001" ,priority: 1 },{_id : 2, host : "10.0.3.3:27001",arbiterOnly: true}]}//(“priority”優先級,數字越大,優先等級越高;“arbiterOnly”衝裁節點;衝裁節點根據優先等級判斷哪個節點作爲主節點)
rs.initiate(config)
> rs.initiate(config)
{ "ok" : 1 }
shard1:SECONDARY>
#查看節點狀態
rs.status()
# 連接到第二個shard分片並初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27002
config = { _id : "shard2",members : [{_id : 0, host : "10.0.3.1:27002" ,arbiterOnly: true },{_id : 1, host : "10.0.3.2:27002" ,priority: 2 },{_id : 2, host : "10.0.3.3:27002",priority: 1}]}
rs.initiate(config)
# 連接到第三個shard分片並初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1 --port 27003
use admin
config = { _id : "shard3",members : [{_id : 0, host : "10.0.3.1:27003" ,priority: 1 },{_id : 1, host : "10.0.3.2:27003" ,arbiterOnly: true },{_id : 2, host : "10.0.3.3:27003",priority: 2}]}
rs.initiate(config)
# 查看各個分片副本集節點狀態
rs.status()
# 3個節點啓動mongos-router,注意啓動時使用mongos命令而非mongod命令:
[mongod@mongodb-node-1 ~]$ mongos -f /export/mongodb-4.4.18/cluster/mongos/mongos.conf
# 連接到第一個mongos節點mongodb01並初始化
[mongod@mongodb-node-1 ~]$ mongo 10.0.3.1:20000
# 執行以下操作將3個分片副本集添加到集羣:
sh.addShard("shard1/10.0.3.1:27001,10.0.3.2:27001,10.0.3.3:27001")
sh.addShard("shard2/10.0.3.1:27002,10.0.3.2:27002,10.0.3.3:27002")
sh.addShard("shard3/10.0.3.1:27003,10.0.3.2:27003,10.0.3.3:27003")
# 查看mongos狀態
sh.status()
開啓密碼認證
- 登陸 mongo01 10.0.3.1 創建賬號密碼(提前創建好賬號密碼)
[mongod@mongodb-node-1 cluster]$ mongo 10.0.3.1:20000
> use admin
##創建賬號/密碼##
db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });
- 在 mongo01 10.0.3.1 上創建keyfile密鑰文件
[mongod@mongodb-node-1 cluster]$ cd /export/mongodb-4.4.18/cluster
[mongod@mongodb-node-1 cluster]$ openssl rand -base64 756 >keyFile.key
[mongod@mongodb-node-1 cluster]$ chmod 400 keyFile.key
[mongod@mongodb-node-1 cluster]$ scp keyFile.key 10.0.3.2:/export/mongodb-4.4.18/cluster
[mongod@mongodb-node-1 cluster]$ scp keyFile.key 10.0.3.3:/export/mongodb-4.4.18/cluster
- 在 config、shard1、shard2、shard3、mongos添加認證配置
# 要先關閉所有 mongos 進程,修改配置文件,增加認證信息,然後再依次啓動進程
# config 配置文件
[mongod@mongodb-node-1 cluster]$ cat config/config.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/config/logs/config.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/config/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/config/configsrv.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 21000
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
security:
keyFile: /export/mongodb-4.4.18/cluster/keyFile.key
authorization: enabled
# shard1配置文件
[mongod@mongodb-node-1 cluster]$ cat shard1/shard1.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard1/logs/shard1.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard1/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard1/shard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27001
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
security:
keyFile: /export/mongodb-4.4.18/cluster/keyFile.key
authorization: enabled
# shard2配置文件
[mongod@mongodb-node-1 cluster]$ cat shard2/shard2.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard2/logs/shard2.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard2/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard2/shard2.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27002
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard2
security:
keyFile: /export/mongodb-4.4.18/cluster/keyFile.key
authorization: enabled
# shard3配置文件
[mongod@mongodb-node-1 cluster]$ cat shard3/shard3.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/shard3/logs/shard3.log
# Where and how to store data.
storage:
dbPath: /export/mongodb-4.4.18/cluster/shard3/data
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 2
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/shard3/shard3.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27003
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard3
security:
keyFile: /export/mongodb-4.4.18/cluster/keyFile.key
authorization: enabled
# mongos 配置文件,注意沒有 authorization: enabled
[mongod@mongodb-node-1 cluster]$ cat mongos/mongos.conf
systemLog:
destination: file
logAppend: true
path: /export/mongodb-4.4.18/cluster/mongos/logs/mongos.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /export/mongodb-4.4.18/cluster/mongos/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: 0.0.0.0
sharding:
configDB: config/10.0.3.1:21000,10.0.3.2:21000,10.0.3.3:21000
security:
keyFile: /export/mongodb-4.4.18/cluster/keyFile.key
mongod -f /export/mongodb-4.4.18/cluster/config/config.conf
mongod -f /export/mongodb-4.4.18/cluster/shard1/shard1.conf
mongod -f /export/mongodb-4.4.18/cluster/shard2/shard2.conf
mongod -f /export/mongodb-4.4.18/cluster/shard3/shard3.conf
mongos -f /export/mongodb-4.4.18/cluster/mongos/mongos.conf
# 按照先後順序來處理關閉,mongos>config>shadr3>shadr2>shadr1
mongodb使用方法
# mongodb 使用方法:
1.查看數據分佈
use testdb;
show tables;
db.order.getShardDistribution()
2.創建片健
在 指定testdb分片生效
sh.enableSharding("testdb")
3.片鍵有兩種模式:hash模式,range模式
使用hash模式分片: 記錄在各片上的分佈比較平均
sh.shardCollection( "testdb.person", { "_id": "hashed" } )
使用range模式分片
mongos> sh.shardCollection( "testdb.person", { "name": 1 } )
要分片的庫原來有數據的情況下,先建index,然後再指定片鍵
mongos> sh.enableSharding("new2")
mongos> use new2
mongos> db.user2.createIndex( { "username": 1 } )
mongos> sh.shardCollection( "new2.user2", { "username": 1 } )
發片後,片健不可改變;一個集合只能有一個片健;片健必須有索引
4.分片的元數據
不要直接到config server上查看分片集羣的元數據信息,這些數據非常重要,安全的方式是通過mongos連接到config數據查看,或者使用sh輔助函數查看。
使用sh輔助函數查看
連接到mongos查看config數據庫中的集合
mongos> use config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/10.0.3.1:27001,10.0.3.2:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/10.0.3.2:27002,10.0.3.3:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/10.0.3.1:27003,10.0.3.3:27003", "state" : 1 }
databases集合保存分片集羣中所有數據庫的信息,不管數據庫是否分片
mongos> db.databases.find()
{ "_id" : "testdb", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("b748ce93-b28d-43ad-a0fa-843813d9e9ff"), "lastMod" : 1 } }
{ "_id" : "test", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("c98e8734-92ad-4644-968d-d5aed0044e89"), "lastMod" : 1 } }
測試
#連接任意一臺mongos節點
mongo -u admin --port 20000
use admin
#爲數據庫啓用分片
爲testdb數據庫啓用分片
sh.enableSharding("testdb")
#爲集合啓用分片
爲order集合設置分片規則
sh.shardCollection("testdb.order", {"_id": "hashed" })
#插入100000條數據進行驗證
use testdb
for (i = 1; i <= 100000; i=i+1){
db.order.insert({'price': 1})
}
#查看插入的數據量
db.order.find().count()
#查看分片情況
db.order.stats();
"ns" : "testdb.order",
"count" : 99999,
"size" : 8388810,
"storageSize" : 4153344,
"totalIndexSize" : 9805824,
"totalSize" : 13959168,
"indexSizes" : {
"_id_" : 4915200,
"_id_hashed" : 4890624
},
"avgObjSize" : 83,
"maxSize" : NumberLong(0),
"nindexes" : 2,
"scaleFactor" : 1,
"nchunks" : 6,
"shards" : {
"shard1" : {
"ns" : "testdb.order",
"size" : 2757979,
"count" : 32877,
"avgObjSize" : 83,
"storageSize" : 1368064,
"freeStorageSize" : 679936,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
}
.........
"shard2" : {
"ns" : "testdb.order",
"size" : 2816622,
"count" : 33576,
"avgObjSize" : 83,
"storageSize" : 1396736,
"freeStorageSize" : 692224,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
}
.........
"shard3" : {
"ns" : "testdb.order",
"size" : 2814209,
"count" : 33546,
"avgObjSize" : 83,
"storageSize" : 1388544,
"freeStorageSize" : 688128,
"capped" : false,
"wiredTiger" : {
"metadata" : {
"formatVersion" : 1
}
.........
以看到數據分到3個分片,各自分片數量爲: shard1 “count” : 32877,shard2 “count” : 33576,shard3 “count” : 33547
https://blog.csdn.net/msh6453/article/details/131161845
https://blog.csdn.net/weixin_49724150/article/details/121748365