发布时间:2022-08-19 11:32
下载:mongodb-macos-x86_64-4.2.8.tgz
解压、改名:shard_cluster
tar -zxvf mongodb-macos-x86_64-4.2.8.tgz
mv mongodb-macos-x86_64-4.2.8 shard_cluster
mkdir -p config/{config1,config2,config3,logs}
mkdir -p shard/{shard1/{shard1-37011,shard1-37013,shard1-37015,shard1-37017,logs},shard2/{shard2-47011,shard2-47013,shard2-47015,shard2-47017,logs},shard3/{shard3-57011,shard3-57013,shard3-57015,shard3-57017,logs},shard4/{shard4-58011,shard4-58013,shard4-58015,shard4-58017,logs}}
mkdir -p route/logs
mkdir -p data/mongodb
# 数据库文件位置
dbpath=config/config1
#日志文件位置
logpath=config/logs/config1.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17011
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
# 数据库文件位置
dbpath=config/config2
#日志文件位置
logpath=config/logs/config2.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17013
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
# 数据库文件位置
dbpath=config/config3
#日志文件位置
logpath=config/logs/config3.log
# 以追加方式写入日志
logappend=true
# 是否以守护进程方式运行
fork=true
bind_ip=0.0.0.0
port=17015
# 表示是一个配置服务器
configsvr=true
#配置服务器副本集名称
replSet=configsvr
./bin/mongod -f config/config-17011.conf
./bin/mongod -f config/config-17013.conf
./bin/mongod -f config/config-17015.conf
./bin/mongo --port 17011
use admin
var cfg = {"_id":"configsvr",
"members":[
{"_id":1, "host":"localhost:17011"},
{"_id":2, "host":"localhost:17013"},
{"_id":3, "host":"localhost:17015"}
]
};
rs.initiate(cfg)
dbpath=shard/shard1/shard1-37011
bind_ip=0.0.0.0
port=37011
fork=true
logpath=shard/shard1/logs/shard1-37011.log
replSet=shard1
shardsvr=true
dbpath=shard/shard1/shard1-37013
bind_ip=0.0.0.0
port=37013
fork=true
logpath=shard/shard1/logs/shard1-37013.log
replSet=shard1
shardsvr=true
dbpath=shard/shard1/shard1-37015
bind_ip=0.0.0.0
port=37015
fork=true
logpath=shard/shard1/logs/shard1-37015.log
replSet=shard1
shardsvr=true
dbpath=shard/shard1/shard1-37017
bind_ip=0.0.0.0
port=37017
fork=true
logpath=shard/shard1/logs/shard1-37017.log
replSet=shard1
shardsvr=true
./bin/mongod -f shard/shard1/shard1-37011.conf
./bin/mongod -f shard/shard1/shard1-37013.conf
./bin/mongod -f shard/shard1/shard1-37015.conf
./bin/mongod -f shard/shard1/shard1-37017.conf
./bin/mongo --port 37011
var cfg = {"_id":"shard1",
"protocolVersion" : 1,
"members":[
{"_id":1, "host":"localhost:37011"},
{"_id":2, "host":"localhost:37013"},
{"_id":3, "host":"localhost:37015"},
{"_id":4, "host":"localhost:37017", "arbiterOnly":true}
]
};
rs.initiate(cfg)
rs.status()
dbpath=shard/shard2/shard2-47011
bind_ip=0.0.0.0
port=47011
fork=true
logpath=shard/shard2/logs/shard2-47011.log
replSet=shard2
shardsvr=true
dbpath=shard/shard2/shard2-47013
bind_ip=0.0.0.0
port=47013
fork=true
logpath=shard/shard2/logs/shard2-47013.log
replSet=shard2
shardsvr=true
dbpath=shard/shard2/shard2-47015
bind_ip=0.0.0.0
port=47015
fork=true
logpath=shard/shard2/logs/shard2-47015.log
replSet=shard2
shardsvr=true
dbpath=shard/shard2/shard2-47017
bind_ip=0.0.0.0
port=47017
fork=true
logpath=shard/shard2/logs/shard2-47017.log
replSet=shard2
shardsvr=true
./bin/mongod -f shard/shard2/shard2-47011.conf
./bin/mongod -f shard/shard2/shard2-47013.conf
./bin/mongod -f shard/shard2/shard2-47015.conf
./bin/mongod -f shard/shard2/shard2-47017.conf
./bin/mongo --port 47011
var cfg = {"_id":"shard2",
"protocolVersion" : 1,
"members":[
{"_id":1, "host":"localhost:47011"},
{"_id":2, "host":"localhost:47013"},
{"_id":3, "host":"localhost:47015"},
{"_id":4, "host":"localhost:47017", "arbiterOnly":true}
]
};
rs.initiate(cfg)
rs.status()
dbpath=shard/shard3/shard3-57011
bind_ip=0.0.0.0
port=57011
fork=true
logpath=shard/shard3/logs/shard3-57011.log
replSet=shard3
shardsvr=true
dbpath=shard/shard3/shard3-57013
bind_ip=0.0.0.0
port=57013
fork=true
logpath=shard/shard3/logs/shard3-57013.log
replSet=shard3
shardsvr=true
dbpath=shard/shard3/shard3-57015
bind_ip=0.0.0.0
port=57015
fork=true
logpath=shard/shard3/logs/shard3-57015.log
replSet=shard3
shardsvr=true
dbpath=shard/shard3/shard3-57017
bind_ip=0.0.0.0
port=57017
fork=true
logpath=shard/shard3/logs/shard3-57017.log
replSet=shard3
shardsvr=true
./bin/mongod -f shard/shard3/shard3-57011.conf
./bin/mongod -f shard/shard3/shard3-57013.conf
./bin/mongod -f shard/shard3/shard3-57015.conf
./bin/mongod -f shard/shard3/shard3-57017.conf
./bin/mongo --port 57011
var cfg = {"_id":"shard3",
"protocolVersion" : 1,
"members":[
{"_id":1, "host":"localhost:57011"},
{"_id":2, "host":"localhost:57013"},
{"_id":3, "host":"localhost:57015"},
{"_id":4, "host":"localhost:57017", "arbiterOnly":true}
]
};
rs.initiate(cfg)
rs.status()
dbpath=shard/shard4/shard4-58011
bind_ip=0.0.0.0
port=58011
fork=true
logpath=shard/shard4/logs/shard4-58011.log
replSet=shard4
shardsvr=true
dbpath=shard/shard4/shard4-58013
bind_ip=0.0.0.0
port=58013
fork=true
logpath=shard/shard4/logs/shard4-58013.log
replSet=shard4
shardsvr=true
dbpath=shard/shard4/shard4-58015
bind_ip=0.0.0.0
port=58015
fork=true
logpath=shard/shard4/logs/shard4-58015.log
replSet=shard4
shardsvr=true
dbpath=shard/shard4/shard4-58017
bind_ip=0.0.0.0
port=58017
fork=true
logpath=shard/shard4/logs/shard4-58017.log
replSet=shard4
shardsvr=true
./bin/mongod -f shard/shard4/shard4-58011.conf
./bin/mongod -f shard/shard4/shard4-58013.conf
./bin/mongod -f shard/shard4/shard4-58015.conf
./bin/mongod -f shard/shard4/shard4-58017.conf
./bin/mongo --port 58011
var cfg = {"_id":"shard4",
"protocolVersion" : 1,
"members":[
{"_id":1, "host":"localhost:58011"},
{"_id":2, "host":"localhost:58013"},
{"_id":3, "host":"localhost:58015"},
{"_id":4, "host":"localhost:58017", "arbiterOnly":true}
]
};
rs.initiate(cfg)
rs.status()
port=27017
bind_ip=0.0.0.0
fork=true
logpath=route/logs/route.log
configdb=configsvr/localhost:17011,localhost:17013,localhost:17015
./bin/mongos -f route/route-27017.conf
./bin/mongo --port 27017
sh.status()
sh.addShard("shard1/localhost:37011,localhost:37013,localhost:37015,localhost:37017");
sh.addShard("shard2/localhost:47011,localhost:47013,localhost:47015,localhost:47017");
sh.addShard("shard3/localhost:57011,localhost:57013,localhost:57015,localhost:57017");
sh.addShard("shard4/localhost:58011,localhost:58013,localhost:58015,localhost:58017");
sh.status()
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f1ef531f4a85c5270098b90")
}
shards:
{ "_id" : "shard1", "host" : "shard1/localhost:37011,localhost:37013,localhost:37015", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/localhost:47011,localhost:47013,localhost:47015", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/localhost:57011,localhost:57013,localhost:57015", "state" : 1 }
{ "_id" : "shard4", "host" : "shard4/localhost:58011,localhost:58013,localhost:58015", "state" : 1 }
active mongoses:
"4.2.8" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
sh.enableSharding("lx_resume")
sh.shardCollection("lx_resume.lx_resume_datas",{"name":"hashed"})
./bin/mongo --port 27017
use lx_resume
db.createUser(
{
user: "lx",
pwd: "abc321",
roles: [
{ role: "readWrite", db: "lx_resume" }
]
}
)
Successfully added user: {
"user" : "lx",
"roles" : [
{
"role" : "readWrite",
"db" : "lx_resume"
}
]
}
killall mongod
killall mongos
killall mongo
openssl rand -base64 756 > data/mongodb/testKeyFile.file
chmod 600 data/mongodb/testKeyFile.file
auth=true
keyFile=data/mongodb/testKeyFile.file
keyFile=data/mongodb/testKeyFile.file
编写shell启动config节点、shard节点、route节点
start.sh
./bin/mongod -f config/config-17011.conf
./bin/mongod -f config/config-17013.conf
./bin/mongod -f config/config-17015.conf
./bin/mongod -f shard/shard1/shard1-37011.conf
./bin/mongod -f shard/shard1/shard1-37013.conf
./bin/mongod -f shard/shard1/shard1-37015.conf
./bin/mongod -f shard/shard1/shard1-37017.conf
./bin/mongod -f shard/shard2/shard2-47011.conf
./bin/mongod -f shard/shard2/shard2-47013.conf
./bin/mongod -f shard/shard2/shard2-47015.conf
./bin/mongod -f shard/shard2/shard2-47017.conf
./bin/mongod -f shard/shard3/shard3-57011.conf
./bin/mongod -f shard/shard3/shard3-57013.conf
./bin/mongod -f shard/shard3/shard3-57015.conf
./bin/mongod -f shard/shard3/shard3-57017.conf
./bin/mongod -f shard/shard4/shard4-58011.conf
./bin/mongod -f shard/shard4/shard4-58013.conf
./bin/mongod -f shard/shard4/shard4-58015.conf
./bin/mongod -f shard/shard4/shard4-58017.conf
./bin/mongos -f route/route-27017.conf
./bin/mongo --port 27017
use lx_resume
db.auth("lx","abc321")
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
<version>2.2.2.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.2.2.RELEASEversion>
<scope>testscope>
dependency>
dependencies>
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=lx_resume
spring.data.mongodb.username=lx
spring.data.mongodb.password=abc321
package com.lx.pojo;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
@Document("lx_resume_datas")
public class Resume {
private String id;
private String name;
private String city;
private Date birthday;
private Double expectSalary;
public Resume() {
}
public Resume(String id, String name, String city, Date birthday, Double expectSalary) {
this.id = id;
this.name = name;
this.city = city;
this.birthday = birthday;
this.expectSalary = expectSalary;
}
@Override
public String toString() {
return "Resume{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", city='" + city + '\'' +
", birthday=" + birthday +
", expectSalary=" + expectSalary +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Double getExpectSalary() {
return expectSalary;
}
public void setExpectSalary(Double expectSalary) {
this.expectSalary = expectSalary;
}
}
package com.lx.dao;
import com.lx.pojo.Resume;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface IResumeDao extends MongoRepository<Resume, String> {
}
package com.lx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MongoRepositoryMain {
public static void main(String[] args) {
SpringApplication.run(MongoRepositoryMain.class, args);
}
}
package com.lx;
import com.lx.dao.IResumeDao;
import com.lx.pojo.Resume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MongoRepositoryMain.class)
public class TestRepository {
@Autowired
private IResumeDao resumeDao;
@Test
public void testAdd() throws ParseException {
Resume resume = new Resume();
resume.setName("test2");
resume.setCity("BJ");
resume.setExpectSalary(18000d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2002-08-11 11:13:14");
resume.setBirthday(date);
resumeDao.save(resume);
}
@Test
public void testQuery() {
List<Resume> list = resumeDao.findAll();
list.forEach(resume -> {
System.out.println(resume);
});
}
}