CentOS 6.3下快速安裝MongoDB 3.2.1
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: 下載安裝文件
下載地址: https://www.mongodb.org/downloads.
或者直接在命令提示符下使用curl命令下載
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.1.tgz
Step 3: 解壓下載的文件
# pwd
/home/mongo
# tar -xvf mongodb-linux-x86_64-rhel62-3.2.1.gz ###注,本文直接從網站下載,所以文件為.gz
-
[mongo@mysql ~]$ tar -xvf mongodb-linux-x86_64-3.2.1.tgz
-
mongodb-linux-x86_64-3.2.1/README
-
tar: mongodb-linux-x86_64-3.2.1/README: time stamp 2016-01-12 02:37:17 is 48732.469243332 s in the future
-
mongodb-linux-x86_64-3.2.1/THIRD-PARTY-NOTICES
-
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
-
mongodb-linux-x86_64-3.2.1/MPL-2
-
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
-
mongodb-linux-x86_64-3.2.1/GNU-AGPL-3.0
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongodump
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongorestore
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongoexport
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongoimport
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongostat
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongotop
-
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
-
mongodb-linux-x86_64-3.2.1/bin/bsondump
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongofiles
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongooplog
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongoperf
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongod
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongos
-
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
-
mongodb-linux-x86_64-3.2.1/bin/mongo
-
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