網(wǎng)站空間購買長沙關(guān)鍵詞優(yōu)化方法
目錄
一、概述
1.概念
2.相關(guān)
2.1?實例
2.2?庫
2.3??集合
2.4??文檔
2.5 主鍵
3.特性
4,應(yīng)用場景
二、安裝
1.RPM安裝
2.啟動數(shù)據(jù)庫
三、目錄結(jié)構(gòu)
1.rpm -ql mongodb-org-server
2.rpm -ql mongodb-org-shell
3.rpm -ql mongodb-org-tools
四、默認數(shù)據(jù)庫
1.admin
2.local
3.config
五、數(shù)據(jù)庫操作
1.庫操作
1.1?查看數(shù)據(jù)庫
1.2?顯示當前所在數(shù)據(jù)庫
1.3?切換數(shù)據(jù)庫
1.4?數(shù)據(jù)庫創(chuàng)建
1.5?刪除數(shù)據(jù)庫
2.文檔操作
2.1?查看集合
2.2?創(chuàng)建集合
2.3?刪除集合
2.4?插入數(shù)據(jù)(文檔)
2.5?查詢數(shù)據(jù)
2.6?刪除數(shù)據(jù)
2.7?更新數(shù)據(jù)
六、MongoDB數(shù)據(jù)庫備份
1.備份命令
1.1?mongodump
1.2?mongoexport
2.恢復(fù)命令
2.1?mongorestore
2.2?mongoimport
一、概述
1.概念
mongodb是一個nosql數(shù)據(jù)庫,它有高性能、無模式、文檔型的特點。是nosql數(shù)據(jù)庫中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。數(shù)據(jù)庫格式為BSON
2.相關(guān)
2.1?實例
系統(tǒng)上運行的mongodb的進程,類似于mysql實例。
2.2?庫
每個數(shù)據(jù)庫都是獨立的,有自己的用戶,權(quán)限,獨立存儲集合,類似于mysql的庫。
2.3??集合
由一組文檔構(gòu)成,類似于mysql的表。
2.4??文檔
mongodb數(shù)據(jù)庫的最小數(shù)據(jù)集,是由多個鍵值對有序組合的數(shù)據(jù)單元,類似于mysql的數(shù)據(jù)記錄。
2.5 主鍵
唯一標識一行數(shù)據(jù)
3.特性
- 面向集合文檔存儲,適合存儲json形式的數(shù)據(jù);
- 格式自由,數(shù)據(jù)格式不固定,數(shù)據(jù)結(jié)構(gòu)發(fā)生變更的同時不會影響程序運行;
- 面向?qū)ο蟮膕ql查詢語句,基本涵蓋關(guān)系型數(shù)據(jù)庫的所有查詢語句;
- 有索引的支持,查詢效率更快;
- 支持復(fù)制和自動故障轉(zhuǎn)移;
- 可以使用分片集群提升查詢性能
4,應(yīng)用場景
- 游戲
- 物流
- 社交
- 物聯(lián)網(wǎng)
- 視頻直播
- 大數(shù)據(jù)
二、安裝
1.RPM安裝
構(gòu)建源
[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
2.啟動數(shù)據(jù)庫
systemctl start mongod.service
三、目錄結(jié)構(gòu)
1.rpm -ql mongodb-org-server
/etc/mongod.conf????????主配置文件
/run/mongodb????????PID文件
/usr/bin/mongod????????啟動命令
/var/lib/mongo????????存儲數(shù)據(jù)文件
/var/log/mongodb????????日志
注:主配置文件中監(jiān)聽端口 port: 27017,監(jiān)聽地址bindIp: 127.0.0.1
2.rpm -ql mongodb-org-shell
/usr/bin/mongo????????客戶端命令
3.rpm -ql mongodb-org-tools
/usr/bin/mongodump????????備份數(shù)據(jù)庫
/usr/bin/mongoexport????????備份文檔
/usr/bin/mongoimport????????恢復(fù)文檔
/usr/bin/mongorestore????????恢復(fù)數(shù)據(jù)庫
四、默認數(shù)據(jù)庫
1.admin
從權(quán)限的角度來看,這是"root"數(shù)據(jù)庫。要是將一個用戶添加到這個數(shù)據(jù)庫,這個用戶自動繼承所有數(shù)據(jù)庫的權(quán)限。
一些特定的服務(wù)器端命令也只能從這個數(shù)據(jù)庫運行,比如列出所有的數(shù)據(jù)庫或者關(guān)閉服務(wù)器。
2.local
這個數(shù)據(jù)庫永遠不會被復(fù)制,可以用來存儲限于本地單臺服務(wù)器的任意集合。
3.config
當Mongo用于分片設(shè)置時,config數(shù)據(jù)庫在內(nèi)部使用,用于保存分片的相關(guān)信息。
五、數(shù)據(jù)庫操作
1.庫操作
1.1?查看數(shù)據(jù)庫
show databases
show dbs
1.2?顯示當前所在數(shù)據(jù)庫
db
1.3?切換數(shù)據(jù)庫
use ?dbName
1.4?數(shù)據(jù)庫創(chuàng)建
隱式創(chuàng)建
?? ?????????不用主動創(chuàng)建,使用use newDB,會自動創(chuàng)建不存在的數(shù)據(jù)庫
?????????? ?只有在庫中創(chuàng)建集合后才會保存,并使用show dbs查看到
1.5?刪除數(shù)據(jù)庫
use dbName
db.dropdatabase()
2.文檔操作
2.1?查看集合
show tables
2.2?創(chuàng)建集合
db.createCollection("test")
2.3?刪除集合
db.需要刪除的集合名稱.drop()
2.4?插入數(shù)據(jù)(文檔)
單行
? ? ? ? ? ? ? ? ? ?db.集合名稱.insert({key:value})
多行
? ?????????????????db.集合名稱.insertmany([{key:value},{key:value},{key:value}])????????
注:mongodb默認數(shù)字的數(shù)據(jù)類型float浮點型,若要改變?yōu)檎?#xff1a;NumberInt(數(shù)字)
2.5?查詢數(shù)據(jù)
全集合查詢
?? ????????????????db.集合名稱.find({})
?? ????????????????db.集合名稱.find()
條件查詢
?? ????????????????db.info.find({查詢條件1key:value,查詢條件2,...},{key1:1|0,key2:1|0,...})
?? ????????????????key:1 顯示,key:0 不顯示
???????????????????當顯示的key只有一個時,key:1 只顯示該key及對應(yīng)value,key:0 顯示除了該key之外其他所有keyvalue
?
查詢集合中有多少文檔
?? ?????????????????db.集合名稱.count()
2.6?刪除數(shù)據(jù)
全文刪除:db.集合名稱.remove({})
條件刪除:db.集合名稱.remove({key:value})
?
2.7?更新數(shù)據(jù)
db.test.update({''name'':"li"}, {$set:{''gender'':"helicopter"}})
所有滿足條件的文檔都進行更新:
?? ?????????????????db.info.update({userid:"1003"},{$set:{nickname:"tom"}},{multi:true})
更新文檔進行數(shù)值更替:
?? ?????????????????db.info.update({userid:"1003"},{$inc:{likenum:NumberInt(1000)}})
六、MongoDB數(shù)據(jù)庫備份
1.備份命令
1.1?mongodump
選項:
-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
1.2?mongoexport
選項:
-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
2.恢復(fù)命令
2.1?mongorestore
選項:
-h=hostname:port
-u=<username>
-p=<password>
--authenticationDatabase=<dbname>
-d=<database>
--drop 當目標數(shù)據(jù)庫中存在同名集合則刪除再恢復(fù)
語法:
mongorestore -h hostName -u username -p password -d dbName -c tableName bakcupDirector/bsonFile
2.2?mongoimport
選項:
-h=hostname:port
-u=<username>
-p=<password>
--authenticationDatabase=<dbname>
-d=<database>
--drop 當目標數(shù)據(jù)庫中存在同名集合則刪除再恢復(fù)
語法:
mongoimport -d dbName -c tableName -h hostName:port -u userName -p Password ?backupDirectory/jsonFile.json