1.下载mongodb
下载地址为 http://www.mongodb.org/downloads ,根据OS选择安装的版本.
我这里下载的是mongodb-win32-i386-2.0.8.zip
下载地址为 http://www.mongodb.org/downloads ,根据OS选择安装的版本.
我这里下载的是mongodb-win32-i386-2.0.8.zip
2.解压缩mongodb-win32-i386-2.0.8.zip
解压缩该zip包,我这里解压缩到E:\soft\mongodb-win32-i386-2.0.8,同时在该目录下创建db和log两个文件夹,这样的话该目录有如下三个文件夹.
E:\soft\mongodb-win32-i386-2.0.8\bin
E:\soft\mongodb-win32-i386-2.0.8\db
E:\soft\mongodb-win32-i386-2.0.8\log
解压缩该zip包,我这里解压缩到E:\soft\mongodb-win32-i386-2.0.8,同时在该目录下创建db和log两个文件夹,这样的话该目录有如下三个文件夹.
E:\soft\mongodb-win32-i386-2.0.8\bin
E:\soft\mongodb-win32-i386-2.0.8\db
E:\soft\mongodb-win32-i386-2.0.8\log
在log文件夹下创建日志文件MongoDB.log,完全目录为E:\soft\mongodb-win32-i386-2.0.8\log\MongoDB.log
3.在PATH变量中加入E:\soft\mongodb-win32-i386-2.0.8\bin,这样就可以直接调用bin目录下的exe文件.
4.执行如下命令
mongod -dbpath "E:\soft\mongodb-win32-i386-2.0.8\db"
执行此命令即将mongodb的数据库文件创建到E:\soft\mongodb-win32-i386-2.0.8\db目录
Sun Dec 02 10:09:18 [clientcursormon] mem (MB) res:0 virt:44 mapped:
mongod -dbpath "E:\soft\mongodb-win32-i386-2.0.8\db"
执行此命令即将mongodb的数据库文件创建到E:\soft\mongodb-win32-i386-2.0.8\db目录
Sun Dec 02 10:09:18 [clientcursormon] mem (MB) res:0 virt:44 mapped:
5.运行mongo.exe
C:\Documents and Settings\xueliang.huang>mongo
MongoDB shell version: 2.0.8
connecting to: test
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
C:\Documents and Settings\xueliang.huang>mongo
MongoDB shell version: 2.0.8
connecting to: test
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods
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 <db_name> 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
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 <db_name> 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
当mongod.exe被关闭时,mongo.exe 就无法连接到数据库了,因此每次想使用mongodb数据库都要开启mongod.exe程序,所以比较麻烦,接下来我们将接下来我们将
MongoDB安装为windows服务吧
MongoDB安装为windows服务吧
6.MongoDB安装为windows服务
创建服务
mongod --dbpath "E:\soft\mongodb-win32-i386-2.0.8\db" --logpath "E:\soft\mongodb-win32-i386-2.0.8\log\MongoDB.log" --install --serviceName "mongodb"
net start mongodb(开启服务)
net stop mongodb(关闭服务)
创建服务
mongod --dbpath "E:\soft\mongodb-win32-i386-2.0.8\db" --logpath "E:\soft\mongodb-win32-i386-2.0.8\log\MongoDB.log" --install --serviceName "mongodb"
net start mongodb(开启服务)
net stop mongodb(关闭服务)
删除服务
mongod --dbpath "E:\soft\mongodb-win32-i386-2.0.8\db" --logpath "E:\soft\mongodb-win32-i386-2.0.8\log\MongoDB.log" --remove --serviceName "MongoDB"
mongod --dbpath "c:\mymongodb" --logpath "c:\mymongodb\logs.txt" --install --serviceName "MongoDB"
mongod --dbpath "E:\soft\mongodb-win32-i386-2.0.8\db" --logpath "E:\soft\mongodb-win32-i386-2.0.8\log\MongoDB.log" --remove --serviceName "MongoDB"
mongod --dbpath "c:\mymongodb" --logpath "c:\mymongodb\logs.txt" --install --serviceName "MongoDB"
要是出现如下错误的话需要将db目录下的mongod.lock删除后再启动
>mongo
>mongo
>couldn't connect to server 127.0.0.1 shell/mongo.js:84
---------------------------/******分割线******--------------------------------------
创建windows服务,就是以最好记住的命令net start mongodb 来代替启动并指定db文件路径的复杂命令;使用mongod时,只需两步:
1-net start mongodb(服务名)
2-mongo /前提是保证已经将mongo.exe的bin路径加入到windows 的path环境变量中