-->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]$