1. 环境说明
主机名称 IP地址 OS版本 Role
node1 172.16.100.51 CentOS7(kernel: 3.10.0-514.el7.x86_64) mongos service
node2 172.16.100.52 CentOS7(kernel: 3.10.0-514.el7.x86_64) sharding and configer service
node3 172.16.100.53 CentOS7(kernel: 3.10.0-514.el7.x86_64) sharding and configer service
node4 172.16.100.54 CentOS7(kernel: 3.10.0-514.el7.x86_64) sharding and configer service
2. 在node2,node3,node4主机节点上安装且配置mongodb,并且启动mongodb服务
# vi /etc/mongod.conf
/etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
port: 27018
bindIp: 0.0.0.0
replication:
oplogSizeMB: 1024
replSetName: shard2|shard3|shard4 #在每个node上的名称不一样
sharding:
clusterRole: shardsvr
# systemctl start mongod.service
3. 在node2,node3,node4主机节点上,初始化replication set
node2
# mongo --port 27018
> rs.initiate({_id: "shard2", members: [{_id: 1, host: "172.16.100.52:27018"}]})
node3
# mongo --port 27018
> rs.initiate({_id: "shard3", members: [{_id: 1, host: "172.16.100.53:27018"}]})
node4
# mongo --port 27018
> rs.initiate({_id: "shard4", members: [{_id: 1, host: "172.16.100.54:27018"}]})
4. 在node2,node3,node4主机节点上安装且配置mongodb,并且启动mongodb的configer service.
# mkdir -p /mongodb/{etc,lib,log,run}
etc: 配置文件
lib:数据文件
log: 日志文件
run:pid文件
# touch /mongodb/log/mongod.log
# vi /mongodb/etc/mongod.conf
systemLog:
destination: file
logAppend: true
path: /mongodb/log/mongod.log
storage:
dbPath: /mongodb/lib
journal:
enabled: true
directoryPerDB: true
processManagement:
fork: true
pidFilePath: /mongodb/run/mongod.pid
net:
port: 27019
bindIp: 0.0.0.0
replication:
oplogSizeMB: 1024
replSetName: cfg
sharding:
clusterRole: configsvr
# chown -R mongod:mongod /mongodb
# vi /usr/lib/systemd/system/mongo.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /mongodb/etc/mongod.conf"
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /mongodb/run
ExecStartPre=/usr/bin/chown mongod:mongod /mongodb/run
ExecStartPre=/usr/bin/chmod 0755 /mongodb/run
PermissionsStartOnly=true
PIDFile=/mongodb/run/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
# systemctl enable mongo.service
# systemctl start mongo.service
5. 在node2上初始化replication set
# mongo --port 27019
> rs.initiate({_id: "cfg", members: [{_id: 1, host: "172.16.100.52:27019"}]})
> rs.add("172.16.100.53:27019")
> rs.add("172.16.100.54:27019")
6. 在node1主机节点上安装且配置mongos,并且启动mongodb的mongos service.
# yum install mongodb-org mongodb-org-shell -y
# mkdir -p /mongos/{etc,log,run}
# touch /mongos/log/mongos.log
# vi /mongos/etc/mongs.conf
systemLog:
destination: file
logAppend: true
path: /mongos/log/mongos.log
processManagement:
fork: true
pidFilePath: /mongos/run/mongos.pid
net:
port: 27017
bindIp: 0.0.0.0
sharding:
configDB: cfg/172.16.100.52:27019,172.16.100.53:27019,172.16.100.54:27019
# mongos -f /mongos/etc/mongs.conf
7. 在node1主机节点上操作数据库
# mongo //分片操作
> sh.addShard("shard2/172.16.100.52:27018")
> sh.addShard("shard3/172.16.100.53:27018")
> sh.addShard("shard4/172.16.100.54:27018")
> use ishop
> sh.enableSharding("ishop") //数据库开启分片
> sh.shardCollection("ishop.good", {goodId: 1}) //集合开启分片,指定分片主键和分片类型(range)
> for (var i = 0; i < 10000; i++) { db.good.insert( {goodId: i, goodName: "goodName_"+i} ); }
> sh.shardCollection("ishop.order", {orderId: "hashed"}) // //集合开启分片,指定分片主键和分片类型(hash)
> for (var i = 0; i < 10000; i++) { db.order.insert( {orderId: i, orderPrice: i%100} ); }