环境准备
操作系统:centos6.5
mongodb版本:v3.6.23
**三台阿里云服务器:**10.168.2.138、10.168.4.204、10.168.4.205
搭建目标
两个分片复制集(Sharded Replica Set)
分片集群1:(10.168.2.138:27011、10.168.4.204:27011、10.168.4.205:27011)
分片集群2: (10.168.2.138:27012、10.168.4.204:27012、10.168.4.205:27012)
分片配置集(Sharding Config Servers)
config配置集:(10.168.2.138:27010、10.168.4.204:27010、10.168.4.205:27010)
mongos路由集(MongoDB Shard Router)
10.168.2.138:27019
1、在三台服务器上分别安装mongodb
第一步:添加mongodb yum仓库 : /etc/yum.repos.d/mongodb-org.repo
bash
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
第二步:执行安装指令
bash
sudo yum install -y mongodb-org
2、搭建分片复制集
在三台服务器上分别搭建、启动分片节点,dbpath和logpath目录可以不一致
第一步:创建分片1配置文件 /etc/mongod-shard1.conf
bash
#添加复制集配置文件
fork=true
dbpath=/data/mongodb/data-shard1
port=27011
bind_ip=0.0.0.0
logpath=/data/mongodb/log/mongod.log
logappend=true
#副本集名称
replSet=obos_rep1
smallfiles=true
#分片集群必须要有的属性
shardsvr=true
第二步:创建分片2配置文件 /etc/mongod-shard2.conf
bash
#添加复制集配置文件
fork=true
dbpath=/data/mongodb/data-shard2
port=27012
bind_ip=0.0.0.0
logpath=/data/mongodb/log/mongod.log
logappend=true
#副本集名称
replSet=obos_rep2
smallfiles=true
#分片集群必须要有的属性
shardsvr=true
第三步:启动复制集
/usr/bin/mongod -f /etc/mongod-shard1.conf
/usr/bin/mongod -f /etc/mongod-shard2.conf
第四步:在10.168.2.138服务器上,执行分片集初始化
bash
#进入mongo客户端
/usr/bin/mongo --port 27011
#执行初始化命令
rs.initiate({
_id:'obos_rep1',
members:
[
{
_id:1,
host: '10.168.2.138:27011'
},
{
_id:2,
host: '10.168.4.204:27011'
},
{
_id:3,
host: '10.168.4.205:27011'
}
]
})-
bash
#进入mongo客户端
/usr/bin/mongo --port 27012
#执行初始化命令
rs.initiate({
_id:'obos_rep2',
members:
[
{
_id:1,
host: '10.168.2.138:27012'
},
{
_id:2,
host: '10.168.4.204:27012'
},
{
_id:3,
host: '10.168.4.205:27012'
}
]
})
3、搭建mongodb分片配置集
第一步:在三台服务器上分别创建分片配置文件: /etc/mongod-cfg.conf
对应修改bindIp
bash
# where to write logging.
systemLog:
destination: file
logAppend: true
path: /data/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /data/mongodb/data/
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 5
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27010
bindIp: 10.168.2.138
replication:
oplogSizeMB: 2048
# 配置节点的复制集名称
replSetName: configReplSet
sharding:
clusterRole: configsvr
第二步:启动
bash
/usr/bin/mongod -f /etc/mongod-cfg.conf
第三步:初始化配置集
bash
#进入10.168.2.138 mongo客户端
/usr/bin/mongo -host 10.168.2.138 --port 27010
#初始化命令:
rs.initiate({
_id:'configReplSet',
members:
[
{
_id:1,
host: '10.168.2.138:27010'
},
{
_id:2,
host: '10.168.4.204:27010'
},
{
_id:3,
host: '10.168.4.205:27010'
}
]
})
4、搭建mongos路由
创建配置文件 /etc/mongos.conf
bash
systemLog:
destination: file
path: /mnt/mongodb/log/mongos.log
logAppend: true
net:
bindIp: 10.168.2.138
port: 27019
sharding:
configDB: configReplSet/10.168.2.138:27010,10.168.4.204:27010,10.168.4.205:27010
processManagement:
fork: true
启动:
bash
/usr/bin/mongos -config /etc/mongos.conf
5、通过路由添加分片配置
第一步:添加分片
bash
#连接路由27019,添加分片
/usr/bin/mongo --host 10.168.2.138 --port 27019
sh.addShard("obos_repl/10.168.2.138:27011,10.168.4.204:27011,10.168.4.205:27011")
sh.addShard("obos_rep2/10.168.2.138:27012,10.168.4.204:27012,10.168.4.205:27012")
添加完成后,查看状态:
bash
>> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("66bb1f38179a5e2e3af20c26")
}
shards:
{ "_id" : "obos_rep2", "host" : "obos_rep2/10.168.2.138:27012,10.168.4.204:27012,10.168.4.205:27012", "state" : 1 }
{ "_id" : "obos_repl", "host" : "obos_repl/10.168.2.138:27011,10.168.4.204:27011,10.168.4.205:27011", "state" : 1 }
active mongoses:
"3.6.23" : 1
----------------------
第二步:创建数据库和集合
bash
use obos_db;
db.createCollection("terminal_param");
针对数据库和集合设置分片 需要切换到admin数据库,然后针对obos_db数据库开启分片,设置terminal_param集合的分片键terminalId
bash
use admin
db.runCommand({ enablesharding: "obos_db" })
db.runCommand({ shardcollection: "obos_db.terminal_param", key: {terminalId: 1}})
创建唯一键
bash
use obos_db
db.terminal_param.createIndex({terminalId:1,paramId:1},{unique:true})
6、创建用户
bash
use obos_db;
db.createUser({user: "admin", pwd: "xxxxxxxx", roles: [ { role: "userAdminAnyDatabase", db: "admin" },{ role: "dbAdminAnyDatabase", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" }]});
7、java 通过账密连接mongodb集群
第一步:添加依赖
XML
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.5</version>
</dependency>
第二步:代码示例
java
@Service
@Configuration
public class MongoDbService {
@Value("${mongodb.host}")
private String host;
@Value("${mongodb.port}")
private Integer port;
@Value("${mongodb.database}")
private String databaseName;
@Value("${mongodb.username}")
private String username;
@Value("${mongodb.password}")
private String password;
private MongoDatabase mongoDatabase;
@PostConstruct
private void createMongoConnect(){
String uri = "mongodb://"+username+":"+password+"@"+host+":"+port+"/"+databaseName;
WriteConcern writeConcern = WriteConcern.UNACKNOWLEDGED;
MongoClientSettings settings=MongoClientSettings.builder()
.applyToClusterSettings(builder -> builder.serverSelectionTimeout(30, TimeUnit.SECONDS))
.applyConnectionString(new ConnectionString(uri))
.writeConcern(writeConcern)
.build();
MongoClient mongoClient = MongoClients.create(settings);
mongoDatabase= mongoClient.getDatabase(databaseName);
}
public MongoDatabase getMongoDatabase(){
return mongoDatabase;
}
public void test(){
MongoCollection<Document> collection = getMongoDatabase().getCollection("terminal_param");
Document dt=new Document();
dt.append("terminalId","1111111111").append("paramId","0X00000001").append("paramValue","12").append("dataTime",1723618966);
collection.insertOne(dt);
}
}