mongoDB初探第一篇(r4筆記第75天)
早就久仰mongoDB大名,一直沒有決心開始學習,從昨天開始嘗試了一把,發現真是輕巧,熟悉了oracle之後,去看mysql,發現mysql真是夠輕量級的,結果再看mongoDB,更加感覺輕量級。 一般資料庫的安裝都是複雜,繁瑣,都是需要一些配置的,有些還要一些第三方軟體依賴等等。 學習mongoDB感覺真是太輕巧了。感覺就是一個解壓這麼簡單。 雖然簡單但是過程總是艱辛的,也碰到一些大大小小的問題,有些問題讓人丈二和尚抓不著頭腦,慶幸的是在今天都解決了大部分,在此分享。 安裝mongoDB,可以通過官方網站下載。 -->下載安裝包 我在linux環境中直接下載即可。官方連結是http://www.mongodb.org/downloads 不過下載的時候碰到了一些問題,貌似是https的原因導致的,使用了選項--no-check-certificate也還是不行
[mongodb@jeanron1005 ~]$ wget https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.6.1.tgz --2015-03-14 06:31:01-- https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.6.1.tgz Resolving fastdl.mongodb.org... 54.230.158.161, 54.230.159.47, 54.230.159.38, ... Connecting to fastdl.mongodb.org|54.230.158.161|:443... failed: Connection refused. Connecting to fastdl.mongodb.org|54.230.159.47|:443... connected. OpenSSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure Unable to establish SSL connection. [mongodb@rac1 ~]$ wget --no-check-certificate https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.6.1.tgz --2015-03-14 06:32:51-- https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.6.1.tgz Resolving fastdl.mongodb.org... 54.192.156.61, 54.230.159.80, 54.230.159.35, ... Connecting to fastdl.mongodb.org|54.192.156.61|:443... connected. OpenSSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure --最後使用curl終於可以了 [mongodb@rac1 ~]$ curl -O -L https://fastdl.mongodb.org/linux/mongodb-linux-i686-2.6.1.tgz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 11 113M 11 5695k 0 0 64416 0 0:12:58 0:01:30 0:11:28 97640
-->mongoDB的目錄結構
[mongodb@jeanron1005 ~]$ ll total 111324 drwxr-xr-x 3 mongodb dba 4096 Mar 15 06:52 mongodb-linux-i686-2.6.1 -rw-r--r-- 1 mongodb dba 113869484 Mar 15 06:52 mongodb-linux-i686-2.6.1.tgz mongoDB的目錄結構很簡單,只有一個bin目錄,裡面只有一些可執行檔案。 [mongodb@jeanron1005 mongodb-linux-i686-2.6.1]$ ll total 64 drwxr-xr-x 2 mongodb dba 4096 Mar 15 06:52 bin -rw-r--r-- 1 mongodb dba 34520 May 5 2014 GNU-AGPL-3.0 -rw-r--r-- 1 mongodb dba 1359 May 5 2014 README -rw-r--r-- 1 mongodb dba 17793 May 5 2014 THIRD-PARTY-NOTICES [mongodb@jeanron1005 bin]$ ll total 210616 -rwxr-xr-x 1 mongodb dba 23081996 May 5 2014 bsondump -rwxr-xr-x 1 mongodb dba 23150252 May 5 2014 mongodump -rwxr-xr-x 1 mongodb dba 23096652 May 5 2014 mongoexport -rwxr-xr-x 1 mongodb dba 23143548 May 5 2014 mongofiles -rwxr-xr-x 1 mongodb dba 23119148 May 5 2014 mongoimport -rwxr-xr-x 1 mongodb dba 23088364 May 5 2014 mongooplog -rwxr-xr-x 1 mongodb dba 7283200 Mar 15 06:52 mongoperf -rwxr-xr-x 1 mongodb dba 23188140 May 5 2014 mongorestore -rwxr-xr-x 1 mongodb dba 23139404 May 5 2014 mongostat -rwxr-xr-x 1 mongodb dba 23085228 May 5 2014 mongotop
其實如果自己學習需要,就不用額外的配置了.
-->啟動mongoDB服務
啟動mongoDB的方式很簡單,可以通過mongod --help來得到
可以通過如下的命令,--dbpath是對應的目錄,logpath是對應的目錄日誌 --fork是在後臺啟動,讓人鬱悶的是,啟動到最後的時候報錯退出了。
[mongodb@jeanron1005 bin]$ ./mongod --dbpath=/home/mongodb/data --fork --logpath=/home/mongodb/logs
2015-03-15T15:57:51.502+0800
2015-03-15T15:57:51.504+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
2015-03-15T15:57:51.505+0800
about to fork child process, waiting until server is ready for connections.
forked process: 6926
ERROR: child process failed, exited with error number 1
查看了各種資料無果,最後在windows上安裝了一遍,採用同樣的方式,終於發現是日誌的設定有問題,--logpath需要設定日誌的檔案而不是目錄
修改為目錄之後,就沒有問題了。
[mongodb@jeanron1005 bin]$ ./mongod --dbpath=/home/mongodb/data --fork --logpath=/home/mongodb/log
2015-03-15T15:58:42.557+0800
2015-03-15T15:58:42.559+0800 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
2015-03-15T15:58:42.559+0800
about to fork child process, waiting until server is ready for connections.
forked process: 6931
child process started successfully, parent exiting
mongoDB會相應的啟動多個子程序,可以通過pstree看到基本的結構。
[mongodb@jeanron1005 bin]$ pstree -p|grep mongod
|-mongod(6931)-+-{mongod}(6932)
| |-{mongod}(6933)
| |-{mongod}(6934)
| |-{mongod}(6935)
| |-{mongod}(6936)
| |-{mongod}(6937)
| |-{mongod}(6938)
| |-{mongod}(6939)
| `-{mongod}(6940)
那麼6931對應的程序是多少呢?
[mongodb@jeanron1005 bin]$ ps -ef|grep 6931
mongodb 6931 1 0 15:58 ? 00:00:00 ./mongod --dbpath=/home/mongodb/data --fork --logpath=/home/mongodb/log
mongodb 6977 6307 0 16:00 pts/2 00:00:00 grep 6931
-->連線mongoDB
連線mongoDB可以直接通過執行mongo來執行
[mongodb@jeanron1005 bin]$ ./mongo
MongoDB shell version: 2.6.1
connecting to: test
Server has startup warnings:
2015-03-15T15:58:42.605+0800 [initandlisten]
2015-03-15T15:58:42.605+0800 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2015-03-15T15:58:42.605+0800 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2015-03-15T15:58:42.605+0800 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
2015-03-15T15:58:42.606+0800 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2015-03-15T15:58:42.606+0800 [initandlisten]
>
來檢視一下有哪些資料庫,可以使用show dbs
連線哪個資料庫,使用use,和mysql基本很相似。
> show dbsshow dbs
admin (empty)
local 0.078GB
> use admin
switched to db admin
如果不確定有哪些命令可用,可以使用help
> help
db.help() help on db methods
....
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
-->關閉mongoDB
關於關閉mongoDB可以通過如下的方式來實現, 一種是通過pkill mongod,如pkill mongod 一種是呼叫mongoDB對應的api 連入admin庫,使用db.shutdownServer()
> db.shutdownServer()db.shutdownServer()
2015-03-15T16:04:33.678+0800 DBClientCursor::init call() failed
server should be down...
2015-03-15T16:04:33.690+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2015-03-15T16:04:33.691+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-03-15T16:04:33.692+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
>
[mongodb@jeanron1005 bin]$ pstree -p|grep mongod
[mongodb@jeanron1005 bin]$