MongoDB三大系统库详解

一、概述

  MongoDB默认会创建admin、config、local、test数据库。test库是一个默认的数据库,除了test库外admin、config、local库为系统库。admin库主要存储MongoDB的用户、角色等信息,config库主要存储分片集群基础信息,local库主要存储副本集的元数据。

二、admin库

  当MongoDB启用auth选项时,用户需要创建数据库帐号,访问时根据帐号信息来鉴权,而数据库帐号信息就存储在admin数据库下。

single:PRIMARY> use admin
switched to db admin
single:PRIMARY> db.auth("root","abc123")
1
single:PRIMARY> show collections;
system.keys
system.roles
system.users
system.version

system.version存储authSchema的版本信息

single:PRIMARY> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "4.2" }
{ "_id" : "authSchema", "currentVersion" : 5 }

system.users存储数据库帐号信息

single:PRIMARY> db.system.users.find()
{ "_id" : "admin.root", "userId" : UUID("642d00b9-5daa-4ff9-881d-248d12f1cea5"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "RYFapYvcnsBTzpBSoOJEtQ==", "storedKey" : "9DDghKxRMNJIGtnJSDMiU5eW3FI=", "serverKey" : "ofDQbqiaHv7TtfgKUsy45+rLygg=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "XuswE9YVGb5kvKAjRgk6C7yYibXueYfV3Z2ftg==", "storedKey" : "V8ujyqxe0LsxFhfeqDedKzrlVyrpEc9gNJJXd+iHtdU=", "serverKey" : "o/eSr/2JERG53rD3syabUtcBPL83733tnslJZCXNrJ4=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "admin.backup", "userId" : UUID("c365c48f-5bb7-446c-8219-e50ab99866ac"), "user" : "backup", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "4s75h29oYQhmrx4SV4Kdjw==", "storedKey" : "wWRXZeXadmQB1B4pgLV+YV6Rjbw=", "serverKey" : "Iu69IR6Y5bnGXHQ0+PyQTZN48i8=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "zZFw7oMOgYRVc8bHfJX+m077pZQ93wbyLGOU0Q==", "storedKey" : "O6DAfbOKyMNUIYsHYNNcFiclZ/iMODmZdZmbJw22c5Y=", "serverKey" : "v/3H594Wo5fcclh9YP8GGe+5SLGfOKIBRBO13OrOggY=" } }, "roles" : [ { "role" : "backup", "db" : "admin" }, { "role" : "restore", "db" : "admin" } ] }

system.roles存储自定义的角色

single:PRIMARY> db.system.roles.find()
{ "_id" : "admin.MyreadWrite", "role" : "MyreadWrite", "db" : "admin", "privileges" : [ { "resource" : { "db" : "bayern", "collection" : "" }, "actions" : [ "insert", "remove", "update" ] } ], "roles" : [ { "role" : "read", "db" : "bayern" } ] }
{ "_id" : "admin.manageOpRole", "role" : "manageOpRole", "db" : "admin", "privileges" : [ { "resource" : { "cluster" : true }, "actions" : [ "inprog", "killop" ] }, { "resource" : { "db" : "", "collection" : "" }, "actions" : [ "killCursors" ] } ], "roles" : [ ] }

  用户可以在admin数据库下建立任意集合,存储任何数据,但强烈建议不要使用admin数据库存储应用业务数据,最好创建新的数据库。
  admin数据库里的system.users、system.roles2个集合的数据MongoDB会cache在内存里,这样不用每次鉴权都从磁盘加载用户角色信息。目前cache的维护代码,只有在保证system.users、system.roles的写入都串行化的情况下才能正确工作。
  MongoDB admin数据库的写入操作的锁级别只能到DB级别,不支持多个collection并发写入,在写入时也不支持并发读取。如果用户在admin数据库里存储业务数据,则可能遭遇性能问题。

三、config库

  config库在使用MongoDB分片功能时起到作用,主要存储分片集群基础信息。

single:PRIMARY> use config
switched to db config
single:PRIMARY> show collections;
actionlog
changelog
chunks
collections
databases
lockpings
locks
migrations
mongos
settings
shards
system.sessions
tags
transactions
version
configReplSet:SECONDARY> db.actionlog.find()
{ "_id" : "cdh01:28020-2020-06-12T20:49:11.591+0800-5ee379c79fb30586e5b40eb0", "server" : "cdh01:28020", "shard" : "config", "clientAddr" : "", "time" : ISODate("2020-06-12T12:49:11.591Z"), "what" : "balancer.round", "ns" : "", "details" : { "executionTimeMillis" : 35548, "errorOccured" : true, "errmsg" : "Could not find host matching read preference { mode: \"primary\" } for set shard2" } }
configReplSet:SECONDARY> db.changelog.find()
{ "_id" : "cdh02:28020-2020-06-12T20:27:56.938+0800-5ee374cc5011858194682148", "server" : "cdh02:28020", "shard" : "config", "clientAddr" : "172.16.101.231:39310", "time" : ISODate("2020-06-12T12:27:56.938Z"), "what" : "addShard", "ns" : "", "details" : { "name" : "shard1", "host" : "shard1/172.16.101.231:28021,172.16.101.204:28021,172.16.101.204:28021" } }
{ "_id" : "cdh02:28020-2020-06-12T20:28:05.127+0800-5ee374d55011858194682197", "server" : "cdh02:28020", "shard" : "config", "clientAddr" : "172.16.101.231:39310", "time" : ISODate("2020-06-12T12:28:05.127Z"), "what" : "addShard", "ns" : "", "details" : { "name" : "shard2", "host" : "shard2/172.16.101.231:28022,172.16.101.204:28022,172.16.101.204:28022" } }
...
configReplSet:SECONDARY> db.chunks.find()
{ "_id" : "test.t2-id_4611686018427387902", "ns" : "test.t2", "min" : { "id" : NumberLong("4611686018427387902") }, "max" : { "id" : { "$maxKey" : 1 } }, "shard" : "shard2", "lastmod" : Timestamp(1, 3), "lastmodEpoch" : ObjectId("5ee3756d9ceec30eccbd4269"), "history" : [ { "validAfter" : Timestamp(1591965038, 1), "shard" : "shard2" } ] }
...
configReplSet:SECONDARY> db.collections.find()
{ "_id" : "test.t1", "lastmodEpoch" : ObjectId("000000000000000000000000"), "lastmod" : ISODate("2020-06-12T12:34:20.482Z"), "dropped" : true }
{ "_id" : "test.t2", "lastmodEpoch" : ObjectId("5ee3756d9ceec30eccbd4269"), "lastmod" : ISODate("1970-02-19T17:02:47.299Z"), "dropped" : false, "key" : { "id" : "hashed" }, "unique" : false, "uuid" : UUID("b759dd58-5f1e-4741-8ce1-6fed53b41aef") }
{ "_id" : "config.system.sessions", "lastmodEpoch" : ObjectId("5ee375f79ceec30eccbd4de1"), "lastmod" : ISODate("1970-02-19T17:02:47.296Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "uuid" : UUID("a37c61f1-b752-4013-a1f8-699c86a6f3a5") }
configReplSet:SECONDARY> db.databases.find()
{ "_id" : "test", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("0e14834d-fd5c-401b-b6ee-3006ef5c16a2"), "lastMod" : 1 } }
configReplSet:SECONDARY> db.lockpings.find()
{ "_id" : "ConfigServer", "ping" : ISODate("2020-07-05T12:53:48.868Z") }
{ "_id" : "cdh01:28018:1591964813:-5318065364096372981", "ping" : ISODate("2020-06-12T12:41:56.406Z") }
configReplSet:SECONDARY> db.locks.find()
{ "_id" : "test", "state" : 0, "process" : "ConfigServer", "ts" : ObjectId("5ee376565011858194682dda"), "when" : ISODate("2020-06-12T12:34:30.791Z"), "who" : "ConfigServer:conn29", "why" : "createCollection" }
configReplSet:SECONDARY> db.mongos.find()
{ "_id" : "cdh01:28018", "advisoryHostFQDNs" : [ ], "mongoVersion" : "4.2.3", "ping" : ISODate("2020-07-05T12:54:54.089Z"), "up" : NumberLong(1987387), "waiting" : true }
configReplSet:PRIMARY> db.settings.find()
{ "_id" : "balancer", "mode" : "off", "stopped" : true }
{ "_id" : "autosplit", "enabled" : false }
{ "_id" : "chunksize", "value" : 8 }
configReplSet:PRIMARY> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/172.16.101.142:28021,172.16.101.204:28021,172.16.101.231:28021", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/172.16.101.142:28022,172.16.101.204:28022,172.16.101.231:28022", "state" : 1 }

四、local库

  local库它只会在本地存储数据,local数据库里的内容不会同步到副本集里其他节点上去;local数据库主要存储副本集的配置信息、oplog信息。
  另外,对于重要的数据不能存储在local数据库,还要注意MongoDB默认的WriteConcern是{w: 1},即数据写到Primary上(不保证journal已经写成功)就向客户端确认,这时同样存在丢数据的风险。对于重要的数据,可以设置更高级别的如{w: “majority”}来保证数据写到大多数节点后再向客户端确认,当然这对写入的性能会造成一定的影响。
注意

在使用MongoDB时,重要的数据千万不要存储在local数据库中,否则当一个节点故障时,存储在local里的数据就会丢失。
single:PRIMARY> use local
switched to db local
single:PRIMARY> show collections;
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.profile
system.replset
system.rollback.id

oplog.rs保存oplog的capped集合,从4.0开始,oplog允许超过固定大小。

db.oplog.rs.find().sort({"ts":-1}).limit(10)
{ "ts" : Timestamp(1593956234, 1), "t" : NumberLong(11), "h" : NumberLong(0), "v" : 2, "op" : "i", "ns" : "test.t1", "ui" : UUID("d53288a9-5e94-4bc7-9558-cac3994f3683"), "wall" : ISODate("2020-07-05T13:37:14.244Z"), "o" : { "_id" : ObjectId("5f01d78a2285c6f655946fe2"), "id" : 1 } }
single:PRIMARY> db.replset.election.find()
{ "_id" : ObjectId("5ec3d8a474a5042ff2b102e5"), "term" : NumberLong(11), "candidateIndex" : NumberLong(0) }

replset.minvalid包含一个由副本集内部用来跟踪复制状态的对象

single:PRIMARY> db.replset.minvalid.find()
{ "_id" : ObjectId("5ec3d8a474a5042ff2b102e4"), "t" : NumberLong(-1), "ts" : Timestamp(0, 0) }
db.replset.oplogTruncateAfterPoint.find()
{ "_id" : "oplogTruncateAfterPoint", "oplogTruncateAfterPoint" : Timestamp(0, 0) }

  启动时,每个mongod实例都会将文档插入 startup_log其中,其中包含有关mongod实例本身和主机信息的诊断 信息。此信息主要用于诊断目的。

single:PRIMARY> db.startup_log.find().pretty().limit(1)
{
#包括系统主机名和时间戳
        "_id" : "zijie-1589893284149",
#系统主机名
        "hostname" : "zijie",
#一个UTC ISODate值,反映了服务器启动的时间
        "startTime" : ISODate("2020-05-19T13:01:24Z"),
#一个字符串,报告startTime 系统本地时区
        "startTimeLocal" : "Tue May 19 21:01:24.149",
#嵌入式文档,报告mongod运行时选项及其值
        "cmdLine" : {
                "config" : "/etc/mongodb.conf",
                "net" : {
                        "bindIp" : "0.0.0.0",
                        "port" : 27017
                },
                "operationProfiling" : {
                        "mode" : "slowOp",
                        "slowOpThresholdMs" : 100
                },
                "processManagement" : {
                        "fork" : true
                },
                "replication" : {
                        "replSet" : "single"
                },
                "security" : {
                        "authorization" : "enabled"
                },
                "storage" : {
                        "dbPath" : "/data/mongodb27017/data",
                        "journal" : {
                                "enabled" : true
                        }
                },
                "systemLog" : {
                        "destination" : "file",
                        "logAppend" : true,
                        "path" : "/data/mongodb27017/logs",
                        "quiet" : false,
                        "verbosity" : 1
                }
        },
#此进程的标识符
        "pid" : NumberLong(13727),
#一个嵌入式文档,报告有关构建环境和用于编译该环境的设置的信息
        "buildinfo" : {
                "version" : "4.2.3",
                "gitVersion" : "6874650b362138df74be53d366bbefc321ea32d4",
                "modules" : [ ],
                "allocator" : "tcmalloc",
                "javascriptEngine" : "mozjs",
                "sysInfo" : "deprecated",
                "versionArray" : [
                        4,
                        2,
                        3,
                        0
                ],
                "openssl" : {
                        "running" : "OpenSSL 1.0.1e-fips 11 Feb 2013",
                        "compiled" : "OpenSSL 1.0.1e-fips 11 Feb 2013"
                },
                "buildEnvironment" : {
                        "distmod" : "rhel70",
                        "distarch" : "x86_64",
                        "cc" : "/opt/mongodbtoolchain/v3/bin/gcc: gcc (GCC) 8.2.0",
                        "ccflags" : "-fno-omit-frame-pointer -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -Werror -O2 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-const-variable -Wno-unused-but-set-variable -Wno-missing-braces -fstack-protector-strong -fno-builtin-memcmp",
                        "cxx" : "/opt/mongodbtoolchain/v3/bin/g++: g++ (GCC) 8.2.0",
                        "cxxflags" : "-Woverloaded-virtual -Wno-maybe-uninitialized -fsized-deallocation -std=c++17",
                        "linkflags" : "-pthread -Wl,-z,now -rdynamic -Wl,--fatal-warnings -fstack-protector-strong -fuse-ld=gold -Wl,--build-id -Wl,--hash-style=gnu -Wl,-z,noexecstack -Wl,--warn-execstack -Wl,-z,relro",
                        "target_arch" : "x86_64",
                        "target_os" : "linux"
                },
                "bits" : 64,
                "debug" : false,
                "maxBsonObjectSize" : 16777216,
                "storageEngines" : [
                        "biggie",
                        "devnull",
                        "ephemeralForTest",
                        "wiredTiger"
                ]
        }
}

system.profile记录慢日志

db.system.profile.find()
{ "op" : "command", "ns" : "local.system.rollback.id", "command" : { "replSetInitiate" : undefined, "lsid" : { "id" : UUID("5908de3e-0c3b-461a-8bdc-0154a8892694") }, "$readPreference" : { "mode" : "secondaryPreferred" }, "$db" : "admin" }, "numYield" : 0, "locks" : { "ParallelBatchWriterMode" : { "acquireCount" : { "r" : NumberLong(20) } }, "ReplicationStateTransition" : { "acquireCount" : { "w" : NumberLong(24) } }, "Global" : { "acquireCount" : { "r" : NumberLong(9), "w" : NumberLong(13), "W" : NumberLong(2) }, "acquireWaitCount" : { "W" : NumberLong(1) }, "timeAcquiringMicros" : { "W" : NumberLong(106) } }, "Database" : { "acquireCount" : { "r" : NumberLong(6), "w" : NumberLong(2), "W" : NumberLong(11) } }, "Collection" : { "acquireCount" : { "r" : NumberLong(4), "w" : NumberLong(2) } }, "Mutex" : { "acquireCount" : { "r" : NumberLong(16) } }, "oplog" : { "acquireCount" : { "r" : NumberLong(1), "w" : NumberLong(1) } } }, "flowControl" : { "acquireCount" : NumberLong(4) }, "storage" : {  }, "responseLength" : 139, "protocol" : "op_msg", "millis" : 142, "ts" : ISODate("2020-05-19T13:06:03.558Z"), "client" : "127.0.0.1", "appName" : "MongoDB Shell", "allUsers" : [ ], "user" : "" }

system.replset包含副本集成员上的集合

single:PRIMARY> db.system.replset.find()
{ "_id" : "single", "version" : 1, "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "members" : [ { "_id" : 0, "host" : "zijie:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {  }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : {  }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("5ec3d9bb74a5042ff2b102f3") } }
single:PRIMARY> db.system.rollback.id.find()
{ "_id" : "rollbackId", "rollbackId" : 1 }

五、总结

  本文基于MongoDB4.2版本介绍了各系统库的常用集合内容以及相关以及对应的文档信息,在MongoDB中,可以通过系统库来获取一些数据库的状态信息。

你可能感兴趣的:(MongoDB)