一、NoSQL介绍
1.NoSQL 简介
NoSQL(NoSQL = Not Only SQL ),意即”不仅仅是SQL”,指的是非关系型的数据库,是对不同于传统的关系型数据库的数据库管理系统的统称。
在现代的计算系统上每天网络上都会产生庞大的数据量。
这些数据有很大一部分是由关系数据库管理系统(RDMBSs)来处理,也有一部分使用非系型数据库处理
对NoSQL最普遍的解释是”非关联型的”,强调Key-Value Stores和文档数据库的优点,而不是单纯的反对RDBMS。
NoSQL用于超大规模数据的存储。(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据)。这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。
2.为什么使用NoSQL
今天我们可以通过第三方平台(如:Google,Facebook等)可以很容易的访问和抓取数据。用户的个人信息,社交网络,地理位置,用户生成的数据和用户操作日志已经成倍的增加。我们如果要对这些用户数据进行挖掘,那SQL数据库已经不适合这些应用了, NoSQL数据库的发展也却能很好的处理这些大的数据。
二、MongoDB简介
Mongodb由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。是专为可扩展性,高性能和高可用性而设计的数据库, 是非关系型数据库中功能最丰富,最像关系型数据库的,它支持的数据结构非常散,是类似 json 的 bjson 格式,因此可以存储比较复杂的数据类型。
MongoDB的(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业,各个行业以及各类应用程序的开源数据库。作为一个适用于敏捷开发的数据库,MongoDB的的数据模式可以随着应用程序的发展而灵活地更新。
MongoDB 以一种叫做 BSON(二进制 JSON)的存储形式将数据作为文档存储。具有相似结构的文档通常被整理成集合。可以把这些集合看成类似于关系数据库中的表: 文档和行相似, 字段和列相似。
区别在于:对 {sex:1} ,在JSON的存储上 1 只使用了一个字节,而如果用BSON,那就是至少4个字节
1.MySQL与mongoDB
mysql
MongoDB
库
库
表
集合
字段
键值
行
文档
1)数据库中数据(student库,user表)
uid
name
age
1
zhangyu
18
2
chencgheng
28
2)mongoDB中的数据(student库,user集合)
1) {uid:1,name:zhangyu,age:18}
2) {uid:2,name:chencgheng,age:28}
3)区别
1.数据结构不同
2.数据库添加不存在字段的数据时报错
3.mongoDB可以添加不存在的字段的数据
4.mongoDB不需要提前创建好库和表,创建数据直接会帮助我们创建好
2.MongoDB 特点
1.高性能:
Mongodb 提供高性能的数据持久性,索引支持更快的查询
2.丰富的语言查询:
Mongodb 支持丰富的查询语言来支持读写操作(CRUD)以及数据汇总
3.高可用性:
Mongodb 的复制工具,成为副本集,提供自动故障转移和数据冗余,
4.水平可扩展性:
Mongodb 提供了可扩展性,作为其核心功能的一部分,分片是将数据分在一组计算机上。
5.支持多种存储引擎:
WiredTiger存储引擎和、 MMAPv1存储引擎和 InMemory 存储引擎
3.0以上版本 3.0以下版本
新的引擎压缩比特别大,原来100个G,可能升级之后所有数据都在,只占用10个G
6.强大的索引支持:
地理位置索引可用于构建 各种 O2O 应用、文本索引解决搜索的需求、TTL索引解决历史数据自动过期的需求
3.MongoDB应用场景
1.游戏场景:使用 MongoDB 存储游戏用户信息,用户的装备、积分等直接以内嵌文档的形式存储,方便查询、更新
2.物流场景:使用 MongoDB 存储订单信息,订单状态在运送过程中会不断更新,以 MongoDB 内嵌数组的形式来存储,一次查询就能将订单所有的变更读取出来。
3.社交场景:使用 MongoDB 存储存储用户信息,以及用户发表的朋友圈信息,通过地理位置索引实现附近的人、地点等功能
将送快递骑手、快递商家的信息(包含位置信息)存储在 MongoDB,然后通过 MongoDB 的地理位置查询,这样很方便的实现了查找附近的商家、骑手等功能,使得快递骑手能就近接单
地图软件、打车软件、外卖软件,MongoDB强大的地理位置索引功能使其最佳选择
4.物联网场景:使用 MongoDB 存储所有接入的智能设备信息,以及设备汇报的日志信息,并对这些信息进行多维度的分析
5.视频直播:使用 MongoDB 存储用户信息、礼物信息等
6.电商场景:上衣有胸围,裤子有腰围,如果用数据库需要分成两个库,如果使用MongoDB都可以存在一起
三、MongoDB安装部署
0.安装依赖
[root@redis01 ~]
1.上传或下载包
[root@redis01 ~]
2.解压包
[root@redis01 ~]
[root@redis01 ~]
3.配置
[root@redis01 ~]
[root@redis01 ~]
systemLog:
destination: file
logAppend: true
path: /server/mongo_27017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /server/mongo_27017/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongo_27017/pid/mongod.pid
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.91
systemLog:
destination: file
logAppend: true
path: /server/mongo_27017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /server/mongo_27017/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongo_27017/pid/mongod.pid
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.91
4.启动
[root@redis01 ~]
about to fork child process, waiting until server is ready for connections.
forked process: 11547
child process started successfully, parent exiting
[root@redis01 ~]
root 11547 1 6 08:48 ? 00:00:00 /usr/local/mongodb/bin/mongod -f /server/mongo_27017/conf/mongodb.conf
5.配置环境变量
[root@redis01 ~]
export PATH="/usr/local/mongodb/bin:$PATH "
[root@redis01 ~]
四、mongo登录警告处理
1.警告一
WARNING: Access control is not enabled for the database.
[root@redis01 ~]
security:
authorization: enabled
2.警告二
WARNING: You are running this process as the root user, which is not recommended.
1.先关闭mongodb
[root@redis01 ~]
2.创建mongo用户
[root@redis01 ~]
[root@redis01 ~]
Changing password for user mongo.
passwd: all authentication tokens updated successfully.
3.授权目录
[root@redis01 ~]
[root@redis01 ~]
4.重新启动
[root@redis01 ~]
Last login: Wed May 27 09:07:51 CST 2020 on pts/1
[mongo@redis01 ~]$ mongod -f /server/mongo_27017/conf/mongodb.conf
3.警告三、四
WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always' .
We suggest setting it to 'never'
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always' .
We suggest setting it to 'never'
[root@redis01 ~]
[mongo@redis01 ~]$ mongod -f /server/mongo_27017/conf/mongodb.conf --shutdown
[mongo@redis01 ~]$ mongod -f /server/mongo_27017/conf/mongodb.conf
4.警告五
WARNING: soft rlimits too low. rlimits set to 7837 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
[root@redis01 ~]
ulimit -f unlimited
ulimit -t unlimited
ulimit -v unlimited
ulimit -n 65535
ulimit -m unlimited
ulimit -u 65535
[root@redis01 ~]
* soft nproc 65535
root soft nproc unlimited
[root@redis01 ~]
五、基本操作
1.操作说明
CRUD操作是create(创建), read (读取), update(更新)和delete(删除) 文档。
MongoDB不支持SQL但是支持自己的丰富的查询语言。
在MongoDB中,存储在集合中的每个文档都需要一个唯一的 _id字段,作为主键。如果插入的文档省略了该_id字段,则MongoDB驱动程序将自动为该字段生成一个ObjectId_id。也用于通过更新操作插入的文档upsert: true .如果文档包含一个_id字段,该_id值在集合中必须是唯一的,以避免重复键错误。
在MongoDB中,插入操作针对单个集合。 MongoDB中的所有写操作都是在单个文档的级别上进行的
2.基本操作
show databases/show dbs
show tables/show collections
use admin
db
show users
test :登录时默认存在的库
admin库:系统预留库,MongoDB系统管理库
local 库:本地预留库,存储关键日志
config库:MongoDB配置信息库
3.插入数据
1)单条数据插入
db.test.insert({"name" :"lhd" ,"age" :18,"sex" :"男" })
db.test.insert({"name" :"lhd" ,"age" :18,"sex" :"男" ,"address" :"上海浦东新区" })
db.test.insertOne({"name" :"lhd" ,"age" :18,"sex" :"男" ,"address" :"上海浦东新区" })
2)多条数据插入
db.inventory.insertMany( [
{ "name" : "lhd" , "age" : 18, "figure" : { "h" : 182, "w" : 200 }, "size" : "big" },
{ "name" : "qiudao" , "age" : 88, "figure" : { "h" : 120, "w" : 160 }, "size" : "very bittle" },
{ "name" : "zengdao" , "age" : 18, "figure" : { "h" : 180, "w" : 160 }, "size" : "nomel" },
]);
4.查询数据
1)查询所有数据
> db.test.find ()
{ "_id" : ObjectId("5ecdcdac13a4155a65ecb332" ), "name" : "lhd" , "age" : 18, "sex" : "男" }
{ "_id" : ObjectId("5ecdcdc413a4155a65ecb333" ), "name" : "lhd" , "age" : 18, "sex" : "男" , "address" : "上海浦东新区" }
{ "_id" : ObjectId("5ecdcdd213a4155a65ecb334" ), "name" : "lhd" , "age" : 18, "sex" : "男" }
{ "_id" : ObjectId("5ecdcdd813a4155a65ecb335" ), "name" : "lhd" , "age" : 18, "sex" : "男" , "address" : "上海浦东新区" }
2)查询单条数据
> db.test.findOne ()
{
"_id" : ObjectId("5ecdcdac13a4155a65ecb332" ),
"name" : "lhd" ,
"age" : 18,
"sex" : "男"
}
3)按条件查询
> db.test.findOne({"name" : "lhd" })
{
"_id" : ObjectId("5ecdcdac13a4155a65ecb332" ),
"name" : "lhd" ,
"age" : 18,
"sex" : "男"
}
> db.test.find({"age" :18})
{ "_id" : ObjectId("5f194695ed599eba23c605cf" ), "name" : "lhd" , "age" : 18, "sex" : "男" }
{ "_id" : ObjectId("5f1946feed599eba23c605d0" ), "name" : "lhd" , "age" : 18, "sex" : "男" , "address" : "上海浦东新区" }
{ "_id" : ObjectId("5f194737ed599eba23c605d1" ), "name" : "lhd" , "age" : 18, "sex" : "男" , "address" : "上海浦东新区" }
{ "_id" : ObjectId("5f194823ed599eba23c605d5" ), "name" : "lhd" , "age" : 18, "sex" : "男" , "address" : "上海浦东新区" }
4)查询多条件
> db.inventory.find({"figure.h" :120,"size" :"very bittle" })
> db.inventory.find(
{
"figure.h" :120,
"size" :"very bittle"
}
)
> db.inventory.find({$or :[{"figure.h" :120},{"size" :"big" }]})
> db.inventory.find(
{
$or [
{"figure.h" :120},
{"size" :"big" }
]
}
)
5)条件加范围的查询
> db.inventory.find({$or :[{"figure.h" :{$lt :130}},{"size" :"big" }]})
> db.inventory.find(
{
$or :[
{"figure.h" :{$lt :130}},
{"size" :"big" }
]
}
)
5.修改数据
1)修改单个数据
> db.inventory.updateOne({"name" :"qiudao" },{$set :{"figure.h" :130}})
> db.inventory.updateOne(
{"name" :"qiudao" },
{
$set :
{"figure.h" :130}
}
)
6.索引
1)查看执行计划
> db.inventory.find().explain ()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test2.inventory" ,
"indexFilterSet" : false ,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "COLLSCAN" ,
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "redis01" ,
"port" : 27017,
"version" : "3.6.13" ,
"gitVersion" : "db3c76679b7a3d9b443a0e1b3e45ed02b88c539f"
},
"ok" : 1
}
2)创建索引
> db.inventory.createIndex({"age" :1},{background:true })
{
"createdCollectionAutomatically" : true ,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
3)查看索引
> db.inventory.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_" ,
"ns" : "test2.test2"
},
{
"v" : 2,
"key" : {
"age" : 1
},
"name" : "name_1" ,
"ns" : "test2.test2" ,
"background" : true
}
]
4)再次查看执行计划
> db.inventory.find({"age" :{$lt :40}}).explain()
"winningPlan" : {
"stage" : "FETCH" ,
"inputStage" : {
"stage" : "IXSCAN" ,
6.删除
1)删除单条数据
> db.inventory.deleteOne({"name" :"lhd" })
{ "acknowledged" : true , "deletedCount" : 1 }
2)删除多个数据
> db.inventory.deleteMany({"name" :"lhd" })
3)删除索引
> db.test.dropIndex({ age: 1 })
{
"ok" : 0,
"errmsg" : "ns not found" ,
"code" : 26,
"codeName" : "NamespaceNotFound"
}
4)删除集合
> db
test2
> show tables;
inventory
test
> db.inventory.drop()
true
5)删除库
> db
test2
> db.dropDatabase()
六、mongo工具
mongo
mongodump
mongorestore
mongostat
mongod
mongoexport
mongoimport
mongos
mongotop
1.mongostat命令
[mongo@redis01 ~]$ mongostat
insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time
*0 *0 *0 *0 0 2|0 0.0% 0.0% 0 972M 56.0M 0|0 1|0 158b 60.9k 1 May 27 11:23:08.248
insert
query
update
delete
getmore
command
dirty
used
flushes
vsize
res
qrw
arw
net_in
net_out
conn
time
七、用户授权认证
1.授权命令
用户管理界面
要添加用户, MongoDB提供了该db.createUser()方法。添加用户时,您可以为用户分配色以授予权限。
注意:
在数据库中创建的第一个用户应该是具有管理其他用户的权限的用户管理员。
您还可以更新现有用户,例如更改密码并授予或撤销角色。
db.auth() 将用户验证到数据库。
db.changeUserPassword() 更改现有用户的密码。
db.createUser() 创建一个新用户。
db.dropUser() 删除单个用户。
db.dropAllUsers() 删除与数据库关联的所有用户。
db.getUser() 返回有关指定用户的信息。
db.getUsers() 返回有关与数据库关联的所有用户的信息。
db.grantRolesToUser() 授予用户角色及其特权。
db.removeUser() 已过时。从数据库中删除用户。
db.revokeRolesFromUser() 从用户中删除角色。
db.updateUser() 更新用户数据。
2.创建用户和角色
[mongo@db01 ~]$ mongo
> use admin
> db.createUser({user: "admin" ,pwd : "123456" ,roles:[ { role: "root" , db:"admin" }]})
Successfully added user: {
"user" : "admin" ,
"roles" : [
{
"role" : "root" ,
"db" : "admin"
}
]
}
3)查看用户
> db.getUsers()
[
{
"_id" : "test.admin" ,
"userId" : UUID("b840b96c-3442-492e-a45f-6ca7dff907fd" ),
"user" : "admin" ,
"db" : "test" ,
"roles" : [
{
"role" : "root" ,
"db" : "admin"
}
]
}
]
4)配置开启认证
[root@redis01 ~]
security:
authorization: enabled
[mongo@redis01 ~]$ mongod -f /server/mongo_27017/conf/mongodb.conf --shutdown
[mongo@redis01 ~]$ mongod -f /server/mongo_27017/conf/mongodb.conf
5)配置认证以后查操作不了
> show databases;
2020-05-27T11:41:06.186+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "there are no users authenticated" ,
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
shellHelper.show@src/mongo/shell/utils.js:860:19
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
6)使用账号密码连接
[mongo@redis01 ~]$ mongo -uadmin -p --authenticationDatabase admin
MongoDB shell version v3.6.13
Enter password:
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
test2 0.000GB
7)创建普通用户
> use test
> db.createUser(
{
user: "test" ,
pwd : "123456" ,
roles: [ { role: "readWrite" , db: "write" },
{ role: "read" , db: "read" } ]
}
)
8)创建测试数据
use write
db.write.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
db.write.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
db.write.insert({"name" :"haoda" ,"age" :18,"ad" :"上海浦东新区" })
db.write.insert({"name" :"linda" ,"age" :18,"ad" :"上海浦东新区" })
db.write.insert({"name" :"linhao" ,"age" :18,"ad" :"上海浦东新区" ,"sex" :"boy" })
use read
db.read.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
db.read.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
db.read.insert({"name" :"haoda" ,"age" :18,"ad" :"上海浦东新区" })
db.read.insert({"name" :"linda" ,"age" :18,"ad" :"上海浦东新区" })
db.read.insert({"name" :"linhao" ,"age" :18,"ad" :"上海浦东新区" ,"sex" :"boy" })
9)验证
mongo -utest -p --authenticationDatabase test
use write
db.write.find()
db.write.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
use read
db.read.find()
db.read.insert({"name" :"linhaoda" ,"age" :17,"ad" :"上海浦东新区" })
八、副本集的搭建
0.介绍副本集
1.创建多实例目录
[root@redis03 ~]
2.编辑多实例配置文件
[root@redis03 ~]
systemLog:
destination: file
logAppend: true
path: /server/mongodb/28017/logs/mongodb.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/28017/data
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/28017/pid/mongod.pid
net:
port: 28017
bindIp: 127.0.0.1,10.0.0.93
replication:
oplogSizeMB: 1024
replSetName: dba
3.启动多实例
[root@redis03 ~]
[root@redis03 ~]
[mongo@redis03 ~]$ mongod -f /server/mongodb/28017/conf/mongo.conf
[mongo@redis03 ~]$ mongod -f /server/mongodb/28018/conf/mongo.conf
[mongo@redis03 ~]$ mongod -f /server/mongodb/28019/conf/mongo.conf
[mongo@redis03 ~]$ netstat -lntp
tcp 0 0 10.0.0.93:27017 0.0.0.0:* LISTEN 20881/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 20881/mongod
tcp 0 0 10.0.0.93:28017 0.0.0.0:* LISTEN 32893/mongod
tcp 0 0 127.0.0.1:28017 0.0.0.0:* LISTEN 32893/mongod
tcp 0 0 10.0.0.93:28018 0.0.0.0:* LISTEN 32938/mongod
tcp 0 0 127.0.0.1:28018 0.0.0.0:* LISTEN 32938/mongod
tcp 0 0 10.0.0.93:28019 0.0.0.0:* LISTEN 32981/mongod
tcp 0 0 127.0.0.1:28019 0.0.0.0:* LISTEN 32981/mongod
4.登录多实例
[mongo@redis03 ~]$ mongo 10.0.0.93:28017
[mongo@redis03 ~]$ mongo 10.0.0.93:28018
[mongo@redis03 ~]$ mongo 10.0.0.93:28019
5.初始化副本集
config = {
_id : "dba" ,
members : [
{_id:0, host:"10.0.0.93:28017" },
{_id:1, host:"10.0.0.93:28018" },
{_id:2, host:"10.0.0.93:28019" },
]
}
rs.initiate(config)
6.查看副本集状态
dba:PRIMARY> rs.status()
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY" ,
"uptime" : 579,
"optime" : {
"ts" : Timestamp(1590593779, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-05-27T15:36:19Z" ),
"lastHeartbeat" : ISODate("2020-05-27T15:36:25.815Z" ),
dba:PRIMARY> rs.isMaster()
dba:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1024MB
log length start to end: 1543secs (0.43hrs)
oplog first event time: Wed May 27 2020 23:26:46 GMT+0800 (CST)
oplog last event time: Wed May 27 2020 23:52:29 GMT+0800 (CST)
now: Wed May 27 2020 23:52:38 GMT+0800 (CST)
dba:PRIMARY> rs.printSlaveReplicationInfo()
source : 10.0.0.93:28018
syncedTo: Wed May 27 2020 23:54:19 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source : 10.0.0.93:28019
syncedTo: Wed May 27 2020 23:54:19 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
dba:PRIMARY> rs.config()
7.主库创建数据,从库查看数据
db.table.insertMany([{"name" :"gcc" ,"age" :10},{"name" :"zzy" ,"age" :9},{"name" :"hxh" ,"age" :11}])
dba:PRIMARY> show tables
table
dba:PRIMARY> db.table.find()
dba:SECONDARY> show databases
2020-05-27T23:43:40.020+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1590594219, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false" ,
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk" ,
"$clusterTime " : {
"clusterTime" : Timestamp(1590594219, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA=" ),
"keyId" : NumberLong(0)
}
}
}
dba:SECONDARY> rs.slaveOk()
dba:SECONDARY> show databases
admin 0.000GB
cluster 0.000GB
config 0.000GB
local 0.000GB
[root@redis03 ~]
rs.slaveOk()
九、副本集实现高可用
1.故障切换测试
[mongo@redis03 ~]$ mongo localhost:28017
dba:PRIMARY> use admin
switched to db admin
dba:PRIMARY> db.shutdownServer()
2.程序怎么实现连接切换的
1.如果使用的是单节点,那么程序里面直接配置写死mongodb的ip和端口即可
2.如果是副本集集群的形式,在程序里面写的就是一个列表,列表里面写
mongo_reip=[10.0.0.91:28017,10.0.0.92:28018,10.0.0.93:29019]
程序会去使用命令询问谁是主节点,得到结果后在写入数据
3.恢复主库
注意:三台节点,只能坏一台,坏两台就有问题了
4.指定节点提升优先级
dba:PRIMARY> rs.conf()
"priority" : 1,
dba:PRIMARY> config=rs.conf()
dba:PRIMARY> config.members[0].priority=10
dba:PRIMARY> rs.reconfig(config)
dba:PRIMARY> rs.stepDown()
dba:PRIMARY> config=rs.conf()
dba:PRIMARY> config.members[0].priority=1
dba:PRIMARY> rs.reconfig(config)
十、扩容与删减节点
1.配置一台新的节点
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[mongo@redis03 ~]$ mongod -f /server/mongodb/28016/conf/mongo.conf
2.将新节点加入集群
dba:PRIMARY> rs.add("10.0.0.93:28016" )
{
"ok" : 1,
"operationTime" : Timestamp(1590597530, 1),
"$clusterTime " : {
"clusterTime" : Timestamp(1590597530, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA=" ),
"keyId" : NumberLong(0)
}
}
}
dba:PRIMARY> rs.status()
3.删除节点
dba:PRIMARY> rs.remove("10.0.0.93:28016" )
{
"ok" : 1,
"operationTime" : Timestamp(1590597842, 1),
"$clusterTime " : {
"clusterTime" : Timestamp(1590597842, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA=" ),
"keyId" : NumberLong(0)
}
}
}
dba:PRIMARY> rs.status()
4.添加仲裁节点
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[root@redis03 ~]
[mongo@redis03 ~]$ mongod -f /server/mongodb/28015/conf/mongo.conf
dba:PRIMARY> rs.addArb(("10.0.0.93:28015" )
十一、备份与恢复数据
1.备份恢复工具
1.mongoexport/mongoimport
2.mongodump/mongorestore
2.导出工具mongoexport
[mongo@redis03 ~]$ mongoexport --port 27017 -d database -c table -o ~/table.json
[mongo@redis03 ~]$ mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d database -c table -o ~/table.json
[mongo@redis03 ~]$ mongoexport --port 27017 -d database -c table --type =csv -f name,age -o ~/table.csv
[mongo@redis03 ~]$ mongoexport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d database -c table --type =csv -f name,age -o ~/table.csv
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明集合的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
3.恢复工具mongoimport
> use database
switched to db database
> show tables
table
> db.table.drop()
true
[mongo@redis03 ~]$ mongoimport --port 27017 -d database -c table ~/table.json
[mongo@redis03 ~]$ mongoimport --port 27017 -d database -c table --type =csv --headerline --file ~/table.csv
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明集合的名字
-f:指明要导入那些列
4.生产案例:数据库数据迁移至mongodb
1)搭建数据库
2)导入数据
3)配置数据库
[root@redis04 ~]
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
secure-file-priv=/tmp
[root@redis04 ~]
4)将数据库导出成csv
mysql> select * from world.city into outfile '/tmp/city1.csv' fields terminated by ',' ;
5)查看文件
[root@redis04 ~]
6)手动处理文件
[root@redis04 ~]
ID,Name,CountryCode,District,Population
1,Kabul,AFG,Kabol,1780000
2,Qandahar,AFG,Qandahar,237500
7)将数据导入mongodb
[root@redis04 ~]
[mongo@redis03 ~]$ mongoimport -uadmin -p123456 --port 27017 --authenticationDatabase admin -d world -c city --type =csv --headerline --file /tmp/city1.csv
8)查看数据
[mongo@redis03 ~]$ mongo -uadmin -p123456 --authenticationDatabase admin
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
read 0.000GB
world 0.000GB
write 0.000GB
> use world
switched to db world
> show tables
city
> db.city.find()
......
> it
5.生产案例:数据误删除恢复
1)过程
每天凌晨1点进行全备
10点进行误操作,删除了数据
恢复数据
2)模拟全备数据
[mongo@redis03 ~]$ mongo localhost:28018
dba:PRIMARY> use backup
dba:PRIMARY> db.backuptable.insertMany([{id :1},{id :2},{id :3}])
{
"acknowledged" : true ,
"insertedIds" : [
ObjectId("5ecfe698e99e372e2e4fe1fd" ),
ObjectId("5ecfe698e99e372e2e4fe1fe" ),
ObjectId("5ecfe698e99e372e2e4fe1ff" )
]
}
dba:PRIMARY> db.backuptable.find ()
{ "_id" : ObjectId("5ecfe698e99e372e2e4fe1fd" ), "id" : 1 }
{ "_id" : ObjectId("5ecfe698e99e372e2e4fe1fe" ), "id" : 2 }
{ "_id" : ObjectId("5ecfe698e99e372e2e4fe1ff" ), "id" : 3 }
3)执行全备
[mongo@redis03 ~]$ mongodump --port 28018 --oplog -o /data
[mongo@redis03 ~]$ ll /data/oplog.bson
-rw-rw-r-- 1 mongo mongo 110 May 29 02:01 /data/oplog.bson
4)模拟增量数据
[mongo@redis03 ~]$ mongo 10.0.0.93:28018
dba:PRIMARY> use backup
switched to db backup
dba:PRIMARY> db.backuptable.insertMany([{id :4},{id :5},{id :6}])
{
"acknowledged" : true ,
"insertedIds" : [
ObjectId("5ecfe86f5c1085fcf692a3cb" ),
ObjectId("5ecfe86f5c1085fcf692a3cc" ),
ObjectId("5ecfe86f5c1085fcf692a3cd" )
]
}
5)删除数据
dba:PRIMARY> use backup
switched to db backup
dba:PRIMARY> db.backuptable.drop()
trueb
dba:PRIMARY> show tables
7)oplog
oplog是local 库下的一个固定集合,从库就是通过查看主库的oplog这个集合来进行复制的。每个节点都有oplog,记录这从主节点复制过来的信息,这样每个成员都可以保证切换主库时的数据同步
6)查找删除动作的时间点
[mongo@redis03 ~]$ mongo 10.0.0.93:28018
dba:PRIMARY> use local
dba:PRIMARY> db.oplog.rs.find()
dba:PRIMARY> db.oplog.rs.find().pretty ()
{
"ts" : Timestamp(1590640219, 1),
"t" : NumberLong(1),
"h" : NumberLong("-8962736529514397515" ),
"v" : 2,
"op" : "n" ,
"ns" : "" ,
"wall" : ISODate("2020-05-28T04:30:19.080Z" ),
"o" : {
"msg" : "periodic noop"
}
}
dba:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1024MB
log length start to end: 1543secs (0.43hrs)
oplog first event time: Wed May 27 2020 23:26:46 GMT+0800 (CST)
oplog last event time: Wed May 27 2020 23:52:29 GMT+0800 (CST)
now: Wed May 27 2020 23:52:38 GMT+0800 (CST)
dba:PRIMARY> db.oplog.rs.find({ns:"backup.$cmd " }).pretty ()
{
"ts" : Timestamp(1590683811, 1),
"t" : NumberLong(2),
"h" : NumberLong("3968458855036608631" ),
"v" : 2,
"op" : "c" ,
"ns" : "backup.$cmd " ,
"ui" : UUID("bec471f5-cd2a-44fe-8056-4c5c2de5de03" ),
"wall" : ISODate("2020-05-28T16:36:51.227Z" ),
"o" : {
"drop" : "backuptable"
}
}
1590690412
7)备份最新的oplog
[mongo@redis03 ~]$ mongodump --port 28018 -d local -c oplog.rs -o /data/
[mongo@redis03 ~]$ ll /data/local/
total 140
-rw-rw-r-- 1 mongo mongo 138093 May 29 00:56 oplog.rs.bson
-rw-rw-r-- 1 mongo mongo 125 May 29 00:56 oplog.rs.metadata.json
8)把最新的oplog替换全备的oplog
[mongo@redis03 ~]$ mv /data/local/oplog.rs.bson /data/oplog.bson
9)恢复数据
[mongo@redis03 data]$ rm -rf /data/local
[mongo@redis03 data]$ mongorestore --port 28018 --oplogReplay --oplogLimit "1590690412:1" --drop /data/
10)查看数据
[mongo@redis03 ~]$ mongo localhost:28018
dba:PRIMARY> show databases
dba:PRIMARY> use backup
switched to db backup
dba:PRIMARY> show tables;
dba:PRIMARY> db.backuptable.find()
6.mongo升级
第12章升级步骤
1.首先确保是副本集状态
2.先关闭1个副本节点
3.检测数据是否可以升级
4.升级副本节点的可执行文件
5.更新配置文件
6.启动升级后的副本节点
7.确保集群工作正常
8.滚动升级其他副本节点
9.最后主节点降级
10.确保集群 可用
11.关闭降级的老的主节点
12.升级老的主节点
13.重新加入集群
十二、mongodb的分片
1.分片的概念
mongodb的副本集跟redis的高可用相同,只能读,分担不了主库的压力,只能在主库出现故障的时候接替主库的工作
mongodb能够使用的内存,只是主库的内存和磁盘,当副本集中机器配置不一致时也会有问题
2.分片的介绍
1.提高机器资源的利用率
2.减轻主库的压力
1.机器需要的更多
2.配置和管理更加的复杂和困难
3.分片配置好之后想修改很困难
3.分片的原理
1)路由服务 mongos server
类似于代理,跟数据库的atlas类似,可以将客户端的数据分配到后端的mongo服务器上
2)分片配置服务器信息 config server
mongos server是不知道后端服务器mongo有几台,地址是什么,他只能连接到这个config server,而config server就是记录后端服务器地址和数据的一个服务
作用:
1.记录后端mongo节点的信息
2.记录数据写入存到了哪个节点
3.提供给mongos后端服务器的信息
3)片键
config server只存储信息,而不会主动将数据写入节点,所以还有一个片键的概念,片键就是索引
作用:
1.将数据根据规则分配到不同的节点
2.相当于建立索引,加快访问速度
分类:
1.区间片键(很有可能出现数据分配不均匀的情况)
可以以时间区间分片,根据时间建立索引
可以以地区区间分片,根据地区建立索引
2.hash片键(足够平均,足够随机)
根据id 或者数据数量进行分配
4)分片
存储数据的节点,这种方式就是分布式集群
十三、分片的高可用
做分片只是针对单节点,mongo服务相当于还是只有一个,所以我们还有对分片进行副本集的操作
跟ES一样,我们不能一台机器上部署多节点,自己做自己的副本,那当机器挂了时,还是有问题
所以我们要错开进行副本集的建立,而且一台机器上不能有相同的数据节点,否则选举又会出现问题
1.服务器规划
主机
ip
部署
端口
mongodb01
10.0.0.81
Shard1_Master Shard2_Slave Shard3_Arbiter Config Server Mongos Server
20010 28020 28030 40000 60000
mongodb02
10.0.0.82
Shard2_Master Shard3_Slave Shard1_Arbiter Config Server Mongos Server
20010 28020 28030 40000 60000
mongodb03
10.0.0.83
Shard3_Master Shard1_Slave Shard2_Arbiter Config Server Mongos Server
20010 28020 28030 40000 60000
2.目录规划
mkdir /server/mongodb/master/{conf,log ,pid,data} -p
mkdir /server/mongodb/slave/{conf,log ,pid,data} -p
mkdir /server/mongodb/arbiter/{conf,log ,pid,data} -p
mkdir /server/mongodb/config/{conf,log ,pid,data} -p
mkdir /server/mongodb/mongos/{conf,log ,pid} -p
3.安装mongo
yum install -y libcurl openssl
rz mongodb-linux-x86_64-3.6.13.tgz
tar xf mongodb-linux-x86_64-3.6.13.tgz -C /usr/local/
ln -s /usr/local/mongodb-linux-x86_64-3.6.13 /usr/local/mongodb
4.配置mongodb01
1)配置master
vim /server/mongodb/master/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/master/log/master.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/master/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/master/pid/master.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28010
bindIp: 127.0.0.1,10.0.0.81
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
2)配置slave
vim /server/mongodb/slave/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/slave/log/slave.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/slave/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/slave/pid/slave.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28020
bindIp: 127.0.0.1,10.0.0.81
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
3)配置arbiter
vim /server/mongodb/arbiter/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/arbiter/log/arbiter.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/arbiter/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/arbiter/pid/arbiter.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28030
bindIp: 127.0.0.1,10.0.0.81
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
5.配置mongodb02
1)配置master
vim /server/mongodb/master/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/master/log/master.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/master/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/master/pid/master.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28010
bindIp: 127.0.0.1,10.0.0.82
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
2)配置slave
vim /server/mongodb/slave/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/slave/log/slave.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/slave/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/slave/pid/slave.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28020
bindIp: 127.0.0.1,10.0.0.82
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
3)配置arbiter
vim /server/mongodb/arbiter/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/arbiter/log/arbiter.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/arbiter/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/arbiter/pid/arbiter.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28030
bindIp: 127.0.0.1,10.0.0.82
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
6.配置mongodb03
1)配置master
vim /server/mongodb/master/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/master/log/master.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/master/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/master/pid/master.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28010
bindIp: 127.0.0.1,10.0.0.83
replication:
oplogSizeMB: 1024
replSetName: shard3
sharding:
clusterRole: shardsvr
2)配置slave
vim /server/mongodb/slave/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/slave/log/slave.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/slave/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/slave/pid/slave.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28020
bindIp: 127.0.0.1,10.0.0.83
replication:
oplogSizeMB: 1024
replSetName: shard1
sharding:
clusterRole: shardsvr
3)配置arbiter
vim /server/mongodb/arbiter/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/arbiter/log/arbiter.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/arbiter/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/arbiter/pid/arbiter.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 28030
bindIp: 127.0.0.1,10.0.0.83
replication:
oplogSizeMB: 1024
replSetName: shard2
sharding:
clusterRole: shardsvr
7.配置环境变量
[root@redis01 ~]
export PATH="/usr/local/mongodb/bin:$PATH "
[root@redis01 ~]
8.优化警告
useradd mongo -s /sbin/nologin -M
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
9.配置system管理
1)配置master管理
vim /usr/lib/systemd/system/mongod-master.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /server/mongodb/master/conf/mongo.conf
ExecStartPre=/usr/bin/chown -R mongo:mongo /server/mongodb/master/
ExecStop=/usr/local/mongodb/bin/mongod -f /server/mongodb/master/conf/mongo.conf --shutdown
PermissionsStartOnly=true
PIDFile=/server/mongodb/master/pid/master.pid
Type=forking
[Install]
WantedBy=multi-user.target
2)配置管理salve
vim /usr/lib/systemd/system/mongod-slave.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /server/mongodb/slave/conf/mongo.conf
ExecStartPre=/usr/bin/chown -R mongo:mongo /server/mongodb/slave/
ExecStop=/usr/local/mongodb/bin/mongod -f /server/mongodb/slave/conf/mongo.conf --shutdown
PermissionsStartOnly=true
PIDFile=/server/mongodb/slave/pid/slave.pid
Type=forking
[Install]
WantedBy=multi-user.target
3)配置管理arbiter
vim /usr/lib/systemd/system/mongod-arbiter.service
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=mongo
Group=mongo
ExecStart=/usr/local/mongodb/bin/mongod -f /server/mongodb/arbiter/conf/mongo.conf
ExecStartPre=/usr/bin/chown -R mongo:mongo /server/mongodb/arbiter/
ExecStop=/usr/local/mongodb/bin/mongod -f /server/mongodb/arbiter/conf/mongo.conf --shutdown
PermissionsStartOnly=true
PIDFile=/server/mongodb/arbiter/pid/arbiter.pid
Type=forking
[Install]
WantedBy=multi-user.target
4)刷新启动程序
systemctl daemon-reload
10.启动mongodb所有节点
systemctl start mongod-master.service
systemctl start mongod-slave.service
systemctl start mongod-arbiter.service
11.配置副本集
1)mongodb01初始化副本集
mongo --port 28010
rs.add("10.0.0.83:28020" )
rs.addArb("10.0.0.82:28030" )
2)mongodb02初始化副本集
mongo --port 28010
rs.add("10.0.0.81:28020" )b
rs.addArb("10.0.0.83:28030" )
3)mongodb03初始化副本集
mongo --port 28010
rs.add("10.0.0.82:28020" )
rs.addArb("10.0.0.81:28030" )
4)检查所有节点副本集状态
mongo --port 28010
rs.status()
rs.isMaster()
12.配置config server
1)创建目录
2)配置config server
vim /server/mongodb/config/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/config/log/mongodb.log
storage:
journal:
enabled: true
dbPath: /server/mongodb/config/data/
directoryPerDB: true
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
processManagement:
fork: true
pidFilePath: /server/mongodb/config/pid/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 40000
bindIp: 127.0.0.1,10.0.0.81
replication:
replSetName: configset
sharding:
clusterRole: configsvr
3)启动
/usr/local/mongodb/bin/mongod -f /server/mongodb/config/conf/mongo.conf
4)mongodb01上初始化副本集
mongo --port 40000
rs.initiate({
_id:"configset" ,
1configsvr: true ,
members:[
{_id:0,host:"10.0.0.51:40000" },
{_id:1,host:"10.0.0.52:40000" },
{_id:2,host:"10.0.0.53:40000" },
]})
5)检查
rs.status()
rs.isMaster()
13.配置mongos
1)创建目录
2)配置mongos
vim /server/mongodb/mongos/conf/mongo.conf
systemLog:
destination: file
logAppend: true
path: /server/mongodb/mongos/log/mongos.log
processManagement:
fork: true
pidFilePath: /server/mongodb/mongos/pid/mongos.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 60000
bindIp: 127.0.0.1,10.0.0.81
sharding:
configDB:
configset/10.0.0.81:40000,10.0.0.82:40000,10.0.0.83:40000
3)启动
/usr/local/mongodb/bin/mongod -f /server/mongodb/mongos/conf/mongo.conf
4)添加分片成员
mongo --port 60000
use admin
db.runCommand({addShard:'shard1/10.0.0.81:28100,10.0.0.83:28200,10.0.0.82:28300' })
db.runCommand({addShard:'shard2/10.0.0.82:28100,10.0.0.81:28200,10.0.0.83:28300' })
db.runCommand({addShard:'shard3/10.0.0.83:28100,10.0.0.82:28200,10.0.0.81:28300' })
5)查看分片信息
db.runCommand( { listshards : 1 } )
14.配置区间分片
1)区间分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "test" } )
2)创建集合索引
mongo --port 60000
use test
db.range.ensureIndex( { id : 1 } )
3)对集合开启分片,片键是id
use admin
db.runCommand( { shardcollection : "test.range" ,key : {id : 1} } )
4)插入测试数据
use test
for (i=1;i<10000;i++){ db.range.insert({"id" :i,"name" :"shanghai" ,"age" :28,"date" :new Date()}); }
db.range.stats()
db.range.count()
15.设置hash分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "testhash" } )
1)集合创建索引
use testhash
db.hash.ensureIndex( { id : "hashed" } )
2)集合开启哈希分片
use admin
sh.shardCollection( "testhash.hash" , { id : "hashed" } )
3)生成测试数据
use testhash
for (i=1;i<10000;i++){ db.hash.insert({"id" :i,"name" :"shanghai" ,"age" :70}); }
4)验证数据
分片验证
mongo --port 28010
use testhash
db.hash.count()
33755
mongo --port 28010
use testhash
db.hash.count()
33142
mongo --port 28010
use testhash
db.hash.count()
33102
16.分片集群常用管理命令
1.列出分片所有详细信息
db.printShardingStatus()
sh.status()
2.列出所有分片成员信息
use admin
db.runCommand({ listshards : 1})
3.列出开启分片的数据库
use config
db.databases.find({"partitioned" : true })
4.查看分片的片键
use config
db.collections.find().pretty()
十四、mongo配置密码做副本集
openssl rand -base64 123 > /server/mongodb/mongo.key
chown -R mongod.mongod /server/mongodb/mongo.key
chmod -R 600 /server/mongodb/mongo.key
scp -r /server/mongodb/mongo.key 192.168.1.82:/server/mongodb/
scp -r /server/mongodb/mongo.key 192.168.1.83:/server/mongodb/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现