溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MongoDB在線yum源安裝及基本命令詳解

發(fā)布時間:2020-06-10 16:35:12 來源:網(wǎng)絡(luò) 閱讀:1044 作者:明月幽谷 欄目:MongoDB數(shù)據(jù)庫

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

[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,端口默認為27017

vim /etc/mongod.conf

..........

bindIp:0.0.0.0      #監(jiān)聽任意地址
port:27017         #默認監(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ù)

MongoDB在線yum源安裝及基本命令詳解

查看版本信息

> 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

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI