新聞中心
MongoDB簡介
1)Mongodb屬于非關(guān)系性數(shù)據(jù)庫 ,數(shù)據(jù)記錄以文檔形式(鍵值對)進行存儲,即bson格式
2)不再有“行”(row)的概念,其運行方式主要基于兩個概念:集合(collection)與文檔(document)
3)支持各種編程語言:Ruby,Python,Java,C++,PHP,C#等多種語言
邏輯結(jié)構(gòu)對比
關(guān)系型:數(shù)據(jù)庫------》表------------》記錄,字段
非關(guān)系型:數(shù)據(jù)庫-----------》集合----------》鍵值對
何為鍵值對
書寫格式:{鍵值:值}
鍵值對:如{“name”:”zhangsan”} 鍵值必須雙引號,值如果是數(shù)值(int)可以不用雙引號引起來,如果是字符串(string)必須用雙引號引起來。
本次實驗在CentOS7系統(tǒng)上進行實施,首先配置網(wǎng)絡(luò)YUM源,baseurl(下載路徑)指定為mongodb官網(wǎng)提供的yum倉庫
vim /etc/yum.repos.d/mongodb.repo
成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)公司、微信開發(fā)、微信小程序開發(fā)、集團成都企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:格柵板等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致好評!
[mongodb-org]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ #指定獲得下載的路徑
gpgcheck=1 #表示對從這個源下載的rpm包進行校驗
enabled=1 #表示啟用這個源。
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
重新加載yum源,并使用yum命令下載安裝mongodb
yum list
yum -y install mongodb-org
等待下載好之后,修改配置文件,指定監(jiān)聽IP,端口默認(rèn)為27017
vim /etc/mongod.conf
..........
bindIp:0.0.0.0 #監(jiān)聽任意地址
port:27017 #默認(rèn)監(jiān)聽端口
啟動mongodb服務(wù),并進入mongodb。
mongodb服務(wù)的啟動,關(guān)閉有兩種方式
(1)systemctl start mongod.service #啟動
systemctl stop mongod.service #關(guān)閉
(2) mongod -f /etc/mongod.conf #啟動
mongod -f /etc/mongod.conf --shutdown #關(guān)閉
開啟服務(wù)后,查看mongodb進程
netstat -antp | grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 16540/mongod
下面主要講解Mongodb的基本操作及語句應(yīng)用,mongodb十分人性化自帶Tab鍵補全功能
不需要使用密碼直接使用mongo命令進入服務(wù)
查看版本信息
> db.version()
3.6.7
查看數(shù)據(jù)庫
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
myschool 0.000GB
school 0.000GB
進入數(shù)據(jù)庫(如果創(chuàng)建集合則自動創(chuàng)建數(shù)據(jù)庫,如果沒有創(chuàng)建集合則數(shù)據(jù)庫沒有被創(chuàng)建),
> use yun
switched to db yun
創(chuàng)建集合;插入數(shù)據(jù)信息,并同時創(chuàng)建集合info
> db.createCollection("abc")
{ "ok" : 1 }
> db.info.insert({"id":1,"name":"jack1"})
WriteResult({ "nInserted" : 1 })
查看集合
> show tables
info
> show collections
info
使用循環(huán)批量添加用戶
> for (var i=2;i<=100;i++)db.abc.insert({"id":i,"name":"jack"+i});
WriteResult({ "nInserted" : 1 })
查看集合中的所有數(shù)據(jù)
> db.abc.find()
查看單條數(shù)據(jù)
db.abc.findOne({"id":10})
{
"_id" : ObjectId("5b972d38fb89e57a63998a84"),
"id" : 10,
"name" : "jack10"
> a=db.abc.findOne({"id":10}) #把這條記錄定義別名為a
{
"_id" : ObjectId("5b9a6f39e80a2611eecb6f7b"),
"id" : 10,
"name" : "jack10"
查看類型
> typeof(a.id)
number
> typeof(a.name)
String
修改數(shù)據(jù):
db.info.update({"id":10},{$set:{"name":"tom10"}}) 格式:條件在前,修改在后
db.info.findOne({"id":10})
{
"_id" : ObjectId("5b972d38fb89e57a63998a84"),
"id" : 10,
"name" : "tom10"
聚合函數(shù)統(tǒng)計記錄
db.info.count()
100
刪除集合,數(shù)據(jù)
db.info.remove({"id":12}) //刪除數(shù)據(jù)
Db.info.drop() //刪除集合
先進入該數(shù)據(jù)庫,再用下面的命令刪除數(shù)據(jù)庫
Use school
db.dropDatebase() //刪除數(shù)據(jù)庫
數(shù)據(jù)跨實例克隆集合:
db.runCommand({"clonecollection":"school.info","from":"192.168.195.137:27017"})
數(shù)據(jù)的備份與恢復(fù),導(dǎo)入與導(dǎo)出,是在linux的shell環(huán)境進行操作
數(shù)據(jù)導(dǎo)出
mongoexport -d school -c info -o /opt/school.jason
條件導(dǎo)出 -q '{"id":{"$eq":10}}'
mongoexport -d school -c info -q '{"id":{"$eq":10}}' -o /opt/school10.jason
數(shù)據(jù)導(dǎo)入
mongoimport -d school -c infos --file /opt/school.jason
數(shù)據(jù)庫備份與恢復(fù),都不需要額外創(chuàng)建數(shù)據(jù)庫和目錄
數(shù)據(jù)庫備份
mongodump -d yunwei(庫名) -o /opt(路徑)
數(shù)據(jù)庫恢復(fù)
mongorestore -d yunjisuan2 --dir=/opt/yunjisuan
授權(quán)啟動
在mongodb中進行授權(quán)
> use admin
switched to db admin
> db.createUser({"user":"root","pwd":"abc123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
退出mongodb,修改配置文件 指定auth=true,否則授權(quán)不起作用
vim /etc/mongod.conf
auth=true
重啟服務(wù),再進入mongodb,只有通過授權(quán)驗證才可以查看數(shù)據(jù)信息
vim mongodb.conf
> use admin
switched to db admin
> db.auth("root","abc123")
1
> show dbs
admin 0.078GB
local 0.078GB
當(dāng)前名稱:MongoDB在線yum源安裝及基本命令詳解
網(wǎng)頁地址:http://fisionsoft.com.cn/article/pdssge.html