????????在這此前建議先了解一下mongo的片鍵概念下梢,結(jié)合自己的業(yè)務(wù)關(guān)系確定一個(gè)合適的片鍵巩搏,才能更好的發(fā)揮集群的作用册舞。
進(jìn)入正題。
Linux centOs 7.4
mongo
首先拷貝mongo安裝包3.0版本到usr/local目錄下并解壓
在101蹬挤,102缚窿,103 三臺(tái)服務(wù)器上分別
創(chuàng)建文件
chown fanml:fanml /usr/local/mongodb
mkdir /data
chown fanml:fanml? /data/
mkdir -p /usr/local/mongodb/conf
mkdir -p /data/mongodb/mongos/log
mkdir -p /data/mongodb/config/data
mkdir -p /data/mongodb/config/log
mkdir -p /data/mongodb/shard1/data
mkdir -p /data/mongodb/shard1/log
mkdir -p /data/mongodb/shard2/data
mkdir -p /data/mongodb/shard2/log
mkdir -p /data/mongodb/shard3/data
mkdir -p /data/mongodb/shard3/log
并確定集群對(duì)外的端口,防火墻要開(kāi)放這些端口
mongos:27000
config:21000
shard1:27001
shard2:27002
shard3:27003
沒(méi)有numactl命令焰扳,使用yum安裝
yum install -y numactl
1倦零、config server配置服務(wù)器
vi /usr/local/mongodb/conf/config.conf
## content
systemLog:
? destination: file
? logAppend: true
? path: /data/config/log/config.log
# Where and how to store data.
storage:
? dbPath: /data/config/data
? journal:
? ? enabled: true
# how the process runs
processManagement:
? fork: true
? pidFilePath: /data/config/log/configsrv.pid
# network interfaces
net:
? port: 21000
? bindIp: 192.168.168.104
#operationProfiling:
replication:
? ? replSetName: config? ? ? ?
sharding:
? ? clusterRole: configsvr
啟動(dòng)配置服務(wù)
numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf
分別在三臺(tái)機(jī)子上配置
登錄任意一臺(tái)初始化副本集
#連接
mongo --port 21000
#config變量
config = {
...? ? _id : "config",
...? ? members : [
...? ? ? ? {_id : 0, host : "192.168.168.102:21000" },
...? ? ? ? {_id : 1, host : "192.168.168.103:21000" },
...? ? ? ? {_id : 2, host : "192.168.168.104:21000" }
...? ? ]
... }
#初始化副本集
rs.initiate(config)
#查看分區(qū)狀態(tài)
rs.status();
其中误续,"_id" : "configs"應(yīng)與配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個(gè)節(jié)點(diǎn)的ip和port
這樣配置服務(wù)器就配置好了
2扫茅、配置分片
vi /usr/local/mongodb/conf/shard1.conf
#配置文件內(nèi)容
# where to write logging data.
systemLog:
? destination: file
? logAppend: true
? path: /data/shard1/log/shard1.log
# Where and how to store data.
storage:
? dbPath: /data/shard1/data
? journal:
? ? enabled: true
# how the process runs
processManagement:
? fork: true
? pidFilePath: /data/shard1/log/shard1.pid
# network interfaces IP對(duì)應(yīng)各自的IP地址
net:
? port: 27001
? bindIp: 192.168.168.102?
#operationProfiling:
replication:
? ? replSetName: shard1
sharding:
? ? clusterRole: shardsvr
啟動(dòng)
numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard1.conf
numactl --interleave=all mongod? --config? /usr/local/mongodb/conf/shard2.conf
numactl --interleave=all mongod? --config? /usr/local/mongodb/conf/shard3.conf
繼續(xù)在三臺(tái)服務(wù)器上配置分片2蹋嵌,3并啟動(dòng)
登錄任意一臺(tái)初始化分片副本集
初始化分片1副本集
mongo 192.168.1.202:27001
#使用admin數(shù)據(jù)庫(kù)
use admin
#定義副本集配置
config = {
...? ? _id : "shard1",
...? ? members : [
...? ? ? ? {_id : 0, host : "192.168.168.102:27001" },
...? ? ? ? {_id : 1, host : "192.168.168.103:27001" },
...? ? ? ? {_id : 2, host : "192.168.168.104:27001" }
...? ? ]
... }
config = {
...? ? _id : "shard2",
...? ? members : [
...? ? ? ? {_id : 0, host : "192.168.168.102:27002" },
...? ? ? ? {_id : 1, host : "192.168.168.103:27002" },
...? ? ? ? {_id : 2, host : "192.168.168.104:27002" }
...? ? ]
... }
config = {
...? ? _id : "shard3",
...? ? members : [
...? ? ? ? {_id : 0, host : "192.168.168.102:27003" },
...? ? ? ? {_id : 1, host : "192.168.168.103:27003" },
...? ? ? ? {_id : 2, host : "192.168.168.104:27003" }
...? ? ]
... }
#初始化副本集配置
rs.initiate(config);
#查看分區(qū)狀態(tài)
rs.status();
4.配置 mongos
systemLog:
? destination: file
? logAppend: true
? path: /data/mongos/log/mongos.log
processManagement:
? fork: true
#? pidFilePath: /usr/local/mongodb/mongos.pid
# network interfaces
net:
? port: 27000
? bindIp: 192.168.168.104
#監(jiān)聽(tīng)的配置服務(wù)器,只能有1個(gè)或者3個(gè) configs為配置服務(wù)器的副本集名字
sharding:
? configDB: config/192.168.168.102:21000,192.168.168.103:21000,192.168.168.104:21000
啟用mongos
mongos? --config? /usr/local/mongodb/conf/mongos.conf
mongos可在多臺(tái)服務(wù)器上配置,此端口為對(duì)外的端口葫隙,供客戶端調(diào)用
此為Java調(diào)用示例
mongo.url=192.168.168.7:27018,192.168.168.7:27019,192.168.168.7:27020
mongoUrlitem = loadAllProperties.get("mongo.url").toString().split(",");
public static MongoClient openMongoConnection()?
{
if (mongoClient == null) {
try {
List addresses = new ArrayList<>();
for (int i = 0; i < mongoUrlitem.length; i++) {
ServerAddress seed = new ServerAddress(mongoUrlitem[i]);
addresses.add(seed);
}
mongoClient = new MongoClient(addresses);
} catch (Exception e) {
// TODO: handle exception
log.error("Open MongoClient Exception : ", e);
}
}
return mongoClient;
}
5栽烂、啟用分片
mongo --port 27000
#使用admin數(shù)據(jù)庫(kù)
use? admin
#串聯(lián)路由服務(wù)器與分配副本集
sh.addShard("shard1/192.168.168.102:27001,192.168.168.102:27001,192.168.168.102:27001")
sh.addShard("shard2/192.168.168.103:27002,192.168.168.103:27002,192.168.168.103:27002")
sh.addShard("shard3/192.168.168.104:27003,192.168.168.104:27003,192.168.168.104:27003")
#查看集群狀態(tài)
sh.status()
最后,分片集群具備靈活性可以根據(jù)業(yè)務(wù)關(guān)系決定是否啟用分片
連接mongos
可以指定數(shù)據(jù)庫(kù)LicenseFile啟用分片機(jī)制
db.runCommand( { enablesharding :"LicenseFile"});
#指定數(shù)據(jù)庫(kù)里需要分片的集合和片鍵
db.runCommand( { shardcollection : "LicenseFile.licenseFile",key : {id: 1} } )
測(cè)試
use LicenseFile
測(cè)試
for (var i = 1; i <= 100000; i++)db.licenseFile.save({id:i,"test1":"testval1"});