关于 mongodb管理与安全认证 请移步这里: Mongodb For Mac OSX && 登录验证

安装mongodb

1、 官网下载 mongodb,如果嫌慢还可以前往百度云盘

2、 如果是安装包,那么安装方式选 Custom 这样可以自定义安装,比如将路径设置在 F:\MongoDb</code> 如果是 压缩包 就不说什么了

3、 创建日志

a、 在安装目录下创建俩个文件夹 \data\db 存放 数据

b、 在 \data\log 存放 日志

c、 (可略)在 log 文件夹中再创建一个文本 MongoDb.log 即日志文件

4、 运行命令行 (CMD、Bash) 都行,以Bash为例:

$ cd /f/MongoDb/bin/
$ mongod -dbpath "/f/MongoDb/data/db" 2014-12-31T11:10:31.408+0800 [initandlisten] MongoDB starting : pid=5528 port=27
017 dbpath=f:/MongoDb/data/db 64-bit host=highsea-PC
2014-12-31T11:10:31.408+0800 [initandlisten] targetMinOS: Windows 7/Windows Serv
er 2008 R2
2014-12-31T11:10:31.408+0800 [initandlisten] db version v2.6.6
2014-12-31T11:10:31.408+0800 [initandlisten] git version: 608e8bc319627693b04cc7
da29ecc300a5f45a1f……以下省略

可以看到 mongodb 已经启动,端口号:27017

5、 测试连接 新开一个Bash窗口

$ cd /f/MongoDb/bin/&&mongo
MongoDB shell version: 2.6.6
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>_

&&符号是同时执行多个指令;退出测试请输入 Ctrl+c

> ^C
bye

6、 创建 Windows 服务

$ mongod --dbpath "/f/MongoDb/data/db" --logpath "/f/MongoDb/data/log/MongDb.log" --install --serviceName MongoDb

这时候进入log目录发现 MongDb.log 里面写了:

2014-12-31T11:45:02.232+0800 Trying to install Windows service 'MongoDb'
2014-12-31T11:45:03.077+0800 Service 'MongoDb' (MongoDB) installed with command line 'f:\MongoDb\bin\mongod.exe --dbpath f:/MongoDb/data/db --logpath f:/MongoDb/data/log/MongDb.log --service'
2014-12-31T11:45:03.077+0800 Service can be started from the command line with 'net start MongoDb'

输入指令:

$ net start MongoDb
MongoDb 服务正在启动 .
MongoDb 服务已经启动成功。

到此,windows Mongodb 服务启动成功;试试在浏览器中输入 localhost:27017 ,会显示什么?

7、 关闭服务删除服务 ,指令:

  • 关闭服务
$ net stop MongoDB
发生系统错误 109。
管道已结束。

此时有提示: 发生系统错误 109 不要紧,只要进入 \data\db\ 目录删除 mongodb.lok 文件重新启动服务即可(不行?多试几次)。

  • 删除服务
$ mongod --dbpath "/f/MongoDb/data/db" --logpath "/f/MongoDb/data/log/MongDb.log" --remove --serviceName "MongoDB"
2014-12-31T12:50:44.304+0800 log file "f:/MongoDb/data/log/MongDb.log" exists; m
oved to "f:/MongoDb/data/log/MongDb.log.2014-12-31T04-50-44".

此时进入 MongDb.log 发现文本为:

2014-12-31T12:50:44.319+0800 Trying to remove Windows service 'MongoDB'
2014-12-31T12:50:44.319+0800 Service 'MongoDB' removed

到此,mongodb 服务删除成功。

mongodb 常用指令

1、 启动 mongodb ;方法不再赘述, 以 $ net start MongoDB 为例;

$ cd /f/MongoDb/bin/&&net start MongoDb&&mongo

MongoDb 服务已经启动成功。

MongoDB shell version: 2.6.6
connecting to: test
> show dbs
admin (empty)
local 0.078GB
>_

2、 增删改查 ,以及常见指令

> db.highsea.save({"_id":"2","nickname":"high sea","job":"web前端开发工程师"})
WriteResult({ "nInserted" : 1 })
>_

注 此处如提示

 chunk 4114 failed with errno:1455 页面文件太小,无法完成操作。
……

则是服务器内存不够用了,建议增加虚拟内存或者物理内存

注 mongodb 中把关系数据库的"表"称作"集合" ……

以上创建了 名为”highsea“的表,(如果已存在 “highsea”的表 mongodb 会合并)并且插入了俩个字段 "nickname" 和 "job" ,主键是 “_id”,继续:

> db.highsea.insert({"_id":"1","nickname":"Gao Hai","QQ":"644494365"})
WriteResult({ "nInserted" : 1 })
> db.highsea.insert({"_id":"3","email":"admin@highsea90.com"})
WriteResult({ "nInserted" : 1 })
> db.highsea.insert({"_id":"3","email":"644494365@qq.com"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicat
e key error index: highsea.highsea.$_id_ dup key: { : \"3\" }"
}
})

以上,我又插入一条 主键 "_id" 为 "1" 的数据,然后又重复插入了 "_id"为"2" 的数据,mongodb 报错,那换成 save 继续:

> db.highsea.save({"_id":"3","email":"644494365@qq.com"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

mongodb 提示 Modified 成功。(这也是 save 和 insert 的区别)

此时

> db.highsea.find()
{ "_id" : "1", "nickname" : "Gao Hai", "QQ" : "644494365" }
{ "_id" : "2", "nickname" : "high sea", "job" : "web前端开发工程师" }
{ "_id" : "3", "email" : "644494365@qq.com" } > db.highsea.find({"QQ":"644494365"})
{ "_id" : "1", "nickname" : "Gao Hai", "QQ" : "644494365" } > show dbs
admin (empty)
highsea 0.078GB
local 0.078GB
test 0.078GB > db.version()
2.6.6 > db.getMongo()
connection to 127.0.0.1 > db.addUser("hs","mg123","true")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' inste
ad
Successfully added user: { "user" : "hs", "roles" : [ "read" ] } > show users
{
"_id" : "highsea.hs",
"user" : "hs",
"db" : "highsea",
"roles" : [
{
"role" : "read",
"db" : "highsea"
}
]
} > show collections
highsea
system.indexes > db.createCollection("wellcome")
{ "ok" : 1 } > use highsea
switched to db highsea > db.dropDatabase()
{ "dropped" : "highsea", "ok" : 1 } > show dbs
admin 0.078GB
local 0.078GB
test 0.078GB
wellcome (empty) >_

这里有个提示: 2.6.6 版本已经不推荐使用 addUser 了,请使用 createUser

  • 注:输入中文时注意光标

以上展示了 指令 :

show dbs                    显示所有数据库
db.foo.save({}) 创建|更新某个数据库
db.foo.insert({}) 插入某个数据库
db.foo.find() 显示某个数据库
db.foo.find({"foo":"foo"}) 对于当前数据库中的foo集合进行查找,条件是数据中有一个属性叫foo,且foo的值为foo
db.version() 查看当前版本
db.getMongo() 获取链接当前数据库的服务器
db.addUser() 增加数据库用户
show users 查看用户
show collections 显示当前数据库的集合
db.createCollection("foo") 创建一个名为"foo"的空数据库
use foo 切换到某个数据库
db.dropDatabase() 删除当前数据库

其他指令 可以使用 help | db.help() | db.yourColl.help() | db.youColl.find().help()

附: Mongodb 数据库操作 help 指令 中文版

  • help
    db.help()                    help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
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

db.help();

DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.fsyncLock() flush data to disk and lock server for backups
db.fsyncUnlock() unlocks server following a db.fsyncLock()
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow queries on a replication slave server
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
db.hostInfo() get details about the server's host
db.isMaster() check replica primary status
db.killOp(opid) kills the current operation in the db
db.listCommands() lists all the db commands
db.loadServerScripts() loads all the scripts in db.system.js
db.logout()
db.printCollectionStats()
db.printReplicationInfo()
db.printShardingStatus()
db.printSlaveReplicationInfo()
db.dropUser(username)
db.repairDatabase()
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
db.serverStatus()
db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
db.setVerboseShell(flag) display extra information in shell output
db.shutdownServer()
db.stats()
db.version() current version of the server
  • db.yourColl.help();
DBCollection help
db.yourColl.find().help() - show DBCursor help
db.yourColl.count()
db.yourColl.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
db.yourColl.convertToCapped(maxBytes) - calls {convertToCapped:'yourColl', size:maxBytes}} command
db.yourColl.dataSize()
db.yourColl.distinct( key ) - e.g. db.yourColl.distinct( 'x' )
db.yourColl.drop() drop the collection
db.yourColl.dropIndex(index) - e.g. db.yourColl.dropIndex( "indexName" ) or db.yourColl.dropIndex( { "indexKey" : 1 } )
db.yourColl.dropIndexes()
db.yourColl.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.yourColl.reIndex()
db.yourColl.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return. - e.g. db.yourColl.find( {x:77} , {name:1, x:1} )
db.yourColl.find(...).count()
db.yourColl.find(...).limit(n)
db.yourColl.find(...).skip(n)
db.yourColl.find(...).sort(...)
db.yourColl.findOne([query])
db.yourColl.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.yourColl.getDB() get DB object associated with collection
db.yourColl.getPlanCache() get query plan cache associated with collection
db.yourColl.getIndexes()
db.yourColl.group( { key : ..., initial: ..., reduce : ...[, cond: ...]} )
db.yourColl.insert(obj)
db.yourColl.mapReduce( mapFunction , reduceFunction , <optional params>)
db.yourColl.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
db.yourColl.remove(query)
db.yourColl.renameCollection( newName , <dropTarget> ) renames the collection.
db.yourColl.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.yourColl.save(obj)
db.yourColl.stats()
db.yourColl.storageSize() - includes free space allocated to this collection
db.yourColl.totalIndexSize() - size in bytes of all the indexes
db.yourColl.totalSize() - storage allocated for all data and indexes
db.yourColl.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multi
db.yourColl.validate( <full> ) - SLOW
db.yourColl.getShardVersion() - only for use with sharding
db.yourColl.getShardDistribution() - prints statistics about data distribution in the cluster
db.yourColl.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
db.yourColl.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
db.yourColl.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
db.yourColl.unsetWriteConcern( <write concern doc> ) - unsets the writeconcern for writes to the collection
  • db.youColl.find().help();
find() modifiers
.sort( {...} )
.limit( n )
.skip( n )
.count(applySkipLimit) - total # of objects matching query. by default ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain([verbose])
.hint(...)
.addOption(n) - adds op_query options -- see wire protocol
._addSpecial(name, value) - http://dochub.mongodb.org/core/advancedqueries#AdvancedQueries-Metaqueryoperators
.batchSize(n) - sets the number of docs to return per getMore
.showDiskLoc() - adds a $diskLoc field to each returned object
.min(idxDoc)
.max(idxDoc)
.comment(comment)
.snapshot()
.readPref(mode, tagset) Cursor methods
.toArray() - iterates through docs and returns an array of the results
.forEach( func )
.map( func )
.hasNext()
.next()
.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
.itcount() - iterates through documents and counts them
.pretty() - pretty print each document, possibly over multiple lines

附:Mongodb help 指令 中文版

Options:

General options:
-h [ --help ] show this usage information
--version show version information
-f [ --config ] arg configuration file specifying additional options
-v [ --verbose ] [=arg(=v)] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
--quiet quieter output
--port arg specify port number - 27017 by default
--bind_ip arg comma separated list of ip addresses to listen on
- all local ips by default
--maxConns arg max number of simultaneous connections - 1000000
by default
--logpath arg log file to send write to instead of stdout - has
to be a file, not directory
--logappend append to logpath instead of over-writing
--timeStampFormat arg Desired format for timestamps in log messages.
One of ctime, iso8601-utc or iso8601-local
--pidfilepath arg full path to pidfile (if not set, no pidfile is
created)
--keyFile arg private key for cluster authentication
--setParameter arg Set a configurable parameter
--httpinterface enable http interface
--clusterAuthMode arg Authentication mode used for cluster
authentication. Alternatives are
(keyFile|sendKeyFile|sendX509|x509)
--auth run with security
--noauth run without security
--ipv6 enable IPv6 support (disabled by default)
--jsonp allow JSONP access via http (has security
implications)
--rest turn on simple rest api
--slowms arg (=100) value of slow for profile and console log
--profile arg 0=off 1=slow, 2=all
--cpu periodically show cpu and iowait utilization
--sysinfo print some diagnostic system information
--dbpath arg directory for datafiles - defaults to \data\db\
--directoryperdb each database will be stored in a separate
directory
--noIndexBuildRetry don't retry any index builds that were
interrupted by shutdown
--noprealloc disable data file preallocation - will often hurt
performance
--nssize arg (=16) .ns file size (in MB) for new databases
--quota limits each database to a certain number of files
(8 default)
--quotaFiles arg number of files allowed per db, implies --quota
--smallfiles use a smaller default file size
--syncdelay arg (=60) seconds between disk syncs (0=never, but not
recommended)
--upgrade upgrade db if needed
--repair run repair on all dbs
--repairpath arg root directory for repair files - defaults to
dbpath
--noscripting disable scripting engine
--notablescan do not allow table scans
--journal enable journaling
--nojournal disable journaling (journaling is on by default
for 64 bit)
--journalOptions arg journal diagnostic options
--journalCommitInterval arg how often to group/batch commit (ms) Windows Service Control Manager options:
--install install Windows service
--remove remove Windows service
--reinstall reinstall Windows service (equivalent to --remove
followed by --install)
--serviceName arg Windows service name
--serviceDisplayName arg Windows service display name
--serviceDescription arg Windows service description
--serviceUser arg account for service execution
--servicePassword arg password used to authenticate serviceUser Replication options:
--oplogSize arg size to use (in MB) for replication op log. default is
5% of disk space (i.e. large is good) Master/slave options (old; use replica sets instead):
--master master mode
--slave slave mode
--source arg when slave: specify master as <server:port>
--only arg when slave: specify a single database to replicate
--slavedelay arg specify delay (in seconds) to be used when applying
master ops to slave
--autoresync automatically resync if slave data is stale Replica set options:
--replSet arg arg is <setname>[/<optionalseedhostlist>]
--replIndexPrefetch arg specify index prefetching behavior (if secondary)
[none|_id_only|all] Sharding options:
--configsvr declare this is a config db of a cluster; default port
27019; default dir /data/configdb
--shardsvr declare this is a shard db of a cluster; default port
27018

Mongodb For Windows的更多相关文章

  1. Mongodb在Windows 7下的安装及配置

    第一步 下载MongoDB: 下载mongodb的windows版本,有32位和64位版本,根据操作系统情况下载,下载地址:http://www.mongodb.org/downloads 解压缩至指 ...

  2. mongoDB在windows下安装与配置方案

    首先在官网下载mongoDB的安装包: https://www.mongodb.org/downloads 百度云盘下载:http://pan.baidu.com/s/1slUSGYp (安装版 wi ...

  3. 【MongoDB】windows平台搭建Mongo数据库复制集(类似集群)(转)

    原文链接:[MongoDB]windows平台搭建Mongo数据库复制集(类似集群)(一) Replica  Sets(复制集)是在mongodDB1.6版本开始新增的功能,它可以实现故障自动切换和自 ...

  4. Mongodb在Windows上的配置

    1.打开mongodb的官网:https://www.mongodb.org/进行下载相应平台的安装包 2.我们选择最新版的3.2版本来下载,选择对应的操作系统版本来下载,这里选择windows Mo ...

  5. mongoDB在windows下基于配置文件的安装和权限配置方式

    下载mongoDB  http://www.mongodb.org/downloads 根据操作系统,选择需要下载的安装包 添加mongodb 安装目录 将解压的文件夹中内容拷贝,存放在想要安装的文件 ...

  6. Install MongoDB on Windows

    Overview Use this tutorial to install MongoDB on a Windows systems. PLATFORM SUPPORT Starting in ver ...

  7. Mongodb在Windows下安装及配置 【转】

    1.下载mongodb的windows版本,有32位和64位版本,根据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.解压缩至E:/mongodb即可 3 ...

  8. Install MongoDB on Windows (Windows下安装MongoDB)

    Install MongoDB on Windows Overview Use this tutorial to install MongoDB on a Windows systems. PLATF ...

  9. PHP学习之-Mongodb在Windows下安装及配置

    Mongodb在Windows下安装及配置 1.下载 下载地址:http://www.mongodb.org/ 建议下载zip版本. 2.安装 下载windows版本安装就和普通的软件一样,直接下一步 ...

  10. mongoDB安装windows 64 bit

    mongoDB安装windows 64 bit   https://www.mongodb.org/downloads?_ga=1.207888916.746558625.1410501054 下载, ...

随机推荐

  1. FGPA 双向 IO 自动方向控制

    Using a Virtex Device to Drive 5V CMOS-Level Signals Voltage Level-Shifter Output Waveform

  2. 管理windows防火墙

    1.导出防火墙规则 netsh advfirewall export "c:\advfirewall.wfw" 2.禁用防火墙 netsh firewall set opmode ...

  3. Microsoft.VisualBasic.DateAndTime.Timer 与 DateTime.Now.TimeOfDay.TotalSeconds 相当

    如题,示例如下: Console.WriteLine(DateTime.Now.TimeOfDay.TotalSeconds); Console.WriteLine(Microsoft.VisualB ...

  4. 使用 Express 和 waterline 创建简单 Restful API

    这几篇都是我原来首发在 segmentfault 上的地址:https://segmentfault.com/a/1190000004996659  突然想起来我这个博客冷落了好多年了,也该更新一下, ...

  5. JavaScript日期对象使用总结

    javascript Date日期对象的创建 创建一个日期对象: var objDate=new Date([arguments list]); 我总结了参数形式主要有以下3种: new Date(& ...

  6. sqlserver 生成UUID随机码

    )) ) AS BEGIN ); ,),),),),) RETURN @id END --使用如下 select dbo.[FunGetUUID32](NEWID());

  7. 查看android app 线程信息的命令

    参考:https://my.oschina.net/zhiweiofli/blog/138454 ps | grep 'joyodream' 找到 app 的pid: joyodream为包名的一部分 ...

  8. Changing the type of a property with EF Code First

    The smartest way is probably to not alter types. If you need to do this, I'd suggest you to do the f ...

  9. Reflector反编译.NET文件后修复【转】

    反编译后的工程文件用VS2010打开后,在打开窗体时会出现一系列错误提示: 第一种情况: “设计器无法处理第 152 行的代码: base.AutoScaleMode = AutoScaleMode. ...

  10. C#中控件数组的讨论

    VB用得习惯后,到C#中来觉得很奇怪,如此好的控件数组怎么不见了.“众所周知,控件数组最主要的两个优点:可以循环附值:可以响应同一个事件.从而大大简化了代码.引自http://wenku.baidu. ...