@52fhy
        
        2015-09-20T08:06:52.000000Z
        字数 4928
        阅读 703
    mongo
这一篇我们以管理员的视角来看mongodb,作为一名管理员,我们经常接触到的主要有4个方面:
1.安装部署
2.状态监控
3.安全认证
4.备份恢复
下面我们就一点一点的讲解。
我之前的文章都是采用console程序来承载,不过在生产环境中这并不是最佳实践,谁也不愿意在机器重启后满地找牙似找mongodb,在mongodb里面提供了一个叫做“服务寄宿”的模式,我想如果大家对wcf比较熟悉的话很容易听懂。好了,我们实践一下,这里我开一下D盘里面的mongodb。
mongod --dbpath=D:\Projects\mongodb\db --logpath=D:\Projects\mongodb\logs\log.txt --port 27017 --install
这里要注意的有两点: 
   <1> logpath: 当我们使用服务寄宿的时候,肯定不会用console来承载日志信息了。 
   <2> install:   开启安装服务寄宿,把管理员的手工操作降低到最小。 
   <3> 可能需要管理员权限。 
   <4> 如果存在db\mongod.lock文件请删除
好了,console程序叫我看log日志,那我就看看,发现mongodb已经提示我们如何开启mongodb,接着我照做就是了。
重新安装是reinstall
移除是remove
以后mongod会随着系统的启动而启动。
关于mongod的帮助 
mongod --help 服务端的命令帮助 
mongo --help  客户端的命令帮助 
进入mongo后还可以输入help查看帮助
监控可以让我们实时的了解数据库的健康状况以及性能调优,在mongodb里面给我们提供了三种方式。
1:http监视器
2:serverStatus() 
    这个函数可以获取到mongodb的服务器统计信息,其中包括:全局锁,索引,用户操作行为等等这些统计信息,对管理员来说非常重要,具体的参数含义可以参考:http://www.cnblogs.com/xuegang/archive/2011/10/13/2210339.html。
这个需要进入客户端运行:
db.serverStatus()
3:mongostat 
前面那些统计信息再牛X,那也是静态统计,不能让我观看实时数据变化,还好,mongodb里面提供了这里要说的mongodstat监视器,这玩意会每秒刷新,在实际生产环境中大有用处。这个命令不是在客户端运行的,与mongod命令同级。
mongostat --port 27017
(1)3.x版本以下: 
作为数据库软件,我们肯定不想谁都可以访问,为了确保数据的安全,mongodb也会像其他的数据库软件一样可以采用用户验证的方法,那么该怎么做呢?其实很简单,mongodb提供了addUser方法,还有一个注意点就是如果在admin数据库中添加将会被视为“超级管理员”。
db.addUser("admin","123456")
如果是
db.addUser("test","123456",true)
存在第三个参数,则表示是否是“只读用户”。
我们第一次登录时不是采用验证模式,现在我们使用--reinstall重启服务并以--auth验证模式登录。
mongod --dbpath=D:\Projects\mongodb\db --logpath=D:\Projects\mongodb\logs\log.txt --port 27017 --auth --reinstall
好了,我们进入test集合翻翻数据看看情况,我们发现jack用户始终都是没有写入的权限,不管是授权或者未授权。
(2)3.x版本: 
addUser在mongdb 3.0已经废弃了,对应的函数请用createUser,createUser接受两个参数,一个user object,一个可选参数用户角色,具体参考mongodb 3.0的文档:http://docs.mongodb.org/manual/reference/method/js-user-management/
db.createUser() Creates a new user.db.updateUser() Updates user data.db.changeUserPassword() Changes an existing user’s password.db.removeUser() Deprecated. Removes a user from a database.db.dropAllUsers() Deletes all users associated with a database.db.dropUser() Removes a single user.db.grantRolesToUser() Grants a role and its privileges to a user.db.revokeRolesFromUser() Removes a role from a user.db.getUser() Returns information about the specified user.db.getUsers() Returns information about all users associated with a database.
示例:
>use test>db.createUser({user: "accountUser",pwd: "password",roles: [ "readWrite", "dbAdmin" ]})
这玩意的重要性我想都不需要我来说了吧,这玩意要是搞不好会死人的,mongodb里面常用的手段有3种。
1: 直接copy 
这个算是最简单的了,不过要注意一点,在服务器运行的情况下直接copy是很有风险的,可能copy出来时,数据已经遭到破坏,唯一能保证的就是要暂时关闭下服务器,copy完后重开。
2:mongodump和mongorestore 
这个是mongo给我们提供的内置工具,很好用,能保证在不关闭服务器的情况下copy数据。为了操作方便,我们先删除授权用户。
使用mongodump备份比较慢,在备份复制集时还有些问题,后续会说到。但是,用来备份单个数据库、集合、子集合还是比较好的方法。
mongod未运行情况下:
备份全部:
mongodump -p 27017
将在当前目录下创建dump目录,备份所有的数据库,所有的数据存储在.bson文件中,可以使用mongodb提供的bsondump工具来检索它。
备份test数据库
mongodump -p 27017 -d test -o D:\Projects\mongodb\dump
恢复时,使用mongorestore工具恢复 
恢复整个数据库:
mongorestore --port 27017 D:\Projects\mongodb\dump
恢复到特定的库和集合:
mongorestore --db test --collection user --drop D:\Projects\mongodb\dump
--drop 先删除原有数据库里面的数据
注意:  
1、mongodump和mongorestore版本最好相匹配。 
2、--dbpath and related flags are not supported in 3.0 tools. 
3、--db与-d是一样的,即-表示缩写 
4、使用mongodump --help查看更多命令(使用 - 和 / 都可以)
mongodump --helpUsage:mongodump <options>Export the content of a running server into .bson files.Specify a database with -d and a collection with -c to only dump that database or collection.See http://docs.mongodb.org/manual/reference/program/mongodump/ for more information.general options:/help print usage/version print the tool version and exitverbosity options:/v, /verbose more detailed log output (includemultiple times for more verbosity, e.g.-vvvvv)/quiet hide all log outputconnection options:/h, /host: mongodb host to connect to(setname/host1,host2 for replica sets)/port: server port (can also use --hosthostname:port)authentication options:/u, /username: username for authentication/p, /password: password for authentication/authenticationDatabase: database that holds the user's credentials/authenticationMechanism: authentication mechanism to usenamespace options:/d, /db: database to use/c, /collection: collection to usequery options:/q, /query: query filter, as a JSON string, e.g.,'{x:{$gt:1}}'/forceTableScan force a table scanoutput options:/o, /out: output directory, or '-' for stdout(defaults to 'dump')/repair try to recover documents from damageddata files (not supported by all storageengines)/oplog use oplog for taking a point-in-timesnapshot/dumpDbUsersAndRoles dump user and role definitions for thespecified database/excludeCollection: collection to exclude from the dump (maybe specified multiple times to excludeadditional collections)/excludeCollectionsWithPrefix: exclude all collections from the dumpthat have the given prefix (may bespecified multiple times to excludeadditional prefixes)
3:主从复制 
这个我在上上篇有所介绍,这里也不赘述了。
其实上面的1,2两点都不能保证获取数据的实时性,因为我们在备份的时候可能还有数据灌在内存中不出来,那么我们想说能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求。
fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实例的写入操作全部被阻塞,直到fsync+lock释放锁为止。这里就不测试了。
加锁(需要use admin):
db.runCommand({"fsync":1,"lock":1})
释放锁:db.fsyncUnlock()  
或者db.$cmd.sys.unlock.findOne()
参考:
8天学通MongoDB——第七天 运维技术 - 一线码农 - 博客园
http://www.cnblogs.com/huangxincheng/archive/2012/03/08/2384571.html