在这此前建议先了解一下mongo的片键概念,结合自己的业务关系确定一个合适的片键,才能更好的发挥集群的作用。
进入正题。
Linux centOs 7.4
mongo
首先拷贝mongo安装包3.0版本到usr/local目录下并解压
在101,102,103 三台服务器上分别
创建文件
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
并确定集群对外的端口,防火墙要开放这些端口
mongos:27000
config:21000
shard1:27001
shard2:27002
shard3:27003
没有numactl命令,使用yum安装
yum install -y numactl
1、config server配置服务器
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
启动配置服务
numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf
分别在三台机子上配置
登录任意一台初始化副本集
#连接
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)
#查看分区状态
rs.status();
其中,"_id" : "configs"应与配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 为三个节点的ip和port
这样配置服务器就配置好了
2、配置分片
vi /usr/local/mongodb/conf/shard1.conf
#配置文件内容
# 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对应各自的IP地址
net:
port: 27001
bindIp: 192.168.168.102
#operationProfiling:
replication:
replSetName: shard1
sharding:
clusterRole: shardsvr
启动
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
继续在三台服务器上配置分片2,3并启动
登录任意一台初始化分片副本集
初始化分片1副本集
mongo 192.168.1.202:27001
#使用admin数据库
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);
#查看分区状态
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
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
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可在多台服务器上配置,此端口为对外的端口,供客户端调用
此为Java调用示例
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数据库
use admin
#串联路由服务器与分配副本集
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")
#查看集群状态
sh.status()
最后,分片集群具备灵活性可以根据业务关系决定是否启用分片
连接mongos
可以指定数据库LicenseFile启用分片机制
db.runCommand( { enablesharding :"LicenseFile"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "LicenseFile.licenseFile",key : {id: 1} } )
测试
use LicenseFile
测试
for (var i = 1; i <= 100000; i++)db.licenseFile.save({id:i,"test1":"testval1"});