江門網(wǎng)站建設(shè)公司哪里注冊域名最便宜
提示:文章寫完后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
目錄
前言
一、概述
1.1 相關(guān)概念
1.2 特性
二、應(yīng)用場景
三、安裝
四、目錄結(jié)構(gòu)
五、默認(rèn)數(shù)據(jù)庫
六、 數(shù)據(jù)庫操作
?6.1?庫操作
6.2? 文檔操作
七、MongoDB數(shù)據(jù)庫備份?
7.1 備份命令
7.2 恢復(fù)命令
總結(jié)
前言
? ? ? ?MongoDB是一種面向文檔的NoSQL數(shù)據(jù)庫管理系統(tǒng),它采用了分布式文件存儲結(jié)構(gòu),可用于存儲和處理大量結(jié)構(gòu)靈活的文檔數(shù)據(jù)。MongoDB以JSON樣式的BSON(Binary JSON)格式存儲數(shù)據(jù),支持復(fù)雜的查詢和索引功能。它具有高可擴(kuò)展性、高性能和高可用性,并且適用于各種應(yīng)用場景,如Web應(yīng)用、移動(dòng)應(yīng)用、物聯(lián)網(wǎng)等。MongoDB被廣泛應(yīng)用于大數(shù)據(jù)、云計(jì)算和實(shí)時(shí)分析等領(lǐng)域。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、概述
mongodb是一個(gè)nosql數(shù)據(jù)庫,它有高性能、無模式、文檔型的特點(diǎn)。是nosql數(shù)據(jù)庫中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。數(shù)據(jù)庫格式為BSON。
1.1 相關(guān)概念
? ? 實(shí)例:系統(tǒng)上運(yùn)行的mongodb的進(jìn)程,類似于mysql實(shí)例;
?? ?庫:每個(gè)數(shù)據(jù)庫都是獨(dú)立的,有自己的用戶,權(quán)限,獨(dú)立存儲集合,類似于mysql的庫;
?? ?集合:由一組文檔構(gòu)成,類似于mysql的表;
?? ?文檔:mongodb數(shù)據(jù)庫的最小數(shù)據(jù)集,是由多個(gè)鍵值對有序組合的數(shù)據(jù)單元,類似于mysql的數(shù)據(jù)記錄;
?? ?主鍵?? ? ? 唯一標(biāo)識一行數(shù)據(jù)
1.2 特性
? ? ? ? ? ? 1、面向集合文檔存儲,適合存儲json形式的數(shù)據(jù);
?? ? ? ? ? ?2、格式自由,數(shù)據(jù)格式不固定,數(shù)據(jù)結(jié)構(gòu)發(fā)生變更的同時(shí)不會影響程序運(yùn)行;
?? ? ? ? ? ?3、面向?qū)ο蟮膕ql查詢語句,基本涵蓋關(guān)系型數(shù)據(jù)庫的所有查詢語句;
?? ? ? ? ? ?4、有索引的支持,查詢效率更快;
?? ? ? ? ? ?5、支持復(fù)制和自動(dòng)故障轉(zhuǎn)移;
?? ? ? ? ? ?6、可以使用分片集群提升查詢性能
二、應(yīng)用場景
? ? 游戲
?? ?物流
?? ?社交
?? ?物聯(lián)網(wǎng)
?? ?視頻直播
?? ?大數(shù)據(jù)
三、安裝
?? ?編譯安裝
?? ?RPM安裝
?? ??? ?[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
?? ??? ?yum install -y mongodb-org
[root@localhost yum.repos.d]# yum install -y mongodb-org
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.huaweicloud.com* updates: mirrors.huaweicloud.com
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在嘗試其它鏡像。
mongodb-org-4.2 | 1.3 kB 00:00
mongodb-org-4.2/7/primary | 25 kB 00:01
mongodb-org-4.2 210/210
正在解決依賴關(guān)系
--> 正在檢查事務(wù)
---> 軟件包 mongodb-org.x86_64.0.4.2.24-1.el7 將被 安裝
--> 正在處理依賴關(guān)系 mongodb-org-tools = 4.2.24,它被軟件包 mongodb-org-4.2.24-1.el7.x86_64 需要
--> 正在處理依賴關(guān)系 mongodb-org-mongos = 4.2.24,它被軟件包 mongodb-org-4.2.24-1.el7.x86_64 需要
--> 正在處理依賴關(guān)系 mongodb-org-shell = 4.2.24,它被軟件包 mongodb-org-4.2.24-1.el7.x86_64 需要
--> 正在處理依賴關(guān)系 mongodb-org-server = 4.2.24,它被軟件包 mongodb-org-4.2.24-1.el7.x86_64 需要
--> 正在檢查事務(wù)
---> 軟件包 mongodb-org-mongos.x86_64.0.4.2.24-1.el7 將被 安裝
---> 軟件包 mongodb-org-server.x86_64.0.4.2.24-1.el7 將被 安裝
---> 軟件包 mongodb-org-shell.x86_64.0.4.2.24-1.el7 將被 安裝
---> 軟件包 mongodb-org-tools.x86_64.0.4.2.24-1.el7 將被 安裝
--> 解決依賴關(guān)系完成依賴關(guān)系解決=========================================================================Package 架構(gòu) 版本 源 大小
=========================================================================
正在安裝:mongodb-org x86_64 4.2.24-1.el7 mongodb-org-4.2 6.2 k
為依賴而安裝:mongodb-org-mongos x86_64 4.2.24-1.el7 mongodb-org-4.2 11 Mmongodb-org-server x86_64 4.2.24-1.el7 mongodb-org-4.2 20 Mmongodb-org-shell x86_64 4.2.24-1.el7 mongodb-org-4.2 13 Mmongodb-org-tools x86_64 4.2.24-1.el7 mongodb-org-4.2 26 M事務(wù)概要
=========================================================================
安裝 1 軟件包 (+4 依賴軟件包)總下載量:70 M
安裝大小:253 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mongodb-org-4.2/packages/mongodb-org-4.2.24-1.el7.x86_64.rpm: 頭V3 RSA/SHA256 Signature, 密鑰 ID 058f8b6b: NOKEY
mongodb-org-4.2.24-1.el7.x86_64.rpm 的公鑰尚未安裝
(1/5): mongodb-org-4.2.24-1.el7.x86_64.rpm | 6.2 kB 00:00:06
(2/5): mongodb-org-mongos-4.2.24-1.el7.x86_64.rpm | 11 MB 00:00:15
(3/5): mongodb-org-shell-4.2.24-1.el7.x86_64.rpm | 13 MB 00:00:06
(4/5): mongodb-org-server-4.2.24-1.el7.x86_64.rpm | 20 MB 00:00:17
(5/5): mongodb-org-tools-4.2.24-1.el7.x86_64.rpm | 26 MB 00:00:12
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
總計(jì) 2.0 MB/s | 70 MB 00:00:34
從 https://www.mongodb.org/static/pgp/server-4.2.asc 檢索密鑰
導(dǎo)入 GPG key 0x058F8B6B:用戶ID : "MongoDB 4.2 Release Signing Key <packaging@mongodb.com>"指紋 : e162 f504 a20c df15 827f 718d 4b7c 549a 058f 8b6b來自 : https://www.mongodb.org/static/pgp/server-4.2.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction正在安裝 : mongodb-org-tools-4.2.24-1.el7.x86_64 1/5 正在安裝 : mongodb-org-mongos-4.2.24-1.el7.x86_64 2/5 正在安裝 : mongodb-org-shell-4.2.24-1.el7.x86_64 3/5 正在安裝 : mongodb-org-server-4.2.24-1.el7.x86_64 4/5
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.正在安裝 : mongodb-org-4.2.24-1.el7.x86_64 5/5 驗(yàn)證中 : mongodb-org-server-4.2.24-1.el7.x86_64 1/5 驗(yàn)證中 : mongodb-org-4.2.24-1.el7.x86_64 2/5 驗(yàn)證中 : mongodb-org-shell-4.2.24-1.el7.x86_64 3/5 驗(yàn)證中 : mongodb-org-mongos-4.2.24-1.el7.x86_64 4/5 驗(yàn)證中 : mongodb-org-tools-4.2.24-1.el7.x86_64 5/5 已安裝:mongodb-org.x86_64 0:4.2.24-1.el7 作為依賴被安裝:mongodb-org-mongos.x86_64 0:4.2.24-1.el7 mongodb-org-server.x86_64 0:4.2.24-1.el7 mongodb-org-shell.x86_64 0:4.2.24-1.el7 mongodb-org-tools.x86_64 0:4.2.24-1.el7 完畢!
?? ?啟動(dòng)數(shù)據(jù)庫
?? ??? ?systemctl start mongod.service
?四、目錄結(jié)構(gòu)
rpm -ql mongodb-org-server
?? ??? ?/etc/mongod.conf
?? ??? ??? ?主配置文件
?? ??? ??? ??? ?port: 27017? ? ? ?監(jiān)聽端口號
?? ??? ??? ??? ? ?bindIp: 127.0.0.1??? ? ? ?監(jiān)聽地址
?? ??? ?/run/mongodb? ? ? ? ?PID文件
?? ??? ?/usr/bin/mongod? ? ? ?啟動(dòng)命令
?? ??? ?/var/lib/mongo? ? ? ? ? ? 存儲數(shù)據(jù)文件
?? ??? ?/var/log/mongodb? ? ? ? ? ? 日志
?? ?rpm -ql mongodb-org-shell
?? ??? ?/usr/bin/mongo??? ?客戶端命令
?? ?rpm -ql mongodb-org-tools
?? ??? ?/usr/bin/mongodump? ? ? ? ? 備份數(shù)據(jù)庫
?? ??? ?/usr/bin/mongoexport? ? ? ? ?備份文檔
?? ??? ?/usr/bin/mongoimport? ? ? ? ?恢復(fù)文檔
?? ??? ?/usr/bin/mongorestore? ? ? ? 恢復(fù)數(shù)據(jù)庫
五、默認(rèn)數(shù)據(jù)庫
? ? admin:
?? ??? ?從權(quán)限的角度來看,這是"root"數(shù)據(jù)庫。要是將一個(gè)用戶添加到這個(gè)數(shù)據(jù)庫,這個(gè)用戶自動(dòng)繼承所有數(shù)據(jù)庫的權(quán)限。
一些特定的服務(wù)器端命令也只能從這個(gè)數(shù)據(jù)庫運(yùn)行,比如列出所有的數(shù)據(jù)庫或者關(guān)閉服務(wù)器。
?? ?local:
?? ??? ?這個(gè)數(shù)據(jù)庫永遠(yuǎn)不會被復(fù)制,可以用來存儲限于本地單臺服務(wù)器的任意集合。
?? ?config:
?? ??? ?當(dāng)Mongo用于分片設(shè)置時(shí),config數(shù)據(jù)庫在內(nèi)部使用,用于保存分片的相關(guān)信息。?
六、 數(shù)據(jù)庫操作
?6.1?庫操作
登錄數(shù)據(jù)庫
mongo
[root@localhost ~]# mongo
MongoDB shell version v4.2.24
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bb8264f9-b2bf-4492-b6b1-78f88888cf4f") }
MongoDB server version: 4.2.24
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, seehttps://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forumshttps://community.mongodb.com
Server has startup warnings:
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
查看數(shù)據(jù)庫
?? ??? ?show databases
?? ??? ?show dbs
?? ?顯示當(dāng)前所在數(shù)據(jù)庫
?? ??? ?db
?? ?切換數(shù)據(jù)庫
?? ??? ?use ?dbName
?? ?數(shù)據(jù)庫創(chuàng)建
?? ??? ?隱式創(chuàng)建
?? ??? ??? ?不用主動(dòng)創(chuàng)建,使用use newDB,會自動(dòng)創(chuàng)建不存在的數(shù)據(jù)庫
?? ??? ??? ?只有在庫中創(chuàng)建集合后才會保存,并使用show dbs查看到
?? ?刪除數(shù)據(jù)庫
?? ??? ?use dbName
?? ??? ?db.dropdatabase()
?
6.2? 文檔操作
? ? ?查看集合
?? ??? ?show tables
?? ?創(chuàng)建集合
?? ??? ?db.createCollection("test")
? ? ?刪除集合
?? ??? ?db.需要?jiǎng)h除的集合名稱.drop()
?? ?插入數(shù)據(jù)(文檔)
?? ??? ?db.集合名稱.insert({key:value})
?? ??? ??? ?單行
?? ??? ?db.集合名稱.insertmany([{key:value},{key:value},{key:value}])
?? ??? ??? ?多行? mongodb默認(rèn)數(shù)字的數(shù)據(jù)類型float浮點(diǎn)型,若要改變?yōu)檎?#xff1a;NumberInt(數(shù)字)
? 查詢數(shù)據(jù)
?? ??? ?全集合查詢
?? ??? ??? ?db.集合名稱.find({})
? ? ? ? ? ? db.集合名稱.find()
?? ??? ?條件查詢
?? ??? ??? ?db.info.find({查詢條件1key:value,查詢條件2,...},{key1:1|0,key2:1|0,...})
?? ??? ??? ?key:1 顯示,key:0 不顯示
?? ??? ??? ?當(dāng)顯示的key只有一個(gè)時(shí),key:1 只顯示該key及對應(yīng)value,key:0 顯示除了該key之外其他所有keyvalue
?? ??? ?查詢集合中有多少文檔
?? ??? ??? ?db.集合名稱.count()
?? ?刪除數(shù)據(jù)
?? ??? ?全文刪除
?? ??? ??? ?db.集合名稱.remove({})
?? ??? ?條件刪除
?? ??? ??? ?db.集合名稱.remove({key:value})?
?? ?更新數(shù)據(jù)
?? ??? ?db.info.update({_id:"3"}, {$set:{nickname:"jack"}})
?? ??? ?所有滿足條件的文檔都進(jìn)行更新
? ? ? ? ? ? ?db.info.update({userid:"1003"},{$set:{nickname:"tom"}},{multi:true})
?? ??? ?更新文檔進(jìn)行數(shù)值遞增
?? ??? ??? ?db.info.update({userid:"1003"},{$inc:{likenum:NumberInt(1000)}})
七、MongoDB數(shù)據(jù)庫備份?
7.1 備份命令
mongodump
?? ??? ?選項(xiàng)
?? ??? ??? ?-h=hostname:port
?? ??? ??? ?-u=<username>
?? ??? ??? ?-p=<password>
?? ??? ??? ?--authenticationDatabase=<dbname>
?? ??? ??? ?-d=<database>
?? ??? ??? ?-o=<path>
?? ??? ?語法
?? ??? ??? ?mongodump -d dbName -h hostName:port -u userName -p Password -o backupDirectory
mongoexport
?? ??? ?選項(xiàng)
?? ??? ??? ?-h=hostname:port
?? ??? ??? ?-u=<username>
?? ??? ??? ?-p=<password>
?? ??? ??? ?--authenticationDatabase=<dbname>
?? ??? ??? ?-d=<database>
?? ??? ??? ?-o=<path>
?? ??? ?語法
?? ??? ??? ?mongoexport -d dbName -c tableName -h hostName:port -u userName -p Password -o backupDirectory/jsonFile.json
?
7.2 恢復(fù)命令
?mongorestore
?? ??? ?選項(xiàng)
?? ??? ??? ?-h=hostname:port
?? ??? ??? ?-u=<username>
?? ??? ??? ?-p=<password>
?? ??? ??? ?--authenticationDatabase=<dbname>
?? ??? ??? ?-d=<database>
?? ??? ??? ?--drop 當(dāng)目標(biāo)數(shù)據(jù)庫中存在同名集合則刪除再恢復(fù)
?? ??? ?語法
?? ??? ??? ?mongorestore -h hostName -u username -p password -d dbName -c tableName bakcupDirector/bsonFile
?查看
[root@localhost ~]# mongo
MongoDB shell version v4.2.24
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("94fdfff5-03ac-4d95-a96b-f632b47a3f8b") }
MongoDB server version: 4.2.24
Server has startup warnings:
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2023-08-14T18:44:55.142+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2023-08-14T18:44:55.143+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---> show dbs
admin 0.000GB
config 0.000GB
jx 0.000GB
local 0.000GB
> use jx
switched to db jx
> show tables
info1
?mongoimport
?? ??? ?選項(xiàng)
?? ??? ??? ?-h=hostname:port
?? ??? ??? ?-u=<username>
?? ??? ??? ?-p=<password>
?? ??? ??? ?--authenticationDatabase=<dbname>
?? ??? ??? ?-d=<database>
?? ??? ??? ?--drop 當(dāng)目標(biāo)數(shù)據(jù)庫中存在同名集合則刪除再恢復(fù)
?? ??? ?語法
?? ??? ??? ?mongoimport -d dbName -c tableName -h hostName:port -u userName -p Password ?backupDirectory/jsonFile.json?
總結(jié)
? ? ? ?mongodb是一個(gè)nosql數(shù)據(jù)庫,它有高性能、無模式、文檔型的特點(diǎn)。是nosql數(shù)據(jù)庫中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。所以,一起好好學(xué)吧!