欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MongoDB部署超詳細步驟記錄

 更新時間:2025年03月12日 09:14:47   作者:木子運維  
這篇文章主要介紹了MongoDB部署超詳細步驟的相關(guān)資料,包括了MongoDB的安裝配置、MongoDB?Shell的安裝、常用命令操作及備份與恢復(fù)方法,需要的朋友可以參考下

一、MongoDB安裝配置

1. 下載安裝包

# https://www.mongodb.com/try/download/community
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.14.tgz

2. 解壓

tar fx mongodb-linux-x86_64-rhel70-7.0.14.tgz -C /usr/local/

3. 創(chuàng)建軟鏈接

ln -s /usr/local/mongodb-linux-x86_64-rhel70-7.0.14/ /usr/local/mongodb

4. 創(chuàng)建數(shù)據(jù)和日志目錄

mkdir /usr/local/mongodb/{data,logs}
touch /usr/local/mongodb/logs/mongodb.log

5. 設(shè)置環(huán)境變量

vim /etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH

6. 生效環(huán)境變量

source /etc/profile

7. 修改配置文件

vim /etc/mongodb.conf
#指定數(shù)據(jù)庫路徑
dbpath=/usr/local/mongodb/data
#指定MongoDB日志文件
logpath=/usr/local/mongodb/logs/mongodb.log
# 使用追加的方式寫日志
logappend=true
#端口號
port=27017 
#方便外網(wǎng)訪問
bind_ip=0.0.0.0
fork=true # 以守護進程的方式運行MongoDB,創(chuàng)建服務(wù)器進程
#auth=true #啟用用戶驗證
#bind_ip=0.0.0.0 #綁定服務(wù)IP,若綁定127.0.0.1,則只能本機訪問,不指定則默認本地所有IP
#replSet=single #開啟oplog日志用于主從復(fù)制

8. 啟動和關(guān)閉服務(wù)

# 啟動
mongod -f /etc/mongodb.conf
# 關(guān)閉
mongod --shutdown -f /etc/mongodb.conf

9. 驗證

 ps -ef|grep mongodb
 netstat -ntlp|grep 27017

二、MongoDB Shell安裝

1. 下載安裝包

# 下載鏈接:https://www.mongodb.com/try/download/shell
wget https://downloads.mongodb.com/compass/mongosh-2.3.2-linux-x64.tgz

2. 解壓

tar fx mongosh-2.3.2-linux-x64.tgz

3 . 修改命令目錄

cp mongosh-2.3.2-linux-x64/bin/mongosh /usr/local/bin/

4. 登錄

# 不需要認證
mongosh
# 需要認證
mongosh mongodb://192.168.9.25:27017/admin -u "admin" -p "abc123456"

三、常用命令合集

1. 角色操作

1)管理員角色

# 只能創(chuàng)建在admin邏輯庫
readAnyDatabase: 只可以把用戶創(chuàng)建在admin邏輯庫中,允許讀取任何邏輯庫
readWriteAnyDatabase: 只可以把用戶創(chuàng)建在admin邏輯庫中,允許讀寫任何邏輯庫
dbAdminAnyDatabase: 只可以把用戶創(chuàng)建在admin邏輯庫中,允許管理任何邏輯庫
userAdminAnyDatabase: 只可以把用戶創(chuàng)建在admin邏輯庫中,允許管理任何邏輯庫用戶
clusterAdmin: 只可以把用戶創(chuàng)建在admin邏輯庫中,允許管理MongoDB集群
root: 只可以把用戶創(chuàng)建在admin邏輯庫中,超級管理員,擁有最高權(quán)限

2)普通角色

# 在指定邏輯庫上創(chuàng)建
Read: 允許用戶讀取指定邏輯庫
readWrite: 允許用戶讀寫指定邏輯庫
dbAdmin: 可以管理指定的邏輯庫
userAdmin: 可以管理指定邏輯庫的用戶

3)創(chuàng)建角色

# 創(chuàng)建管理員
use admin
db.createUser({user:"admin",pwd:"abc123456",roles:[{role:"root",db:"admin"}]})
# 創(chuàng)建普通角色
use common
db.createUser({user:"qyc",pwd:"abc123456",roles:[{role:"dbAdmin",db:"common"},{role:"readWrite",db:"common"}]})

4)查詢角色

# 查詢所有
db.system.users.find().pretty()
show users
# 查詢指定角色
db.getUser('qyc')
db.runCommand({usersInfo:"qyc"})

5) 更新角色

db.updateUser('qyc',{'roles':[{'role':'userAdmin','db':'common'},{'role':'read','db':'common'}]})

6) 修改角色密碼

db.changeUserPassword("qyc", "123456")

7) 刪除角色

db.dropUser('qyc')

8) 角色認證

db.auth('qyc','123456')

2. 數(shù)據(jù)庫操作

1)查看所有庫

show dbs

2) 切換庫

# 切換到指定庫,不存在會自動創(chuàng)建
use common

3)查看當前庫

db

4)刪除當前庫

db.dropDatabase()

3. 集合操作

1)創(chuàng)建集合

db.createCollection("student")

2)查看集合

show collections

3)重命名集合

db.student.renameCollection("stu")

4) 查看集合記錄數(shù)量

db.student.count()

5) 查看集合數(shù)據(jù)空間容量

# db.student.dataSize()
# 查看集合總大小(字節(jié)為單位)
db.student.totalSize()
# 查看集合的統(tǒng)計信息
db.student.stats()

6) 刪除集合

db.student.drop()

4. 文檔操作

1)在集合中插入文檔

# 插入單條
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"}])

2)更新文檔

# 修改一條記錄
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}})
# 向數(shù)組屬性添加元素
db.student.update({name:"Scott"},{$push:{role:"班長"}})

3)從文檔主鍵ID中提取時間

ObjectId("66dac03ddf68fdd4c95796d4").getTimestamp()

4) 刪除文檔

# 刪除文檔中的字段,{}代表修改所有
db.student.update({name:"Scott"},{$unset:{classno:"2-6"}})
# 刪除數(shù)組中的某個元素
db.student.update({name:"Scott"},{$pull:{role:"班長"}})
# 刪除所有文檔
db.student.remove({})
# 刪除指定文檔
db.student.remove({name:"Scott2"})

5) 簡單查詢

表達式說明
$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]})

6)分頁查詢

# 取前十條
db.student.find().limit(10)
# 從21條開始取十條
db.student.find().skip(20).limit(10)

7) 文檔排序

# 數(shù)據(jù)排序(1代表升序,-1代表降序)
db.student.find().sort({name:1})

8) 文檔去重

# 返回去重后指定數(shù)據(jù),格式為數(shù)組
db.student.distinct("name")
# 為去重后數(shù)據(jù)排序,(-1為正序,1為倒序)
db.student.distinct("name").sort(function(){return -1})
# 截取數(shù)組中指定數(shù)據(jù),(0,5)表示截取從第一行到第六行,(5)表示截取第六行到最后一行
db.stuent.distinct("name").slice(0,5)

5. 索引操作

1) 創(chuàng)建索引

# 1升序,-1降序,background代表在空閑時創(chuàng)建
db.student.createIndex({name:1})
db.student.createIndex({name:-1},{background:true,name:"index_name"})

2) 創(chuàng)建唯一性索引

db.student.createIndex({sid:1},{background:true,unique:true})

3) 查看索引

db.student.getIndexes()

4) 刪除索引

db.student.dropIndexes()

四、備份與恢復(fù)

1. 全庫備份

mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -o /data

2. 備份邏輯庫

# --dumpDbUsersAndRoles參數(shù)可以備份隸屬于邏輯庫的用戶
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -o /data

3. 備份集合數(shù)據(jù)

# --gzip壓縮備份,--oplog使用oplog進行時間點快照
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student -o /data
# 數(shù)據(jù)導(dǎo)出JSON或CSV格式數(shù)據(jù),-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

4. 單庫恢復(fù)

# --drop表示導(dǎo)入前刪除數(shù)據(jù)庫中集合
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --drop -d school /data/school

5. 集合恢復(fù)

# --gzip解壓Gzip壓縮存檔還原,--oplogReplay重放oplog.bson中的操作內(nèi)容,--oplogLimit與--oplogReplay一起使用時,可以限制重放到指定的時間點
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student /data/school/student.bson
# 導(dǎo)入json數(shù)據(jù)
mongoimport --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin -d school -c student --file student.json

6. 增量恢復(fù)

# 使用oplog參數(shù)全備,需要開啟副本集
mongodump --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplog -o /data
# 解析oplog文件,找出全備最后一個時間的數(shù)據(jù)
bsondump /data/oplog.bson > /data/oplog.json
# 導(dǎo)出增量數(shù)據(jù),這里修改為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
# 恢復(fù)全備
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplogReplay /data
# 復(fù)制增量的oplog到備份目錄,重命名為oplog.bson,將原來的oplog.bson覆蓋
cp oplog.rs.bson /data/oplog.bson
# 將增量的oplog進行恢復(fù),指定要恢復(fù)的時間點
mongorestore --host=localhost --port=27017 -u admin -p abc123456 --authenticationDatabase=admin --oplogReplay --oplogLimit "1610789168:1" /data

總結(jié) 

到此這篇關(guān)于MongoDB部署超詳細步驟記錄的文章就介紹到這了,更多相關(guān)MongoDB部署內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • NoSQL反模式 - 文檔數(shù)據(jù)庫篇

    NoSQL反模式 - 文檔數(shù)據(jù)庫篇

    我們設(shè)計關(guān)系數(shù)據(jù)庫Schema的都有一套完整的方案,而NoSQL卻沒有這些。半年前筆者讀了本《SQL反模式》的書,覺得非常好。就開始留意,對于NoSQL是否也有反模式?好的反模式可以在我們設(shè)計Schema告訴哪里是陷阱和懸崖。
    2014-08-08
  • MongoDB數(shù)據(jù)備份遷移的全過程

    MongoDB數(shù)據(jù)備份遷移的全過程

    這篇文章主要記錄了MongoDB數(shù)據(jù)備份遷移的全過程,文中通過圖文結(jié)合的方式介紹的非常詳細,對大家了解學(xué)習(xí)MongoDB數(shù)據(jù)備份遷移有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • MongoDB對Document(文檔)的插入、刪除及更新

    MongoDB對Document(文檔)的插入、刪除及更新

    這篇文章介紹了MongoDB對Document(文檔)的插入、刪除及更新,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • MongoDB分組查詢和聚合查詢實例教程

    MongoDB分組查詢和聚合查詢實例教程

    聚合(aggregate)是MongoDB的高級查詢語言,它允許我們通過轉(zhuǎn)化合并多個文檔的數(shù)據(jù)來生成新的在單個文檔里不存在的文檔信息,下面這篇文章主要給大家介紹了關(guān)于MongoDB分組查詢和聚合查詢的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • springboot整合mongodb

    springboot整合mongodb

    這篇文章主要介紹了springboot如何整合mongodb,mongodb的安裝和使用,感興趣的同學(xué)可以參考閱讀本文
    2023-03-03
  • MongoDB 監(jiān)控工具mongostat和mongotop的使用

    MongoDB 監(jiān)控工具mongostat和mongotop的使用

    這篇文章主要介紹了MongoDB 監(jiān)控工具mongostat和mongotop的使用方法,幫助大家更好的理解和學(xué)習(xí)使用MongoDB,感興趣的朋友可以了解下
    2021-03-03
  • MongoDB通過查詢與游標徹底玩轉(zhuǎn)分布式文件存儲

    MongoDB通過查詢與游標徹底玩轉(zhuǎn)分布式文件存儲

    MongoDB最大的特點是它支持的查詢語言非常強大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引,這篇文章主要介紹了MongoDB查詢與游標,徹底玩轉(zhuǎn)分布式文件存儲,需要的朋友可以參考下
    2023-01-01
  • MongoDB 簡單入門教程(安裝、基本概念、創(chuàng)建用戶)

    MongoDB 簡單入門教程(安裝、基本概念、創(chuàng)建用戶)

    這篇文章主要介紹了MongoDB 簡單入門教程(安裝、基本概念、創(chuàng)建用戶)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用MongoDB數(shù)據(jù)庫,感興趣的朋友可以了解下
    2021-03-03
  • MongoDB快速入門及其SpringBoot實戰(zhàn)教程

    MongoDB快速入門及其SpringBoot實戰(zhàn)教程

    MongoDB是一個開源、高性能、無模式的文檔型數(shù)據(jù)庫,當初的設(shè)計就是用于簡化開發(fā)和方便擴展,是NoSQL數(shù)據(jù)庫產(chǎn)品中的一種,它支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是一種類似于JSON的格式叫BSON,本文介紹MongoDB快速入門及其SpringBoot實戰(zhàn),感興趣的朋友一起看看吧
    2023-12-12
  • MongoDB 刪除文檔的方式(刪除一個、批量刪除)

    MongoDB 刪除文檔的方式(刪除一個、批量刪除)

    這篇文章主要介紹了MongoDB 刪除文檔的方式(刪除一個、批量刪除),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04

最新評論