mongodb 知识图谱_MongoDB.md · 程林/分布式文档存储独角兽MongoDB_知识图谱存储数据库Neo4j - Gitee.com...

# MongoDB

## 1. 搭建分片节点集群

搭建三组分片节点,每个节点中(复制集replica sets)有三个数据节点和一个仲裁节点

shard1:

192.168.11.108:47017、192.168.11.108:47018、192.168.11.108:47018和192.168.11.108:47020(仲裁)

shard2:

192.168.11.108:57017、192.168.11.108:57018、192.168.11.108:57019和192.168.11.108:57020(仲裁)

shard3:

192.168.11.108:57011、192.168.11.108:57012、192.168.11.108:57013和192.168.11.108:57014(仲裁)

![1597548126485](https://gitee.com/clj2ee/typeor/raw/master/image/202008/16/112214-930684.png)

### 1.1 配置文件

shard1的配置文件:

```properties

#-------------- 192.168.11.108:47017 ---

#数据文件,使用相对路径

dbpath=shard/shard1/shard1_47017

bind_ip=0.0.0.0

port=47017

# 后台启动

fork=true

# 日志文件,使用相对路径

logpath=shard/shard1/logs/shard1-47017.log

# 复制集的名称

replSet=shard1

#开启复制集

shardsvr=true

#权限认证

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:47018 ---

dbpath=shard/shard1/shard1_47018

bind_ip=0.0.0.0

port=47018

fork=true

logpath=shard/shard1/logs/shard1-47018.log

replSet=shard1

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:47019 ---

dbpath=shard/shard1/shard1_47019

bind_ip=0.0.0.0

port=47019

fork=true

logpath=shard/shard1/logs/shard1-47019.log

replSet=shard1

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:47020 ---

dbpath=shard/shard1/shard1_47020

bind_ip=0.0.0.0

port=47020

fork=true

logpath=shard/shard1/logs/shard1-47020.log

replSet=shard1

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

```

shard2的配置文件:

```properties

#-------------- 192.168.11.108:57017 ---

dbpath=shard/shard2/shard2_57017

bind_ip=0.0.0.0

port=57017

fork=true

logpath=shard/shard2/logs/shard2-57017.log

replSet=shard2

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57018 ---

dbpath=shard/shard2/shard2_57018

bind_ip=0.0.0.0

port=57018

fork=true

logpath=shard/shard2/logs/shard2-57018.log

replSet=shard2

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57019 ---

dbpath=shard/shard2/shard2_57019

bind_ip=0.0.0.0

port=57019

fork=true

logpath=shard/shard2/logs/shard2-57019.log

replSet=shard2

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57020 ---

dbpath=shard/shard2/shard2_57020

bind_ip=0.0.0.0

port=57020

fork=true

logpath=shard/shard2/logs/shard2-57020.log

replSet=shard2

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

```

shard3的配置文件:

```properties

#-------------- 192.168.11.108:57011 ---

dbpath=shard/shard3/shard3_57011

bind_ip=0.0.0.0

port=57011

fork=true

logpath=shard/shard3/logs/shard3-57011.log

replSet=shard3

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57012 ---

dbpath=shard/shard3/shard3_57012

bind_ip=0.0.0.0

port=57012

fork=true

logpath=shard/shard3/logs/shard3-57012.log

replSet=shard3

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57013 ---

dbpath=shard/shard3/shard3_57013

bind_ip=0.0.0.0

port=57013

fork=true

logpath=shard/shard3/logs/shard3-57013.log

replSet=shard3

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#-------------- 192.168.11.108:57014 ---

dbpath=shard/shard3/shard3_57014

bind_ip=0.0.0.0

port=57014

fork=true

logpath=shard/shard3/logs/shard3-57014.log

replSet=shard3

# shard serve

shardsvr=true

#auth=true

#keyFile=data/mongodb/testKeyFile.file

```

### 1.3 初始化复制集

* shard1

* 启动复制集

```shell

./bin/mongod -f shard/shard1/shard1_47017.conf

./bin/mongod -f shard/shard1/shard1_47018.conf

./bin/mongod -f shard/shard1/shard1_47019.conf

./bin/mongod -f shard/shard1/shard1_47020.conf

```

* 初始化

192.168.11.108:47017、192.168.11.108:47018、192.168.11.108:47018和192.168.11.108:47020

进入上面任意节点的mongo shell

```properties

var cfg ={

"_id":"shard1",

"protocolVersion" : 1,

"members":[

{"_id":1,"host":"192.168.11.108:47017"},

{"_id":2,"host":"192.168.11.108:47018"},

{"_id":3,"host":"192.168.11.108:47019"},

{"_id":4,"host":"192.168.11.108:47020","arbiterOnly":true}

]}

```

rs.initiate(cfg)

* shard2

* 启动复制集

```shell

./bin/mongod -f shard/shard2/shard2_57017.conf

./bin/mongod -f shard/shard2/shard2_57018.conf

./bin/mongod -f shard/shard2/shard2_57019.conf

./bin/mongod -f shard/shard2/shard2_57020.conf

```

* 初始化

192.168.11.108:57017、192.168.11.108:57018、192.168.11.108:57019和192.168.11.108:57020

```properties

var cfg ={

"_id":"shard2",

"protocolVersion" : 1,

"members":[

{"_id":1,"host":"192.168.11.108:57017"},

{"_id":2,"host":"192.168.11.108:57018"},

{"_id":3,"host":"192.168.11.108:57019"},

{"_id":4,"host":"192.168.11.108:57020","arbiterOnly":true}

]}

```

rs.initiate(cfg)

* shard3

* 启动复制集

```shell

./bin/mongod -f shard/shard3/shard3_57011.conf

./bin/mongod -f shard/shard3/shard3_57012.conf

./bin/mongod -f shard/shard3/shard3_57013.conf

./bin/mongod -f shard/shard3/shard3_57014.conf

```

* 初始化

192.168.11.108:57011、192.168.11.108:57012、192.168.11.108:57013和192.168.11.108:57014

```properties

var cfg ={

"_id":"shard3",

"protocolVersion" : 1,

"members":[

{"_id":1,"host":"192.168.11.108:57011"},

{"_id":2,"host":"192.168.11.108:57012"},

{"_id":3,"host":"192.168.11.108:57013"},

{"_id":4,"host":"192.168.11.108:57014","arbiterOnly":true}

]}

```

rs.initiate(cfg)

## 2 搭建配置节点

搭建一组分片节点,三个节点

config:

​192.168.11.108:17017、192.168.11.108:17018、192.168.11.108:17019

### 2.1 配置文件

```properties

#----------------- 192.168.11.108:17017 ---

# 数据库文件位置

dbpath=config/config1

#日志文件位置

logpath=config/logs/config1.log

# 以追加方式写入日志

logappend=true

# 是否以守护进程方式运行

fork = true

bind_ip=0.0.0.0

port = 17017

# 表示是一个配置服务器

configsvr=true

#配置服务器副本集名称

replSet=configsvr

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#----------------- 192.168.11.108:17018 ---

# 数据库文件位置

dbpath=config/config2

#日志文件位置

logpath=config/logs/config2.log

# 以追加方式写入日志

logappend=true

# 是否以守护进程方式运行

fork = true

bind_ip=0.0.0.0

port = 17018

# 表示是一个配置服务器

configsvr=true

#配置服务器副本集名称

replSet=configsvr

#auth=true

#keyFile=data/mongodb/testKeyFile.file

#----------------- 192.168.11.108:17019 ---

# 数据库文件位置

dbpath=config/config3

#日志文件位置

logpath=config/logs/config3.log

# 以追加方式写入日志

logappend=true

# 是否以守护进程方式运行

fork = true

bind_ip=0.0.0.0

port = 17019

# 表示是一个配置服务器

configsvr=true

#配置服务器副本集名称

replSet=configsvr

#auth=true

#keyFile=data/mongodb/testKeyFile.file

```

### 2.2 搭建配置节点集群

* 启动配置节点

```shell

./bin/mongod -f config/config_17017.conf

./bin/mongod -f config/config_17018.conf

./bin/mongod -f config/config_17019.conf

```

* 配置节点

```properties

var cfg ={

"_id":"configsvr",

"members":[

{"_id":1,"host":"192.168.11.108:17017"},

{"_id":2,"host":"192.168.11.108:17018"},

{"_id":3,"host":"192.168.11.108:17019"}

]};

```

rs.initiate(cfg)

## 3 搭建路由节点

搭建一个路由节点,不需要存储数据

route:

​192.168.11.108:27017

### 3.1 配置文件

```properties

port=27017

bind_ip=0.0.0.0

fork=true

logpath=route/logs/route.log

# 配置 配置节点

#configdb=/x.x.x.x:yyyy,z.z.z.z:s.s.s.s

configdb=configsvr/192.168.11.108:17017,192.168.11.108:17018,192.168.11.108:17019

#keyFile=data/mongodb/testKeyFile.file

```

### 3.2 启动和初始化路由节点

* 启动路由节点

```properties

# 启动路由节点使用 mongos (注意不是mongod)

./bin/mongos -f route/route_27017.conf

```

* 添加分片

```properties

#

sh.addShard("shard1/192.168.11.108:47017,192.168.11.108:47018,192.168.11.108:47019,192.168.11.108:47020");

#

sh.addShard("shard2/192.168.11.108:57017,192.168.11.108:57018,192.168.11.108:57019,192.168.11.108:57020");

#

sh.addShard("shard3/192.168.11.108:57011,192.168.11.108:57012,192.168.11.108:57013,192.168.11.108:57014");

```

* 查看分片信息

```properties

sh.status()

```

![1597560274357](https://gitee.com/clj2ee/typeor/raw/master/image/202008/16/144435-67368.png)

## 4 开启分片集群安全认证

### 4.1 开启安全认证之前

**注意:**

MongoDB 服务端开启安全检查之前,至少需要有一个管理员账号,admin 数据库中的用户都被视为管理员

* 进入路由节点创建管理员

```shell

mongos> use admin

switched to db admin

mongos> db

admin

mongos> db.createUser({

user:"root",

pwd:"123456",

roles:[

{role:"root",db:"admin"}

]

})

Successfully added user: {

"user" : "root",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

mongos>

```

* 创建lg_resume数据库,并给该库添加有读写权限的账号

```shell

mongos> use lg_resume;

switched to db lg_resume

mongos> db

lg_resume

mongos> db.createUser({

user:"lagou_gx",

pwd:"abc321",

roles:[{role:"readWrite",db:"lg_resume"}]

})

Successfully added user: {

"user" : "lagou_gx",

"roles" : [

{

"role" : "readWrite",

"db" : "lg_resume"

}

]

}

mongos>

```

* 关闭所有的配置节点、分片节点 和路由节点

![1597561508057](C:\Users\程林\AppData\Roaming\Typora\typora-user-images\1597561508057.png)

```properties

#安装psmisc

yum install psmisc

#安装完之后可以使用killall 命令 快速关闭多个进程

killall mongod

```

killall mongod & killall mongos

* 生成密钥文件

```properties

#生成密码文件

openssl rand -base64 756 > data/mongodb/testKeyFile.file

#赋予权限

chmod 600 data/mongodb/testKeyFile.file

```

* 配置节点集群和分片节点集群开启安全认证和指定密钥文件

```properties

auth=true

keyFile=data/mongodb/testKeyFile.file

```

将上面的注释去掉

* 在路由配置文件中 设置密钥文件

```properties

keyFile=data/mongodb/testKeyFile.file

```

将配置文件的注释去掉

### 4.2 启动所有节点

**注意:**启动所有的配置节点 、分片节点 和 路由节点

```properties

#配置节点

./bin/mongod -f config/config_17017.conf

./bin/mongod -f config/config_17018.conf

./bin/mongod -f config/config_17019.conf

#分片集群节点

# shard1

./bin/mongod -f shard/shard1/shard1_47017.conf

./bin/mongod -f shard/shard1/shard1_47018.conf

./bin/mongod -f shard/shard1/shard1_47019.conf

./bin/mongod -f shard/shard1/shard1_47020.conf

# shard2

./bin/mongod -f shard/shard2/shard2_57017.conf

./bin/mongod -f shard/shard2/shard2_57018.conf

./bin/mongod -f shard/shard2/shard2_57019.conf

./bin/mongod -f shard/shard2/shard2_57020.conf

# shard3

./bin/mongod -f shard/shard3/shard3_57011.conf

./bin/mongod -f shard/shard3/shard3_57012.conf

./bin/mongod -f shard/shard3/shard3_57013.conf

./bin/mongod -f shard/shard3/shard3_57014.conf

#路由节点

./bin/mongos -f route/route_27017.conf

```

### 4.3 开启分片

* 为数据库开启分片功能

```properties

sh.enableSharding("lg_resume")

```

* 为指定集合开启分片功能

sh.shardCollection("lagou_resume.lagou_resume_datas",{"片键字段名如 name":索引 明})

```properties

sh.shardCollection("lg_resume.lg_resume_datas",{"name":"hashed"})

```

使用路由进行权限验证

## 5 springboot + MongoDB

* yml

```yaml

spring:

data:

mongodb:

#spring.data.mongodb.uri=mongodb://账号:密码@IP:端口/数据库名

uri: mongodb://lagou_gx:abc321@192.168.11.108:27017/lg_resume

```

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值