mongodb 安装与简单使用

本文详细介绍了在Windows环境下安装MongoDB的过程,并提供了简单的使用指南,包括如何配置环境变量、添加服务、启动及停止服务,以及基本的命令行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows下安装mongodb和简单使用mongodb命令
一。下载,解压文件
到官方下载合适的版本 http://www.mongodb.org/downloads
例如:http://fastdl.mongodb.org/win32/mongodb-win32-i386-1.6.5.zip
解压后放到对应的盘符下面,例如:G:\mongodb165
二。安装
1.把bin目录加入到环境变量中 G:\mongodb165\bin
2.在G:\mongodb165下创建data文件夹放数据用
3.简单启动mongodb方法:
进入到bin目录下
C:\Documents and Settings\zheng>G:
G:\>cd mongodb165/bin
G:\mongodb165\bin>mongod --dbpath G:/mongodb165/data
G:\mongodb165\bin>mongod --dbpath G:/mongodb165/data
Sun Jan 16 14:56:03 MongoDB starting : pid=860 port=27017 dbpath=G:/mongodb165/d
ata 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data

**       see http://blog.mongodb.org/post/137788967/32-bit-limitations

Sun Jan 16 14:56:03 db version v1.6.5, pdfile version 4.5
Sun Jan 16 14:56:03 git version: 0eb017e9b2828155a67c5612183337b89e12e291
Sun Jan 16 14:56:03 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_LI
B_VERSION=1_35
Sun Jan 16 14:56:03 [initandlisten] waiting for connections on port 27017
Sun Jan 16 14:56:03 [websvr] web admin interface listening on port 28017

注:必须先建立data文件夹和到bin目录下执行。mongoDB 服务端的默认连接端口是 27017

2.添加到注册表作为Windows服务启动,和mysql一样启动Windows时会自动启动服务,到bin目录下执行
G:\mongodb165\bin>mongod --logpath G:\mongodb165\logs\mongodb165.log --logappend
 --dbpath G:\mongodb165\data --directoryperdb --serviceName mongodb165 --install

完成后输出下面内容(360等杀毒软件会阻止,需要允许通过)
all output going to: G:\mongodb165\logs\mongodb165.log
Creating service mongodb165.
Service creation successful.
Service can be started from the command line via 'net start "mongodb165"'.

G:\mongodb165\bin>

其中:logs\mongodb165.log日志是以追加的方式输出的,--serviceName mongodb165是服务名称
启动MongoDB:net start mongodb165
停止MongoDB:net stop mongodb165
注:添加到注册表后重启电脑在服务项里面可以看到已经启动,但服务仍然没有启动,重启服务发现给360安全卫士阻止了需要再次确定才能启动。

3.mongodb的简单使用命令,到bin命令下执行mongo.exe进入管理界面,默认是进入到test帐号。
G:\mongodb165\bin>mongo.exe
MongoDB shell version: 1.6.5
connecting to: test
> show dbs;
admin
local
> help;查看命令提示
        db.help()                    help on db methods
        db.mycoll.help()             help on collection methods
        rs.help()                    help on replica set methods
        help connect                 connecting to a db help
        help admin                   administrative help
        help misc                    misc things to know

        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 wit
h time >= 1ms
        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 f
urther iterate
        exit                         quit the mongo shell
> use testdb;切换到testdb数据库,如果不存在则在插入数据后会自动创建一个,在data目录下可以看到新增了一个testdb的文件夹
switched to db testdb
> db.myc.save({a:10});向collection mpc 中保存一条信息,如果不存在collection会自动创建一个
> db.myc.find();检索所有记录
{ "_id" : ObjectId("4d32c9204e6100000000691e"), "a" : 10 }
> show collections;
myc
system.indexes
>exit;退出

转载于:https://my.oschina.net/ellan/blog/29077

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值