安装前准备
1.准备3台服务器
3台服务器地址为rs0,rs1,rs2
2.生成密钥文件(用于集群内部安全认证)
openssl rand -base64 756 -out ./mongodb-keyfile
chmod 400 ./mongodb-keyfile
密钥文件分别放于3台服务器/data/keys/目录下
3.安装mongodb数据库
mongodb安装参见:https://blog.csdn.net/qq_35344198/article/details/102594425
集群配置
1.集群结构
分片(shard_30001,shard_30002,shard_30003):每个分片包含分片数据的子集,每个分片可以部署为副本集
mongos(mongos_40000):查询路由器,提供客户端应用程序和分片集群直接的接口,可部署单台或多台,本例子将在rs0,rs1,rs2服务器部署
config server(config_20000):配置服务器,存储集群的元数据和配置设置,必须部署为副本集
2.配置服务器配置文件
# in /etc/mongodb/mongod_config_20000.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/log/mongodb/mongod_config_20000.log
# Where and how to store data.
storage:
dbPath: /data/lib/mongodb/mongod_config_20000
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod_config_20000.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 20000
bindIp: rs0 # rs1,rs2服务器的配置改成相应服务器地址
security:
authorization: enabled
keyFile: /data/keys/mongodb-keyfile
#operationProfiling:
replication:
replSetName: configs
sharding:
clusterRole: configsvr
3.分片配置文件
# in /etc/mongodb/mongod_shard_30001.conf
# shard_30002,shard_30003配置与shard_30001一致,仅修改端口号
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/log/mongodb/mongod_shard_30001.log
# Where and how to store data.
storage:
dbPath: /data/lib/mongodb/mongod_shard_30001
journal:
enabled: true
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod_shard_30001.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 30001
bindIp: rs0 # rs1,rs2服务器的配置改成相应服务器地址
security:
authorization: enabled
keyFile: /data/keys/mongodb-keyfile
#operationProfiling:
replication:
replSetName: shard_30001
sharding:
clusterRole: shardsvr
4.路由配置文件
# in /etc/mongodb/mongod_mongos_40000.conf
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/log/mongodb/mongod_mongos_40000.log
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod_mongos_40000.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 40000
bindIp: rs0 # rs1,rs2服务器的配置改成相应服务器地址
security:
keyFile: /data/keys/mongodb-keyfile
#operationProfiling:
sharding:
configDB: configs/rs0:20000,rs1:20000,rs2:20000
集群搭建
1.配置服务器副本集配置
启动配置服务器,暂不开启用户权限验证与密钥验证
// rs0,rs1,rs2服务器上分别启动配置服务器
sudo mongod --config /etc/mongodb/mongod_config_20000.conf
配置副本集
// rs0,rs1,rs2任意一台服务器上连接配置服务器
mongo --host rs0 --port 20000
// 初始化副本集:
rs.initiate(
{
_id: 'configs',
configsvr: true,
members: [
{ _id : 0, host : 'rs0:20000' },
{ _id : 1, host : 'rs1:20000' },
{ _id : 2, host : 'rs2:20000' }
]
}
)
rs.status()
// 创建本地用户:
use admin
db.createUser(
{
user: 'root',
pwd: '123456',
roles: [ { role: 'root', db: 'admin' } ]
}
)
开启权限验证与密钥验证,重启配置服务器副本集
2.分片副本集配置
启动shard_30001,暂不开启用户权限验证与密钥验证
// rs0,rs1,rs2服务器上分别启动shard_30001
sudo mongod --config /etc/mongodb/mongod_shard_30001.conf
配置副本集
// rs0,rs1,rs2任意一台服务器上连接配置服务器
mongo --host rs0 --port 30001
// 初始化副本集:
rs.initiate(
{
_id: 'shard_30001',
members: [
{ _id : 0, host : 'rs0:30001' },
{ _id : 1, host : 'rs1:30001' },
{ _id : 2, host : 'rs2:30001' }
]
}
)
rs.status()
// 创建本地用户:
use admin
db.createUser(
{
user: 'root',
pwd: '123456',
roles: [ { role: 'root', db: 'admin' } ]
}
)
开启权限验证与密钥验证,重启配置服务器副本集
用同样方式配置shard_30002,shard_30003
3.路由器副本集配置
启动路由器
// rs0,rs1,rs2服务器上分别启动路由器
sudo mongos --config /etc/mongodb/mongod_mongos_40000.conf
添加分片
// rs0,rs1,rs2任意一台服务器上连接配置服务器
mongo --host rs0 --port 40000
// 用户验证
use admin
db.auth('root', '123456')
// 添加分片
db.runCommand( { addShard: 'shard_30001/rs1:30001,rs0:30001,rs2:30001'} )
db.runCommand( { addShard: 'shard_30002/rs1:30002,rs0:30002,rs2:30002'} )
db.runCommand( { addShard: 'shard_30003/rs1:30003,rs0:30003,rs2:30003'} )
分片常用命令
db.runCommand( { addShard: '<replica_set>/<hostname><:port>,<hostname><:port>, ...' } ) // 添加分片
db.runCommand( { removeShard : 'shard_name' } ) // 移除分片
sh.status() // 查看分片信息
sh.enableSharding('db') // 数据库启用分片
sh.shardCollection('db.collection', { key : 'hashed' } ) // 散列分片
sh.shardCollection('db.collection', { 'key1' : 1, 'key2' : 1) // 范围分片
sh.addShardTag('shard_name', 'zone') // 给分片添加区域
sh.removeShardTag('shard_name', 'zone') // 移除分片的区域
sh.addTagRange('db.collection', { key: 'value_min' }, { key: 'value_max' }, 'zone') // 添加区域范围
sh.removeTagRange('db.collection', { key: 'value_min' }, { key: 'value_max' }, 'zone') // 删除区域范围