MongoDB的安裝,mongod和mongo的區別


 

一. mongoDB安裝路徑

安裝路徑(最新4.0.11):https://www.mongodb.com/download-center/community?jmp=nav

建議另外找路徑下載,外網太慢,等不住。。

這是一位博主提供的下載路徑(4.0.10):

百度鏈接:https://pan.baidu.com/s/1xhFsENTVvU-tnjK9ODJ7Ag 密碼:ctyy

 

 

二. 安裝步驟

正常的安裝步驟

1. 勾選協議,然后Next

 

2. 選擇"Custom"自定義安裝,不要選擇“Complete”

 

3. 安裝在自己選擇的(Browse...)文件夾下

 

4. 下一步安裝 "install mongoDB compass" 不勾選,否則可能要很長時間都一直在執行安裝,MongoDB Compass 是一個圖形界面管理工具,我們可以在后面自己到官網下載安裝

 

 

 5. 點擊 “Install” 安裝即可

 

三 安裝完成后

1. 安裝完成后的目錄結構

2. 安裝完成的驗證

>mongo -version

查看當前mongo的版本,

可以在mongoDB/bin目錄下打開執行命令

也可配置環境變量:即配置bin文件夾的路徑

 

3. 啟動mongodb服務

>mongod -dbpath "D:\mongodatabase\data"

mongod 命令 是啟用數據庫服務,即搭建並開啟服務器,可以通過端口被訪問(27017)

表示在27017端口啟動成功,瀏覽器打開 http://localhost:27017/ 顯示如下

注意:

mongod --dbpath 命令是創建數據庫文件的存放位置,啟動mongodb服務時需要先確定數據庫文件存放的位置,否則系統不會自動創建,啟動會不成功。

mongod --logpath 表示日志文件存放的路徑     --logappend  表示以追加的方式寫日志文件

 

在 " bin/mongod.cfg " 文件中也會有 dbPath 和 logPath的配置

另外:

在 bin目錄下的 mongod.exe文件,雙擊會閃出,就是因為沒有執行上面的 mongod --dbpath 命令

在 bin目錄下的 mongo.exe文件,雙擊來打開 MongoDB 客戶端,進行數據庫操作

 

4. mongo的操作

mongo 命令 是連接數據庫服務,即連接服務器,可以通過端口進行訪問(27017)

我們的連接:

connecting to: mongodb://127.0.0.1:27017

 

5. 操作數據庫

到這一步數據庫已經成功跑起來了,接下來就是操作一些命令向數據庫里面插入數據等並且可以看到自己對數據庫的一系列操作的結果了

db.stats() 命令將顯示數據庫名稱,數據庫中的集合和文檔數量

> db.stats()
{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "fsUsedSize" : 0,
        "fsTotalSize" : 0,
        "ok" : 1
}

 

db.help()將列出一個命令列表

> db.help()
DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost) - deprecated
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost) - deprecated
        db.createCollection(name, {size: ..., capped: ..., max: ...})
        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
        db.createUser(userDocument)
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase()
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getLogComponents()
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printShardingStatus()
        db.printSlaveReplicationInfo()
        db.dropUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into {cmdObj: 1}
        db.serverStatus()
        db.setLogLevel(level,<component>)
        db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
        db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
        db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
>

 

 

四 最后

  1. 注意 mongod 和 mongo 的區別

  前者是啟用MongoDB進程,后者是對MongoDB進行連接操作

  2. 執行 mongod 需要 配置路徑 進行啟用,單純的雙擊 mongod.exe文件會閃出,正確的是執行mongod --dbpath 命令

  3. 在執行mongod命令啟用MongoDB進程(服務器)的基礎上,再使用mongo 對其進行連接操作,

  

五,幾個概念:

來源 MongoDB面試題問題以及參考答案

8.什么是”mongod“
  mongod是處理MongoDB系統的主要進程。它處理數據請求,管理數據存儲,和執行后台管理操作。當我們運行mongod命令意味着正在啟動MongoDB進程,並且在后台運行。

9."mongod"參數有什么
  傳遞數據庫存儲路徑,默認是"/data/db"
  端口號 默認是 "27017"
10.什么是"mongo"
  它是一個命令行工具用於連接一個特定的mongod實例。當我們沒有帶參數運行mongo命令它將使用默認的端口號和localhost連接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM