最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • mongodb 中rs.stauts()命令參數(shù)解析

    mongodb 中rs.stauts()命令參數(shù)解析

    MongoDB的rs.status()命令是查看副本集狀態(tài)的重要工具,它可以展示副本集中各個成員的角色、健康狀態(tài)、同步進度等關(guān)鍵信息,本文介紹mongodb 中rs.stauts()命令參數(shù)解析,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • MongoDB Map-Reduce 使用方法及原理解析

    MongoDB Map-Reduce 使用方法及原理解析

    MongoDB Map-Reduce 是一種強大的數(shù)據(jù)處理模式,可以用于對大量數(shù)據(jù)進行批量處理和聚合操作,本文將詳細介紹 MongoDB Map-Reduce 的原理、使用方法和最佳實踐,需要的朋友可以參考下
    2024-07-07
  • MongoDB入門教程(包含安裝、常用命令、相關(guān)概念、使用技巧、常見操作等)

    MongoDB入門教程(包含安裝、常用命令、相關(guān)概念、使用技巧、常見操作等)

    這篇文章主要介紹了MongoDB入門教程,包含安裝、常用命令、相關(guān)概念、使用技巧、常見操作等,是一篇比較好的入門文章,需要的朋友可以參考下
    2014-06-06
  • 淺談MongoDB內(nèi)部的存儲原理

    淺談MongoDB內(nèi)部的存儲原理

    這篇文章主要介紹了淺談MongoDB內(nèi)部的存儲原理,MongoDB是一個面向文檔的數(shù)據(jù)庫系統(tǒng)。使用C++編寫,不支持SQL,但有自己功能強大的查詢語法,需要的朋友可以參考下
    2023-07-07
  • PHP中的mongodb group操作實例

    PHP中的mongodb group操作實例

    這篇文章主要介紹了PHP中的mongodb group操作實例,本文給出了3個group命令的用法,需要的朋友可以參考下
    2014-09-09
  • window下安裝配置mongodb的教程圖解

    window下安裝配置mongodb的教程圖解

    本文通過圖文并茂的形式給大家介紹了window下安裝配置mongodb的方法,需要的朋友可以參考下
    2018-02-02
  • MongoDB如何查詢耗時記錄的方法詳解

    MongoDB如何查詢耗時記錄的方法詳解

    查詢操作是我們?nèi)粘2僮鲾?shù)據(jù)庫經(jīng)常會遇到的一個功能,下面這篇文章主要給大家介紹了關(guān)于MongoDB如何查詢耗時記錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-09-09
  • Mongodb實戰(zhàn)之全文搜索功能

    Mongodb實戰(zhàn)之全文搜索功能

    全文檢索對每一個詞建立一個索引,指明該詞在文章中出現(xiàn)的次數(shù)和位置,當用戶查詢時,檢索程序就根據(jù)事先建立的索引進行查找,并將查找的結(jié)果反饋給用戶的檢索方式。下面這篇文章主要給大家介紹了Mongodb全文搜索功能的相關(guān)資料,需要的朋友可以參考下。
    2017-07-07
  • 2021最新版windows10系統(tǒng)MongoDB數(shù)據(jù)庫安裝及配置環(huán)境

    2021最新版windows10系統(tǒng)MongoDB數(shù)據(jù)庫安裝及配置環(huán)境

    這篇文章主要介紹了2021最新版MongoDB數(shù)據(jù)庫安裝及配置環(huán)境(windows10系統(tǒng)),本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • MongoDB聚合$listSampledQueries實例操作

    MongoDB聚合$listSampledQueries實例操作

    使用$listSampledQueries可以返回指定集合或所有集合的采樣查詢,analyzeShardKey命令使用采樣查詢來計算分片密鑰的讀寫分布指標,這篇文章主要介紹了MongoDB聚合$listSampledQueries,需要的朋友可以參考下
    2024-02-02

最新評論

聊城市| 大宁县| 湘乡市| 右玉县| 绍兴市| 磐石市| 靖江市| 黔西县| 蕲春县| 嵊泗县| 关岭| 德昌县| 张家港市| 惠东县| 都安| 当涂县| 宣汉县| 右玉县| 奉化市| 瑞金市| 麻城市| 潢川县| 武夷山市| 绥宁县| 班戈县| 屏边| 衡山县| 永年县| 齐河县| 海林市| 舟曲县| 灌阳县| 屏南县| 温宿县| 贵港市| 定襄县| 建宁县| 萨迦县| 海盐县| 汶上县| 松桃|