早就久仰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]$