Ubuntu下MongoDB的安裝和使用

來源:互聯網
上載者:User

標籤:tor   博文   name   ssi   print   查看   pretty   question   start   

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

 

安裝MongoDB

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

sudo apt-get install mongodb



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

mongo -version


輸出版本資訊,表明安裝成功,如下:

[email protected]:/usr/src# mongo -version
MongoDB shell version: 2.4.9



啟動和關閉mongodb命令如下:

service mongodb start
輸入命令,結果如下:
[email protected]:/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,否則會出現錯誤,啟動之後運行成功,如:

[email protected]:/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

 

Ubuntu下MongoDB的安裝和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.