CentOS 6.3下快速安裝MongoDB 3.2.1

luashin發表於2016-03-26
MongoDB是一個基於分散式檔案儲存的資料庫。由C++語言編寫。旨在為WEB應用提供可擴充套件的高效能資料儲存解決方案。
是一個介於關係資料 庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料庫的。他支援的資料結構非常鬆散,是類似json的bson格式,因此可以儲存 比較複雜的資料型別。Mongo最大的特點是他支援的查詢語言非常強大,其語法有點類似於物件導向的查詢語言,幾乎可以實現類似關聯式資料庫單表查詢的絕大 部分功能,而且還支援對資料建立索引。

MongoDB 是目前在IT行業非常流行的一種非關係型資料庫(NoSql),其靈活的資料儲存方式備受當前IT從業人員的青睞。MongoDB很好的實現了物件導向的 思想(OO思想),在MongoDB中 每一條記錄都是一個Document物件。MongoDB最大的優勢在於所有的資料持久操作都無需開發人員手動編寫SQL語句,直接呼叫方法就可以輕鬆的 實現CRUD操作。本文介紹瞭如何快速安裝mongodb供大家參考。

一、安裝配置mongodb
useradd mongodb

Step 1: 設定系統環境及確保預設埠27107可用
###當前環境
cat /etc/issue

# vi /etc/selinux/config
SELINUX=disabled

Step 2: 下載安裝檔案
下載地址:
或者直接在命令提示符下使用curl命令下載
curl -O

Step 3: 解壓下載的檔案
# pwd
/home/mongo
# tar -xvf mongodb-linux-x86_64-rhel62-3.2.1.gz  ###注,本文直接從網站下載,所以檔案為.gz

點選(此處)摺疊或開啟

  1. [mongo@mysql ~]$ tar -xvf mongodb-linux-x86_64-3.2.1.tgz
  2. mongodb-linux-x86_64-3.2.1/README
  3. tar: mongodb-linux-x86_64-3.2.1/README: time stamp 2016-01-12 02:37:17 is 48732.469243332 s in the future
  4. mongodb-linux-x86_64-3.2.1/THIRD-PARTY-NOTICES
  5. tar: mongodb-linux-x86_64-3.2.1/THIRD-PARTY-NOTICES: time stamp 2016-01-12 02:37:17 is 48732.468806195 s in the future
  6. mongodb-linux-x86_64-3.2.1/MPL-2
  7. tar: mongodb-linux-x86_64-3.2.1/MPL-2: time stamp 2016-01-12 02:37:17 is 48732.468729339 s in the future
  8. mongodb-linux-x86_64-3.2.1/GNU-AGPL-3.0
  9. tar: mongodb-linux-x86_64-3.2.1/GNU-AGPL-3.0: time stamp 2016-01-12 02:37:17 is 48732.468416571 s in the future
  10. mongodb-linux-x86_64-3.2.1/bin/mongodump
  11. tar: mongodb-linux-x86_64-3.2.1/bin/mongodump: time stamp 2016-01-12 02:37:30 is 48745.395380045 s in the future
  12. mongodb-linux-x86_64-3.2.1/bin/mongorestore
  13. tar: mongodb-linux-x86_64-3.2.1/bin/mongorestore: time stamp 2016-01-12 02:37:28 is 48743.308178108 s in the future
  14. mongodb-linux-x86_64-3.2.1/bin/mongoexport
  15. tar: mongodb-linux-x86_64-3.2.1/bin/mongoexport: time stamp 2016-01-12 02:37:25 is 48740.256976344 s in the future
  16. mongodb-linux-x86_64-3.2.1/bin/mongoimport
  17. tar: mongodb-linux-x86_64-3.2.1/bin/mongoimport: time stamp 2016-01-12 02:37:26 is 48741.205864231 s in the future
  18. mongodb-linux-x86_64-3.2.1/bin/mongostat
  19. tar: mongodb-linux-x86_64-3.2.1/bin/mongostat: time stamp 2016-01-12 02:37:23 is 48738.158042438 s in the future
  20. mongodb-linux-x86_64-3.2.1/bin/mongotop
  21. tar: mongodb-linux-x86_64-3.2.1/bin/mongotop: time stamp 2016-01-12 02:37:31 is 48746.110027909 s in the future
  22. mongodb-linux-x86_64-3.2.1/bin/bsondump
  23. tar: mongodb-linux-x86_64-3.2.1/bin/bsondump: time stamp 2016-01-12 02:37:21 is 48736.074523763 s in the future
  24. mongodb-linux-x86_64-3.2.1/bin/mongofiles
  25. tar: mongodb-linux-x86_64-3.2.1/bin/mongofiles: time stamp 2016-01-12 02:37:24 is 48739.014639938 s in the future
  26. mongodb-linux-x86_64-3.2.1/bin/mongooplog
  27. tar: mongodb-linux-x86_64-3.2.1/bin/mongooplog: time stamp 2016-01-12 02:37:33 is 48747.960322499 s in the future
  28. mongodb-linux-x86_64-3.2.1/bin/mongoperf
  29. tar: mongodb-linux-x86_64-3.2.1/bin/mongoperf: time stamp 2016-01-12 02:49:50 is 49484.63946251 s in the future
  30. mongodb-linux-x86_64-3.2.1/bin/mongod
  31. tar: mongodb-linux-x86_64-3.2.1/bin/mongod: time stamp 2016-01-12 02:49:50 is 49484.277970529 s in the future
  32. mongodb-linux-x86_64-3.2.1/bin/mongos
  33. tar: mongodb-linux-x86_64-3.2.1/bin/mongos: time stamp 2016-01-12 02:49:49 is 49483.120921568 s in the future
  34. mongodb-linux-x86_64-3.2.1/bin/mongo
  35. tar: mongodb-linux-x86_64-3.2.1/bin/mongo: time stamp 2016-01-12 02:49:50 is 49483.896681132 s in the future


Step 4: 複製解壓檔案到執行目錄
[mongo@mysql ~]$ pwd
/home/mongo
[mongo@mysql ~]$ mv mongodb-linux-x86_64-3.2.1 mongo
[mongo@mysql ~]$ ll
total 55108
drwxrwxr-x. 3 mongo mongo     4096 Jan 11 13:05 mongo
-rw-r-----. 1 mongo mongo 56419937 Jan 21  2016 mongodb-linux-x86_64-3.2.1.tgz

[mongo@mysql ~]$ 
Step 5: 設定環境變數
[mongo@mysql ~]$ vi .bash_profile 
PATH=$PATH:$HOME/bin:/home/mongo/mongo/bin
export PATH

Step 6: 建立資料目錄
# mkdir -p /data/mongodata

二、啟動及驗證mongodb
###啟動mongo
# mongod --dbpath /home/mongo/mongo/data


###以下內容為啟動後輸出的相關資訊
[mongo@mysql ~]$ mongod -dbpath /home/mongo/mongo/data/
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] MongoDB starting : pid=12579 port=27017 dbpath=/home/mongo/mongo/data/ 64-bit host=mysql
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] db version v3.2.1
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] git version: a14d55980c2cdc565d4704a7e3ad37e4e535c1b2
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] modules: none
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] build environment:
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten]     distarch: x86_64
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2016-01-11T13:08:33.684+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/home/mongo/mongo/data/" } }
2016-01-11T13:08:33.725+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-01-11T13:08:33.828+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/mongo/mongo/data/diagnostic.data'
2016-01-11T13:08:33.829+0800 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-01-11T13:08:33.844+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
###停止mongo,直接使用ctrl + c
f^C2016-01-11T13:09:46.734+0800 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2016-01-11T13:09:46.734+0800 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2016-01-11T13:09:46.735+0800 I CONTROL  [signalProcessingThread] now exiting
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] closing listening socket: 5
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] closing listening socket: 6
2016-01-11T13:09:46.735+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2016-01-11T13:09:46.737+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2016-01-11T13:09:46.737+0800 I NETWORK  [signalProcessingThread] shutdown: going to close sockets...
2016-01-11T13:09:46.737+0800 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2016-01-11T13:09:46.781+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2016-01-11T13:09:46.781+0800 I CONTROL  [signalProcessingThread] dbexit:  rc: 0


###修復啟動過程中的兩個警告,關於使用root使用者啟動mongo的警告先忽略
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

###再次重啟,後置於後臺程式,
# mongod --dbpath /home/mongo/mongo/data &

###檢視啟動後的程式
# ps -ef|grep mongo


###使用mongo連線到mongod
# mongo
MongoDB shell version: 3.2.1


> help
        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                 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
> db.getCollection("version");
test.version
> exit
bye

三、mongodb相關工具
###在安裝檔案下有README,描述了常用的mongodb相關命令列工具
# more /usr/local/mongodb/README
MongoDB README
Welcome to MongoDB!
COMPONENTS

  bin/mongod - The database process.
  bin/mongos - Sharding controller.
  bin/mongo  - The database shell (uses interactive javascript).

UTILITIES

  bin/mongodump        - MongoDB dump tool - for backups, snapshots, etc..
  bin/mongorestore      - MongoDB restore a dump
  bin/mongoexport      - Export a single collection to test (JSON, CSV)
  bin/mongoimport      - Import from JSON or CSV
  bin/mongofiles        - Utility for putting and getting files from MongoDB GridFS
  bin/mongostat        - Show performance statistics

RUNNING

  For command line options invoke:

    $ ./mongod --help

  To run a single server database:

    $ mkdir /data/db
    $ ./mongod
    $
    $ # The mongo javascript shell connects to localhost and test database by default:
    $ ./mongo
    > help

DRIVERS

  Client drivers for most programming languages are available at mongodb.org.  Use the
  shell ("mongo") for administrative tasks.

###獲取單個命令用法
# --help
# mongod --help|more
Options:

General options:
  -h [ --help ]              show this usage information
  --version                  show version information

# mongod --version
db version v3.2.1
git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9034054/viewspace-2064060/,如需轉載,請註明出處,否則將追究法律責任。

相關文章