新聞中心
NoSQL介紹(七)
MongoDB介紹
- 官網(wǎng)www.mongodb.com
- c++編寫,基于分布式,屬于NoSQL的一種
- 在NoSQL中是最像關(guān)系型數(shù)據(jù)庫的
- MongoDB將數(shù)據(jù)存儲(chǔ)為一種文檔,數(shù)據(jù)結(jié)構(gòu)由鍵值(key=>value)對(duì)組成。MongoDB文檔類似于JSON對(duì)象。字段值可以包含其他文檔、數(shù)組及文檔數(shù)組。
- 因?yàn)榛诜植际?,所以很容易擴(kuò)展
MongoDB和關(guān)系型數(shù)據(jù)庫的對(duì)比
SQL術(shù)語概念 | MongoDB術(shù)語概念 | 解釋說明 |
---|---|---|
database | database | 數(shù)據(jù)庫 |
table | collection | 數(shù)據(jù)庫表/集合 |
row | document | 數(shù)據(jù)記錄行/文檔 |
column | filed | 數(shù)據(jù)字段/域 |
index | index | 索引 |
table joins | 表連接MongoDB不支持 | |
primary key | primary key | 主鍵MongoDB自動(dòng)將_id字段設(shè)置為主鍵 |
MongoDB安裝
官方安裝文檔 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
vim /etc/yum.repos.d/mongodb-org-3.6.repo
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
yum list | grep mongodb
yum install -y mongodb-org
MongoDB的連接
systemctl start mongod.service
netstat -tlnp|grep mongod
tcp 0 0 192.168.221.10:27017 0.0.0.0:* LISTEN 1999/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1999/mongod
mongo --port 27017 --host 192.168.221.10
mongo -uusername -ppasswd --authenticationDatabase db
MongoDB用戶管理
mongo --port 27017 --host 192.168.221.10
> use admin //切換到admin庫
> db.createUser({user:"admin",customData:{description:"superuser"},pwd:"admin",roles:[{role:"root",db:"admin"}]});
> db.system.users.find();
> show users;
> db.createUser({user:"zs",pwd:"zs",roles:[{role:"read",db:"testdb"}]});//創(chuàng)建zs用戶
> db.dropUser('zs');//刪除用戶zs
使用用戶名,密碼連接mongo數(shù)據(jù)庫,需要修改啟動(dòng)腳本并重啟
vim /usr/lig/systemd/system/mongod.service //在"OPTIONS="后加"--auth"
Environment="OPTIONS=--auth -f /etc/mongod.conf"
systemctl daemon-reload
systemctl restart mongod.service
mongo -u 'admin' -p 'admin' --authenticationDatabase 'admin' //需要指定數(shù)據(jù)庫
在數(shù)據(jù)庫db1中創(chuàng)建用戶test1對(duì)db1庫讀寫,對(duì)db2庫只讀。
> use db1;
> db.createUser({user:"test1",pwd:"test1",roles:[{role:"readWrite",db:"db1"},{role:"read",db:"db2"}]});
ctrl+d
mongo -u 'test1' -p 'test1' --authenticationDatabase 'db1'
> use db2;
> db.auth("test1","test1");
Error: Authentication failed. //報(bào)錯(cuò),因?yàn)橛脩魌est1在db1中創(chuàng)建
MongoDB用戶角色
- Read:允許用戶讀取指定數(shù)據(jù)庫
- readWrite:允許用戶讀寫指定數(shù)據(jù)庫
- dbAdmin:允許用戶在指定數(shù)據(jù)庫中執(zhí)行管理函數(shù),如索引創(chuàng)建,刪除,查看統(tǒng)計(jì)或訪問system.profile
- userAdmin:允許用戶向system.users集合寫入,可以找指定數(shù)據(jù)庫里創(chuàng)建、刪除和管理用戶
- clusterAdmin:只在admin數(shù)據(jù)庫中可用,賦予用戶所有分片和復(fù)制集相關(guān)函數(shù)的管理權(quán)限。
- readAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀權(quán)限
- readWriteAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的讀寫權(quán)限
- userAdminAnyDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的userAdmin權(quán)限
- dbAdminDatabase:只在admin數(shù)據(jù)庫中可用,賦予用戶所有數(shù)據(jù)庫的dbAdmin權(quán)限
- root:只在admin數(shù)據(jù)中可用。超級(jí)賬號(hào),超級(jí)權(quán)限
MongoDB創(chuàng)建集合
//db.createCollection(name,options);
name就是集合的名字,iptions可選,用來配置集合的參數(shù),參數(shù)如下:
capped true/false:為true,則啟用封頂集合。封頂集合是固定大小的集合,當(dāng)它達(dá)到其大大小,會(huì)自動(dòng)覆蓋最早的條目。如果指定true,則也需要指定尺寸參數(shù)。
size (可選)指定大大小字節(jié)封頂集合。如果封頂為true,還需要指定這個(gè)字段
max 指定封頂集合允許在文件的大數(shù)量。
> db.createCollection('mycol',{capped:true,size:6142800,max:10000});
查看集合
> show tables; 或 show collections
創(chuàng)建集合Account并插入內(nèi)容
> db.Account.insert({AccountID:2,UserName:'lisi',password:'lisi'});
查看集合Account的所有內(nèi)容及條件查詢
> db.Account.find();
> db.Account.find({AccountID:1});
根據(jù)條件刪除Account集合中的一條記錄
> db.Account.remove({AccountID:1});
打印集合狀態(tài)
> db.printCollectionStats();
修改集合中的一條記錄
> db.Account.update({AccountID:2},{"$set":{age:20}});
刪除某個(gè)集合
> db.Account.drop();
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:NoSQL介紹(七)-創(chuàng)新互聯(lián)
瀏覽路徑:http://fisionsoft.com.cn/article/dcsgod.html