MongoDB是一个基于分布式文件存储的数据库
。由C++语言编写。旨在为WEB应用提供可扩展高性能数据存储解决方案
。
MongoDB是一个介于关系数据库和非关系数据库
之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型 。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引
。
总结: mongoDB 是一个非关系型文档数据库
副本集
说明
https://docs.mongodb.com/manual/replication/
MongoDB 副本集(Replica Set)是有自动故障恢复功能的主从集群,有一个Primary节点和一个或多个Secondary节点组成。副本集没有固定的主节点
,当主节点
发生故障时整个集群会选举一个主节点
为系统提供服务以保证系统的高可用。
Automatic Failover
自动故障转移机制: 当主节点未与集合的其他成员通信超过配置的选举超时时间(默认为 10 秒)时,合格的辅助节点将调用选举以将自己提名为新的主节点。集群尝试完成新主节点的选举并恢复正常操作。
搭建副本集
演示环境使用Debian 10.13
和MongoDB v5.0.14
创建数据目录
1 2 3 4
| # 在安装目录中创建 - mkdir -p ../repl/data1 - mkdir -p ../repl/data2 - mkdir -p ../repl/data3
|
搭建副本集
1 2 3 4 5
| ./mongod --port 27017 --dbpath ../repl/data1 --bind_ip 0.0.0.0 --replSet myreplace/124.239.149.46:27018,124.239.149.46:27019
./mongod --port 27018 --dbpath ../repl/data2 --bind_ip 0.0.0.0 --replSet myreplace/124.239.149.46:27019,124.239.149.46:27017
./mongod --port 27019 --dbpath ../repl/data3 --bind_ip 0.0.0.0 --replSet myreplace/124.239.149.46:27017,124.239.149.46:27018
|
配置副本集
连接任意节点,切换到admin
库use admin
初始化副本集
1 2 3 4 5 6 7 8 9 10
| var config = { _id:"myreplace", members:[ {_id:0,host:"124.239.149.46:27017"}, {_id:1,host:"124.239.149.46:27018"}, {_id:2,host:"124.239.149.46:27019"} ] }
rs.initiate(config);
|
设置从节点客户端临时访问
分片集群
说明
https://docs.mongodb.com/manual/sharding/
分片(sharding)
是指将数据拆分,将其分散存在不同机器的过程
,有时也用分区(partitioning)
来表示这个概念,将数据分散在不同的机器上,不需要功能强大的大型计算机就能存储更多的数据,处理更大的负载。
分片目的是通过分片能够增加更多机器来应对不断的增加负载和数据,还不影响应用运行。
MongoDB支持自动分片
,可以摆脱手动分片的管理困扰,集群自动切分数据做负载均衡。MongoDB分片的基本思想就是将集合拆分成多个块,这些块分散在若干个片里,每个片只负责总数据的一部分,应用程序不必知道哪些片对应哪些数据,甚至不需要知道数据拆分了,所以在分片之前会运行一个路由进程,mongos进程,这个路由器知道所有的数据存放位置,应用只需要直接与mongos交互即可。mongos自动将请求转到相应的片上获取数据,从应用角度看分不分片没有什么区别。
架构
Shard: 用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个replica set承担,防止主机单点故障
Config Server:mongod实例,存储了整个 ClusterMetadata。
Query Routers: 前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
Shard Key: 片键,设置分片时需要在集合中选一个键,用该键的值作为拆分数据的依据,这个片键称之为(shard key),片键的选取很重要,片键的选取决定了数据散列是否均匀。
搭建
集群规划
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Shard Server 1:27017 Shard Repl 1:27018
Shard Server 2:27019 Shard Repl 2:27020
Shard Server 3:27021 Shard Repl 3:27022
Config Server :27023 Config Server :27024 Config Server :27025
Route Process :27026
|
进入bin目录创建数据目录
1 2 3 4 5 6 7 8 9 10 11 12
| mkdir -p ../cluster/shard/s0 mkdir -p ../cluster/shard/s0-repl
mkdir -p ../cluster/shard/s1 mkdir -p ../cluster/shard/s1-repl
mkdir -p ../cluster/shard/s2 mkdir -p ../cluster/shard/s2-repl
mkdir -p ../cluster/shard/config1 mkdir -p ../cluster/shard/config2 mkdir -p ../cluster/shard/config3
|
启动分片服务
启动s0,r0
1 2 3
| ./mongod --port 27017 --dbpath ../cluster/shard/s0 --bind_ip 0.0.0.0 --shardsvr --replSet r0/124.239.149.46:27018
./mongod --port 27018 --dbpath ../cluster/shard/s0-repl --bind_ip 0.0.0.0 --shardsvr --replSet r0/124.239.149.46:27017
|
配置
1 2 3 4 5 6 7 8 9
| use admin
config = {_id: "r0", members: [ {_id:0,host:"124.239.149.46:27017"}, {_id:1,host:"124.239.149.46:27018"}, ] }
rs.initiate(config);//初始化
|
启动s1,r1
1 2 3
| ./mongod --port 27019 --dbpath ../cluster/shard/s1 --bind_ip 0.0.0.0 --shardsvr --replSet r1/124.239.149.46:27020
./mongod --port 27020 --dbpath ../cluster/shard/s1-repl --bind_ip 0.0.0.0 --shardsvr --replSet r1/124.239.149.46:27019
|
配置
1 2 3 4 5 6 7 8 9
| use admin
config = {_id: "r1", members: [ {_id:0,host:"124.239.149.46:27019"}, {_id:1,host:"124.239.149.46:27020"}, ] }
rs.initiate(config);//初始化
|
启动s2,r2
1 2 3
| ./mongod --port 27021 --dbpath ../cluster/shard/s2 --bind_ip 0.0.0.0 --shardsvr --replSet r2/124.239.149.46:27022
./mongod --port 27022 --dbpath ../cluster/shard/s2-repl --bind_ip 0.0.0.0 --shardsvr --replSet r2/124.239.149.46:27021
|
配置
1 2 3 4 5 6 7 8 9
| use admin
config = {_id: "r2", members: [ {_id:0,host:"124.239.149.46:27021"}, {_id:1,host:"124.239.149.46:27022"}, ] }
rs.initiate(config);//初始化
|
启动3个config服务
1 2 3 4 5
| ./mongod --port 27023 --dbpath ../cluster/shard/config1 --bind_ip 0.0.0.0 --replSet config/124.239.149.46:27024,124.239.149.46:27025 --configsvr
./mongod --port 27024 --dbpath ../cluster/shard/config2 --bind_ip 0.0.0.0 --replSet config/124.239.149.46:27023,124.239.149.46:27025 --configsvr
./mongod --port 27025 --dbpath ../cluster/shard/config3 --bind_ip 0.0.0.0 --replSet config/124.239.149.46:27023,124.239.149.46:27024 --configsvr
|
初始化 config server 副本集
1 2 3 4 5 6 7 8 9 10 11 12 13
| use admin
config = { _id:"config", configsvr: true, members:[ {_id:0,host:"124.239.149.46:27023"}, {_id:1,host:"124.239.149.46:27024"}, {_id:2,host:"124.239.149.46:27025"} ] }
rs.initiate(config); //初始化副本集配置
|
启动 mongos 路由服务
1
| ./mongos --port 27026 --configdb config/124.239.149.46:27023,124.239.149.46:27024,124.239.149.46:27025 --bind_ip 0.0.0.0
|
添加分片信息
1 2 3 4 5 6 7 8 9 10 11
| # 登录mongos服务 ./mongo --port 27026 use admin # 添加分片信息 db.runCommand({ addshard:"r0/124.239.149.46:27017,124.239.149.46:27018", "allowLocal":true }); db.runCommand({ addshard:"r1/124.239.149.46:27019,124.239.149.46:27020", "allowLocal":true }); db.runCommand({ addshard:"r2/124.239.149.46:27021,124.239.149.46:27022", "allowLocal":true }); # 指定分片库 db.runCommand({ enablesharding: "baizhi" }); # 设置库的片键信息 db.runCommand({ shardcollection: "baizhi.emps", key: { _id: "hashed"}})
|
测试分片
1 2 3 4 5 6 7 8 9
| # 连接mongos ./mongo --port 27026 # 切换库 use baizhi # 插入1000条信息 for(let i = 0; i < 1000; i++) { db.emps.insert({_id: i, name: "xxx_" + i}); } # 登录3个分片分别查询插入的数量
|
其他资料
MongoDB权威指南
MongoDB基本语法
MongoDB笔记