MongoDB 管理
相对于管理传统的关系型数据库而言,管理MongoDB算是比较轻松的,无论是备份还是多节点的复制都有其快捷的办法,这其实是MongoDB的设计理念:尽可能的简化系统操作,系统会尽量的自动完成各种配置工作,不需要管理员过多的参与。但是也不可能做到完全不需要管理员,还是有一些人为需要管理员人工参与。
- MongoDB其实就是一个用mongod进程调用的普通程序。
- MongoDB提供了内置的管理接口和监控功能,易于第三方监控集成。
- MongoDB支持基本的、数据库级别的用户认证,包括只读用于以及独立的管理员权限。
- 可以使用多种备份方式对Mongodb进行备份,可以根据实际情况选择。
启动和停止MongoDB
启动MongoDB的方法一般都是通过命令行和配置文件启动
#mongod --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
--syslog log to system's syslog facility instead of file
or stdout
--syslogFacility arg syslog facility used for monogdb syslog message
--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)
--nounixsocket disable listening on unix sockets
--unixSocketPrefix arg alternative directory for UNIX domain sockets
(defaults to /tmp)
--fork fork server process
--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)
--shutdown kill a running server (for init scripts)
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
以上列出了mongod可用的参数。当初次安装并启动MongoDB时,可以通过日志获取一些警告信息,能够避免发生一些错误。
让MongoDB稳妥的停止下来和启动它同样重要。最基本的方法就是向MongoDB服务器发送一个SIGINT或者SIGTERM信号。如果服务器是作为前台进程运行的终端,可以直接Ctrl+C。否则可以通过kill这种命令发出信号。如果mongod的PID是10014,就可以kill -2 10014(SIGINT)或者kill 10014(SIGTERM)。
当mongod收到SIGINT、SIGTERM信号时会正常的退出。也就是等到当前运行的操作或者文件预分配完成,关闭所有打开的链接,将缓存的数据刷新到磁盘,最后停止。另一种稳妥的方式就是使用shutdown命令,这是管理命令,要在admin数据库下使用,shell提供辅助函数
db.shutdownServer();
监控
作为MongoDB管理员,很重要的工作就是监控系统的状态和性能。
- 可以使用MongoDB提供的管理接口,这个服务的监听端口号比主服务的大1000。这个服务提供一个http服务器接口,可以查看MongoDB的一些基本信息。
- serverStatus(),要获取运行中的MongoDB服务器统计信息,最基本的工具就是serverStatus命令,呈现MongoDB内部的详细信息。
- mongostat 比较强大,可以便捷的查看serverStatus的结果。
安全和认证
许多公司在使用MongoDB的时候并不注重其用户认证方面,甚至不使用用户认证,这是十分危险的情况,用户认证是报障数据安全的一道屏障,建议在正式环境启用数据认证
--auth
。每个MongoDB实例中的数据库都可以有许多的用户。如果开启了安全性检查,则只有数据库认证用户才能执行读或者写操作。在开启用户认证之前,一定要至少有一个管理员账号。
MongoDB shell version: 2.6.9
connecting to: 127.0.0.1:7739/admin
>
> db
admin
> db.addUser("root","1qaz2wsx")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> use test
switched to db test
> db.addUser("test_user","1qaz2wsx")
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "test_user", "roles" : [ "dbOwner" ] }
> db.ad
db.addUser( db.adminCommand(
> db.addUser("read_only","1qaz2wsx",true);
WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' instead
Successfully added user: { "user" : "read_only", "roles" : [ "read" ] }
数据库的用户账号以文档的形式存储在
system.user
集合里面。知道了用户信息存储的位置后,有些日常管理任务执行起来就很轻松,直接通过删除system.user
集合中删掉用户账号文档,就可以删除用户。用户认证时,服务器将认证和连接绑定来跟踪认证。也就是说如果驱动程序或者是工具使用了连接池或者是因为故障切换到另一个节点,所有的认证用户必须对每个新连接重新认证。有些驱动程序可以将这步透明化,但要是没有,就得手动完成。在这种情况下,可以选择不启用auth。
备份与恢复
无论是关系型还是非关系型数据库,只要数据最终会落盘(有些内存数据库如memcached数据都在内存中,重启后数据消失),那么备份数据都是重中之重的一项工作。MongoDB将所有的数据文件存放在数据目录下,可以通过拷贝文件的方式对数据库进行冷备份,在MongoDB运行中拷贝数据是不安全的,但是通知服务器备份是不太理想的操作方式。备份的数据是MongodDB服务停止时刻的快照。
mongodump就是一种能在运行时备份的方法,其对运行的MongoDB做查询,然后将所有查到的文档写入磁盘。因为mongodump是一般的客户端,所以可供运行的服务使用,即便是正在处理其他请求或者执行写入也没问题。mongodump使用的是普通的查询机制,所以产生的备份不一定是服务器数据的实时快照。服务器在备份过程中处理大量的写入,问题会更加明显。备份时的查询会对其他客户端的性能产生不利影响。
#mongodump --help
Export MongoDB data to BSON files.
Options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple
times for more verbosity e.g. -vvvvv)
--quiet silence all non error diagnostic
messages
--version print the program's version and exit
-h [ --host ] arg mongo host to connect to ( <set
name>/s1,s2 for sets)
--port arg server port. Can also use --host
hostname:port
--ipv6 enable IPv6 support (disabled by
default)
-u [ --username ] arg username
-p [ --password ] arg password
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg (=MONGODB-CR)
authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication
--dbpath arg directly access mongod database files
in the given path, instead of
connecting to a mongod server - needs
to lock the data directory, so cannot
be used if a mongod is currently
accessing the same path
--directoryperdb each db is in a separate directory
(relevant only if dbpath specified)
--journal enable journaling (relevant only if
dbpath specified)
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-o [ --out ] arg (=dump) output directory or "-" for stdout
-q [ --query ] arg json query
--oplog Use oplog for point-in-time
snapshotting
--repair try to recover a crashed database
--forceTableScan force a table scan (do not use
$snapshot)
--dumpDbUsersAndRoles Dump user and role definitions for the
given database
相对备份而言,MongoDB提供了mongorestore用来恢复数据,其获取mongodump的输出并将备份的数据插入到运行的MongoDB实例中。
fsync和锁
fsync命令会强制服务器将所有的缓存数据写入磁盘,还可以选择上锁阻止对数据库的进一步写入,直到释放锁为止。这样可以保障备份的时候不会有数据写入到数据库,以此备份数据库的完整快照,但是在执行备份期间,应用无法对数据库执行写入操作,这在对于繁忙的数据库系统是不可以接受的。
> db.runCommand({"fsync":1,"lock":1})
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
有加锁的操作就必然有解锁的操作。
> db.$cmd.sys.unlock.findOne()
{ "ok" : 1, "info" : "unlock completed" }
> db.currentOp()
{ "inprog" : [ ] }
在本人所接触的环境中,多数是将备份的压力转移给了slave服务器,因为slave服务器的数据几乎和主服务器的同步,而不用太在乎从服务器的性能或者是能不能读写,于是就可以随意选择关停、转储和恢复等操作。
修复
备份是为了以防不测,MongoDB在掉电等情况发生时,其数据文件由于其存储方式,不能保证其可用性,此时,MongoD内置了修复功能会尝试恢复损毁的数据文件。修复数据库的实际过程非常简单,就是讲所有的文档全部导出然后导入,忽略那些无效的文档,完成后重新建立索引。数据量大的话会话很长时间,因为所有的数据都要验证,所有的索引都要重建,修复后有些损毁的数据文档就消失了,这会造成数据丢失的情况。修复数据还能起到压缩数据的作用。闲置的空间(比如删除体积较大的集合,或删除大量的文档腾出空间)在修复后被重新回收。因为修复损毁数据会造成最后的一招,尽可能稳妥地停掉数据库,做备份恢复才是有效的容灾策略,而不是寄希望于数据修复操作。
>