Ubuntu下MongoDB的安裝和使用


本博文介紹了MongoDB,並詳細指引讀者在Ubuntu下MongoDB的安裝和使用。本教程在Ubuntu14.04下測試通過.(2017.09.07)

 

安裝MongoDB

MongoDB安裝很簡單,無需下載源文件,可以直接用apt-get命令進行安裝。
打開終端,輸入以下命令:

sudo apt-get install mongodb



安裝完成后,在終端輸入以下命令查看MongoDB版本:

mongo -version


輸出版本信息,表明安裝成功,如下:

root@ubantu:/usr/src# mongo -version
MongoDB shell version: 2.4.9



啟動和關閉mongodb命令如下:

service mongodb start
輸入命令,結果如下:
root@ubantu:/usr/src# service mongodb start
start: Job is already running: mongodb


service mongodb stop



默認設置MongoDB是隨Ubuntu啟動自動啟動的。
輸入以下命令查看是否啟動成功:

pgrep mongo -l #注意:-l是英文字母l,不是阿拉伯數字1


卸載MongoDB
sudo apt-get --purge remove mongodb mongodb-clients mongodb-server


使用MongoDB

 

shell命令模式

輸入mongo進入shell命令模式,默認連接的數據庫是test數據庫,在此之前一定要確保你已經啟動了MongoDB,否則會出現錯誤,啟動之后運行成功,如下圖:

root@ubantu:/usr/src# mongo
MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
>
常用操作命令:

    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                   administrative help
    help connect                 connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce

    show dbs                     show database names
    show collections             show collections in current database
    show users                   show users in current database
    show profile                 show most recent system.profile entries with time >= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, 'global' is default
    use <db_name>                set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )     list objects in foo where a == 1
    it                           result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x   set default number of items to display on shell
    exit                         quit the mongo shell

 


免責聲明!

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



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