环境准备 mogo-version:
mongodb-org-mongos-4.2.3-1.el7.x86_64.rpm
mongodb-org-server-4.2.3-1.el7.x86_64.rpm
mongodb-org-shell-4.2.3-1.el7.x86_64.rpm
mongodb-org-tools-4.2.3-1.el7.x86_64.rpm
IP:192.168.0.201 | IP:192.168.0.202 | IP:192.168.0.203 |
---|---|---|
mongos() | mongos() | mongos() |
config(27019) | config(27019) | config(27019) |
shard1主节点(27017) | shard1副节点(27017) | shard1副节点(27017) |
shard2主节点(27016) | shard2副节点(27016) | shard1副节点(27016) |
shard3主节点(27015) | shard3副节点(27015) | shard1副节点(27015) |
在每个服务器上执行
mkdir -p /data/mongodb/logs/ log的存放目录
mkdir -p /data/mongodb/etc 配置文件的存放目录
touch /data/mongodb/etc{mongo-config.conf,mongod-shard1.conf,mongod-shard2.conf,mongod-shard3.conf}
mkdir /data/mongodb/shard{1,2,3}
touch /data/mongodb/logs/shard{1,2,3}.log
touch /data/mongodb/logs/mongos.log
touch /data/mongodb/logs/config.log
每台服务器: mongo-config.conf
mongod --configsvr -f /data/mongodb/etc/mongo-config.conf
启动配置服务加上 --configsvr
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/config.log
storage:
dbPath: /data/mongodb/config
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongoconfig1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27019
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
replication:
replSetName: myconf
每台服务器: mongod-shard1.conf
mongod-shard2.conf, mongod-shard3.conf 两个配置文件和这个文件一样只是将shard1,替换为shardn即可
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/shard1.log
storage:
dbPath: /data/mongodb/shard1
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongoshard1.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
启动分片并配置分片复制集
mongod -f /data/mongodb/etc/mongod-shard1.conf
#配置分片shard1 复制集
config = { _id:"shard1", members:[
{_id:0,host:"node1:27017"},
{_id:1,host:"node2:27017"},
{_id:2,host:"node3:27017"}]
}
rs.initiate(config);
shard2,shard3 启动和配置和shard1类似修改相关的复制集名称和端口信息
=====mongos=========
mongos.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongos.log
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongos.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27018
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
sharding:
configDB: myconf/node1:27019,node2:27019,node3:27019
mongos -f mongos.conf 启动
启动成功后进入
mongo --port 27018
use admin
sh.addShard("shard1/node1:27017,node2:27017,node3:27017")
sh.addShard("shard2/node1:27016,node2:27016,node3:27016")
sh.addShard("shard3/node1:27015,node2:27015,node3:27015")
#查看分片状态
sh.status()