平时做技术实践时,很多问题不是概念不会,而是细节没串起来。拿“MongoDB部署超详细步骤记录”来说,它看着像小点,放到项目里常会牵出环境、配置、兼容性和维护成本。下面按实际采用顺序,把思路、关键写法和容易踩坑的地方讲清楚,便于大家直接对照操作。
# https://www.mongodb.com/try/download/community
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.14.tgz
tar fx mongodb-linux-x86_64-rhel70-7.0.14.tgz -C /usr/local/
ln -s /usr/local/mongodb-linux-x86_64-rhel70-7.0.14/ /usr/local/mongodb
mkdir /usr/local/mongodb/{data,logs}
touch /usr/local/mongodb/logs/mongodb.log
vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
source /etc/profile
vim /etc/mongodb.conf
#指定数据库路径
dbpath=/usr/local/mongodb/data
#指定MongoDB日志文件
logpath=/usr/local/mongodb/logs/mongodb.log
# 使用追加的方式写日志
logappend=true
#端口号
port=27017
#方便外网访问
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
#auth=true #启用用户验证
#bind_ip=0.0.0.0 #绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
#replSet=single #开启oplog日志用于主从复制
# 启动
mongod -f /etc/mongodb.conf
# 关闭
mongod --shutdown -f /etc/mongodb.conf
ps -ef|grep mongodb
netstat -ntlp|grep 27017
# 下载链接:https://www.mongodb.com/try/download/shell
wget https://downloads.mongodb.com/compass/mongosh-2.3.2-linux-x64.tgz
tar fx mongosh-2.3.2-linux-x64.tgz
cp mongosh-2.3.2-linux-x64/bin/mongosh /usr/local/bin/
# 不需要认证
mongosh
# 需要认证
mongosh mongodb://192.168.9.25:27017/admin -u "admin" -p "abc123456"
# 只能创建在admin逻辑库
readAnyDatabase: 只可以把用户创建在admin逻辑库中,允许读取任何逻辑库
readWriteAnyDatabase: 只可以把用户创建在admin逻辑库中,允许读写任何逻辑库
dbAdminAnyDatabase: 只可以把用户创建在admin逻辑库中,允许管理任何逻辑库
userAdminAnyDatabase: 只可以把用户创建在admin逻辑库中,允许管理任何逻辑库用户
clusterAdmin: 只可以把用户创建在admin逻辑库中,允许管理MongoDB集群
root: 只可以把用户创建在admin逻辑库中,超级管理员,拥有最高权限
# 在指定逻辑库上创建
Read: 允许用户读取指定逻辑库
readWrite: 允许用户读写指定逻辑库
dbAdmin: 可以管理指定的逻辑库
userAdmin: 可以管理指定逻辑库的用户
# 创建管理员
use admin
db.createUser({user:"admin",pwd:"abc123456",roles:[{role:"root",db:"admin"}]})
# 创建普通角色
use common
db.createUser({user:"qyc",pwd:"abc123456",roles:[{role:"dbAdmin",db:"common"},{role:"readWrite",db:"common"}]})
# 查询所有
db.system.users.find().pretty()
show users
# 查询指定角色
db.getUser('qyc')
db.runCommand({usersInfo:"qyc"})
db.updateUser('qyc',{'roles':[{'role':'userAdmin','db':'common'},{'role':'read','db':'common'}]})
db.changeUserPassword("qyc", "123456")
db.dropUser('qyc')
db.auth('qyc','123456')
show dbs
# 切换到指定库,不存在会自动创建
use common
db
db.dropDatabase()
db.createCollection("student")
show collections
db.student.renameCollection("stu")
db.student.count()
# db.student.dataSize()
# 查看集合总大小(字节为单位)
db.student.totalSize()
# 查看集合的统计信息
db.student.stats()
db.student.drop()
# 插入单条
db.student.insertOne({name:"Scott",sex:"male",age:25,city:"Beijing"})
# 插入多条,save在_id主键存在就更新,不存在就插入
db.student.insert([{name:"Scott3",sex:"male",age:22,city:"Beijing"},{name:"Scott2",sex:"male",age:22,city:"Beijing"}])
db.student.insertMany([{name:"Scott3",sex:"male",age:22,city:"Beijing"},{name:"Scott2",sex:"male",age:22,city:"Beijing"}])
db.student.save([{name:"Scott3",sex:"male",age:22,city:"Beijing"},{name:"Scott2",sex:"male",age:22,city:"Beijing"}])
# 修改一条记录
db.student.update({name:"Scott2"},{$set:{age:26,classno:"2-6"}})
# 修改多条记录
db.student.updateMany({name:"Scott3"},{$set:{classno:"2-7"}})
# 在age属性上都加2
db.student.updateMany({},{$inc:{age:2}})
# 向数组属性添加元素
db.student.update({name:"Scott"},{$push:{role:"班长"}})
ObjectId("66dac03ddf68fdd4c95796d4").getTimestamp()
# 删除文档中的字段,{}代表修改所有
db.student.update({name:"Scott"},{$unset:{classno:"2-6"}})
# 删除数组中的某个元素
db.student.update({name:"Scott"},{$pull:{role:"班长"}})
# 删除所有文档
db.student.remove({})
# 删除指定文档
db.student.remove({name:"Scott2"})
| 表达式 | 说明 |
|---|---|
| $lt | 小于 |
| $gt | 大于 |
| $lte | 小于等于 |
| $gte | 大于等于 |
| $in | 包括 |
| $nin | 不包括 |
| $ne | 不等于 |
| $all | 全部匹配 |
| $not | 取反 |
| $or | 或 |
| $exists | 含有字段 |
# 查询所有文档
db.student.find()
# 查询指定文档,并显示指定字段,1为显示,0不显示
db.student.find({name:"Scott3",classno:"2-8"},{name:1,_id:0})
db.student.find({age:{$gte:24}})
db.student.find({name:/^S/})
# 查看单条记录
db.student.findOne({name:/3$/})
# 文档嵌套查询
# {class:{type: 1, data: [1,2,3]}}
db.student.find({data.class.type:1})
db.student.find({data.class.data.1:2})
db.student.find({data.class.data:[1,2,3]})
# 取前十条
db.student.find().limit(10)
# 从21条开始取十条
db.student.find().skip(20).limit(10)
# 数据排序(1代表升序,-1代表降序)
db.student.find().sort({name:1})
# 返回去重后指定数据,格式为数组
db.student.distinct("name")
# 为去重后数据排序,(-1为正序,1为倒序)
db.student.distinct("name").sort(function(){return -1})
# 截取数组中指定数据,(0,5)表示截取从第一行到第六行,(5)表示截取第六行到最后一行
db.stuent.distinct("name").slice(0,5)
# 1升序,-1降序,background代表在空闲时创建
db.student.createIndex({name:1})
db.student.createIndex({name:-1},{background:true,name:"index_name"})
db.student.createIndex({sid:1},{background:true,unique:true})
db.student.getIndexes()
db.student.dropIndexes()
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -o /data
# --dumpDbUsersAndRoles参数可以备份隶属于逻辑库的用户
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -o /data
# --gzip压缩备份,--oplog使用oplog进行时间点快照
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student -o /data
# 数据导出JSON或CSV格式数据,-f指定输出字段,-q指定查询语句
mongoexport --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student -f "_id,name,sex,age" -o student.json
# --drop表示导入前删除数据库中集合
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --drop -d school /data/school
# --gzip解压Gzip压缩存档还原,--oplogReplay重放oplog.bson中的操作内容,--oplogLimit与--oplogReplay一起使用时,可以限制重放到指定的时间点
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student /data/school/student.bson
# 导入json数据
mongoimport --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student --file student.json
# 使用oplog参数全备,需要开启副本集
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplog -o /data
# 解析oplog文件,找出全备最后一个时间的数据
bsondump /data/oplog.bson > /data/oplog.json
# 导出增量数据,这里修改为oplog.json最近一行的时间戳
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d local -c oplog.rs -q '{ts:{$gt:Timestamp(1610789118,416)}}' -o /data/oplog
# 恢复全备
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplogReplay /data
# 复制增量的oplog到备份目录,重命名为oplog.bson,将原来的oplog.bson覆盖
cp oplog.rs.bson /data/oplog.bson
# 将增量的oplog进行恢复,指定要恢复的时间点
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplogReplay --oplogLimit "1610789168:1" /data
到此这篇关于MongoDB部署超详细步骤记录的文章就介绍到这了,更多相关MongoDB部署内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多兼容脚本之家!