os-7.x版本
mongo-3.6
192.168.1.10 config server shard1主节点/2副节点/3仲裁节点 mongos
192.168.1.11 config server shard1仲裁节点/2主节点/3副节点 mongos
192.168.1.12 config server shard1副节点/2仲裁节点/3主节点 mongos
端口分配
mongos: 27010
config: 27020
shard1: 27030
shard2: 27031
shard3: 27032
开放端口-一般不设置也可以
firewall-cmd —add-port=27010 —permanent
firewall-cmd —add-port=27010
firewall-cmd —add-port=27020 —permanent
firewall-cmd —add-port=27020
firewall-cmd —add-port=27030 —permanent
firewall-cmd —add-port=27030
firewall-cmd —add-port=27031 —permanent
firewall-cmd —add-port=27031
firewall-cmd —add-port=27032 —permanent
firewall-cmd —add-port=27032
关闭selinux
vi /etc/selinux/config
SELINUX=disabled
1.安装mongodb
下载解压即可
2.创建目录
#配置文件和pid文件和日志文件路径
mkdir -p /data/mongo/conf.d
mkdir -p /data/mongo/pid
mkdir -p /data/mongo/log/
#创建config server数据存储路径
mkdir -p /data/mongo/config/data
#创建shard server数据存储路径
mkdir -p /data/mongo/shard1/data
mkdir -p /data/mongo/shard2/data
mkdir -p /data/mongo/shard3/data
#赋予权限-使用root用户则不需要
chown -R mongod:mongod /data/mongo
3.config server配置服务器
vi /data/mongo/conf.d/config.conf
#系统日志选项
systemLog:
destination: file
logAppend: true
path: /data/mongo/log//configsvr.log
storage:
dbPath: /data/mongo/config/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/mongo/pid/configsvr.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 21020
bindIp: 0.0.0.0
maxIncomingConnections: 20000
replication:
replSetName: configServer
sharding:
clusterRole: configsvr
启动三台服务器的config server
mongod —config /data/mongo/conf.d/config.conf
登陆任意一台config server 初始化副本集
mongo 192.168.1.10:27020
config = {
_id : “configServer”,
members : [
{_id : 1, host : “192.168.1.10:27020” },
{_id : 2, host : “192.168.1.11:27020” },
{_id : 3, host : “192.168.1.12:27020” }
]
}
初始化副本集
rs.initate(config)
4.配置分片副本集
1.设置第一个分片副本集
添加配置文件
vi /data/mongo/conf.d/shard1.conf
systemLog:
destination: file
logAppend: true
path: /data/mongo/log/shard1.log
storage:
dbPath: /data/mongo/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/mongo/pid/shard1.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27030
bindIp: 0.0.0.0
maxIncomingConnections: 20000
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
启动3台服务器的shard1 server
mongod —config /data/mongo/conf.d/shard1.conf
登陆任意一台shard1初始化副本集
mongo 192.168.1.10:27030
use admin
定义副本集配置.第三个节点为仲裁节点
config = {
_id : “shard1”,
members : [
{_id : 1, host : “192.168.1.10:27030” , priority:2},
{_id : 2, host : “192.168.1.11:27030” , priority:1},
{_id : 3, host : “192.168.1.12:27030” , arbiterOnly: true }
]
}
初始化副本集
rs.initate(config)
设置第二个分片副本集—同第一个分片副本集
设置第三个分片副本集
5.配置路由服务器 mongos
先启动配置服务器和分配服务器,后启动路由实例,
vi /data/mongo/conf.d/mongos.conf
配置文件内容
systemLog:
destination: file
logAppend: true
path: /data/mongo/log/mongos.log
processManagement:
fork: true
pidFilePath: /data/mongo/pid/mongos.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27010
bindIp: 0.0.0.0
maxIncomingConnections: 20000
sharding:
configDB: configServer/192.168.1.10:27020, 192.168.1.11:27020, 192.168.1.12:27020
注意监听的配置服务器,至少一个 configServer为配置服务器副本集名字
启动三台服务器的mongos server
mongod —config /data/mongo/conf.d/mongos.conf
6.启用分片
登陆任意一台mongos
mongo 192.168.1.10:27010
使用admin数据库
use admin
串联路由服务器与分片副本集
sh.addShard(“shard1/192.168.1.10:27030,192.168.1.11:27030,192.168.1.12:27030”)
sh.addShard(“shard2/192.168.1.10:27031,192.168.1.11:27031,192.168.1.13:27031”)
sh.addShard(“shard3/192.168.1.10:27032,192.168.1.11:27032,192.168.1.12:27032”)
将副本集shard1的 ip1:prot /2:prot /3:prot添加到分片集群
查看集群状态
sh.status()
启用顺序
config server
shard1/2/3
mongos
hash分片
登陆mongos
mongo 192.168.1.10:27010
链接目标数据库,并对该库启用分片
use data1
sh.enableSharding(“data1”)
对data1库的user表的id列使用hash分片方式
sh.shardCollection(“data1.user”, { id : “hashed” } )
适用空的集合——有数据的使用下面方法
链接数据库data1,并在user集合上创建散列索引并对id列使用hash分片
use data1
db.user.createIndex({id:1})
sh.shardCollection(“data1.user”, { id : “hashed” } )
range分片
登陆mongos
mongo 192.168.1.10:27010
链接目标数据库,并对该库启用分片
use data1
sh.enableSharding(“data1”)
对data1库的user表的id列使用range分片方式
sh.shardCollection(“data1.user”, { id : “1” } )
适用空的集合——有数据的使用下面方法
链接数据库data1,并在user集合上创建索引并对id列使用range分片
use data1
db.user.createIndex({id:1})
sh.shardCollection(“data1.user”, { id : “1” } )
测试
登陆到mongos上
mongo 192.168.1.10:27010
链接到数据库data1
use data1
循环插入1000条数到user集合里
for(var i=0; i<1000;i++){db.user.insert({name:"cctv"+i})}
查看分片情况
sh.status()
或 db.prinshardingStatus()
登陆shard1/2/3上确认数据分布情况
use data1
db.user.find()