我正在阅读 Postgres 7.3文档以向 PostgreSQL 表添加约束。
它说:
要添加不能写为表约束的非空约束,请使用以下语法:
ALTER TABLE 产品 ALTER COLUMN product_no SET NOT NULL;
约束会被立即检查,所以表数据必须满足约束才能添加。
我不明白“不能写成表约束”是什么意思?
什么是表约束?什么约束不立即检查?
我正在阅读 Postgres 7.3文档以向 PostgreSQL 表添加约束。
它说:
要添加不能写为表约束的非空约束,请使用以下语法:
ALTER TABLE 产品 ALTER COLUMN product_no SET NOT NULL;
约束会被立即检查,所以表数据必须满足约束才能添加。
我不明白“不能写成表约束”是什么意思?
什么是表约束?什么约束不立即检查?
我正在使用 nodejs mongodb 驱动程序连接到自签名的 mongodb 实例。MongoDB 和 Mongo Shell 版本是 4.0.0。以下是启动自签名 mongodb 实例的命令:
mongod --port 27018 --sslMode requireSSL --sslPEMKeyFile mongodb.pem --dbpath data
当我使用 mongo shell 连接到此服务器时,我可以使用以下命令而不使用 pem 文件:
mongo --port 27018 --ssl --sslAllowInvalidCertificates
我想知道 PEM 文件在连接中的用途。
MongoDB 3.6 中的新特性之一是 jsonSchema 验证器。我可以使用验证器创建一个集合,例如:
{
"validator": {
"$jsonSchema": {
"properties": {
"name": {
"bsonType": "string",
"description": "name"
},
"age": {
"bsonType": "int",
"minimum": 10,
"maximum": 30
}
},
"required": ["name", "age"],
"bsonType": "object"
}
}
}
但是,如果该集合已经存在一些文档,而这些文档不能满足验证模式呢?如何根据验证器找出所有无效文件?
我正在使用 mongodb 3.6 并为该students
集合提供以下验证器:
rs1:PRIMARY> db.getCollectionInfos({name: 'students'})
[
{
"name" : "students",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"properties" : {
"name" : {
"bsonType" : "string",
"description" : "name"
},
"age" : {
"bsonType" : "int",
"minimum" : 10,
"maximum" : 30
}
},
"required" : [
"name",
"age"
],
"bsonType" : "object"
}
},
"validationLevel" : "strict",
"validationAction" : "error"
},
"info" : {
"readOnly" : false,
"uuid" : BinData(4,"111r7z+rQpGoebZ1jAHO4g==")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.students"
}
}
]
它需要两个属性name
和age
。姓名必须是字符串,年龄必须在 10 到 30 之间。但是为什么下面的插入会失败?
rs1:PRIMARY> db.students.insert({age: 11, name: 'Mike'})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
我正在使用db.setProfilingLevel(1, 1000)
为每个数据库设置配置文件级别。我看到可以为每个数据库设置级别,但这slowms
是一个全局状态。有没有办法为每个单独的数据库设置这个值?
如果我不能为每个单独的数据库设置它,为什么有一种方法db.setProfilingLevel(level, slowms)
接受第二个参数作为slowms
. 看起来这个参数是全局值而不是数据库值。令人困惑的是,此方法专门用于设置单个数据库配置文件级别,但由于某些原因,它还在全局设置中设置了 slowms。
我有一个连接到三个分片集群的 mongos。我在这个 mongos 实例中有一些数据库和集合。除了一个有性能问题外,所有其他集合都工作正常。下面命令运行需要45分钟
db.orders.find().explain('executionStats')
此集合中的文档总数为111898400
. 每个分片集群都有关于30000000
文档。我知道我没有针对分片键运行此查询,但我只是想知道这是否是花费这么长时间的原因。我预计最多需要几分钟,但不会超过 45 分钟。
以下是上述命令的执行统计信息。我怎样才能更多地分析它以找出为什么需要这么长时间?
"executionStats" : {
"nReturned" : 111432926,
"executionTimeMillis" : 2727745,
"totalKeysExamined" : 0,
"totalDocsExamined" : 111899059,
"executionStages" : {
"stage" : "SHARD_MERGE",
"nReturned" : 111432926,
"executionTimeMillis" : 2727745,
"totalKeysExamined" : 0,
"totalDocsExamined" : 111899059,
"totalChildMillis" : NumberLong(6550367),
"shards" : [
{
"shardName" : "s0",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 36545358,
"executionTimeMillisEstimate" : 2723505,
"works" : 36613353,
"advanced" : 36545358,
"needTime" : 67994,
"needYield" : 0,
"saveState" : 321990,
"restoreState" : 321990,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 67993,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 36613351,
"executionTimeMillisEstimate" : 2689811,
"works" : 36613353,
"advanced" : 36613351,
"needTime" : 1,
"needYield" : 0,
"saveState" : 321990,
"restoreState" : 321990,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 36613351
}
}
},
{
"shardName" : "s1",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 36423091,
"executionTimeMillisEstimate" : 1647595,
"works" : 36423093,
"advanced" : 36423091,
"needTime" : 1,
"needYield" : 0,
"saveState" : 305586,
"restoreState" : 305586,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 0,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 36423091,
"executionTimeMillisEstimate" : 1614736,
"works" : 36423093,
"advanced" : 36423091,
"needTime" : 1,
"needYield" : 0,
"saveState" : 305586,
"restoreState" : 305586,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 36423091
}
}
},
{
"shardName" : "s2",
"executionSuccess" : true,
"executionStages" : {
"stage" : "SHARDING_FILTER",
"nReturned" : 38464477,
"executionTimeMillisEstimate" : 2166740,
"works" : 38862619,
"advanced" : 38464477,
"needTime" : 398141,
"needYield" : 0,
"saveState" : 331525,
"restoreState" : 331525,
"isEOF" : 1,
"invalidates" : 0,
"chunkSkips" : 398140,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 38862617,
"executionTimeMillisEstimate" : 2128083,
"works" : 38862619,
"advanced" : 38862617,
"needTime" : 1,
"needYield" : 0,
"saveState" : 331525,
"restoreState" : 331525,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 38862617
}
}
}
]
}
},
我有一个分片集群:一个 mongos,三个分片副本集和一个配置副本集。我的应用程序只mongos
与 mongodb 进行读/写通信。我想知道我是否可以制作mongos
复制品。我的意思是,如果 a 发生mongos
故障,我需要 mongos 的一个奴隶成为主要的。有--replSet
参数mongod
但是mongos
没有这样的参数。我怎样才能在mongos中制作它?
我有一个 mongo 分片集群和一个 mongos 实例。这些服务器在 MongoDB 3.4.0 上运行。当我使用 mongo shell 3.4.0 连接到 mongos 时,我得到以下输出sh.isBalancerRunning()
,
mongos> sh.isBalancerRunning()
false
但是当我使用 mongo shell 3.2.13 时得到了不同的结果:
mongos> sh.isBalancerRunning()
true
这是mongo版本之间不兼容的东西吗?如果是,为什么其他命令可以正常工作而这个命令不能?
我正在使用此连接 uri 连接到 mongodb: mongodb://user:password@localhost/admin
。它将admin
用作机器人身份验证和目标数据库。如何使 uriadmin
用作身份验证但允许我连接到不同的数据库?请参阅以下命令作为示例:
mongo --host localhost -u user -p password --authentication admin test
上述命令将用作admin
身份验证数据库但连接到test
数据库。我怎样才能在uri上做同样的事情?
我想在 MongoDB 中设置一个用户。此用户将没有管理员数据库访问权限。但它使用 admin 作为身份验证数据库。此命令将无法连接 mongoDB mongo --host localhost admin
。相反,它可以使用此命令连接到test
数据库:mongo --host localhost --authenticationDatabases admin test
. 在这种情况下如何限制权限?
我尝试了以下命令来创建用户:
db.createUser({user: 'testUser', pwd: '123456', roles: [{role:'readWrite', db: 'SampleCollections'}]})
当我使用该用户帐户登录 mongo shell 时,我可以列出admin
数据库下的集合。我怎样才能将用户限制在SampleCollections
数据库上而不是admin
?
我已经设置了一个 MongoDB 分片集群。它有三个分片副本,每个副本有三个 mongo 实例。它有 3 个 mongo 配置服务器的一个副本。还有一个mongos。一开始它工作正常,但运行几天后配置副本连接失败。当我登录到每个配置服务器 mongo 实例时,以下是命令rs.status()
输出:
配置服务器1:
OTHER> rs.status()
{
"state" : 10,
"stateStr" : "REMOVED",
"uptime" : 121353,
"optime" : {
"ts" : Timestamp(1504367995, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2017-09-02T15:59:55Z"),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
配置服务器 2:
OTHER> rs.status()
{
"state" : 10,
"stateStr" : "REMOVED",
"uptime" : 121421,
"optime" : {
"ts" : Timestamp(1504367995, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2017-09-02T15:59:55Z"),
"ok" : 0,
"errmsg" : "Our replica set config is invalid or we are not a member of it",
"code" : 93,
"codeName" : "InvalidReplicaSetConfig"
}
配置服务器 3:
SECONDARY> rs.status()
{
"set" : "cnf-serv",
"date" : ISODate("2017-09-04T01:45:05.842Z"),
"myState" : 2,
"term" : NumberLong(3),
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1504367995, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1504367995, 1),
"t" : NumberLong(3)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.19.0.10:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 121454,
"optime" : {
"ts" : Timestamp(1504367995, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2017-09-02T15:59:55Z"),
"configVersion" : 403866,
"self" : true
},
{
"_id" : 1,
"name" : "172.19.0.7:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-09-04T01:45:02.312Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "172.19.0.4:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-09-04T01:45:02.310Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
}
],
"ok" : 1
}
看起来前两个实例已被删除,第三个配置服务器是次要的。根据我的理解,如果副本中有一个实例关闭,则应选择另一个健康的实例成为主实例。为什么第三个实例没有成为我的副本中的主实例?
所有 mongo 实例都在使用 version 3.4.4
。
下面是我用来启动 mongod 配置服务器的命令:
mongod --replSet cnf-serv --rest --configsvr --port 27017 --oplogSize 16 --noprealloc --smallfiles
仅供参考,从前两个实例日志中,我看到以下错误消息:
2017-09-04T01:39:23.006+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s
2017-09-04T01:39:53.006+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s
2017-09-04T01:40:23.006+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s
2017-09-04T01:40:53.006+0000 I SHARDING [shard registry reload] Periodic reload of shard registry failed :: caused by :: 134 could not get updated shard list from config server due to Read concern majority reads are currently not possible.; will retry after 30s
我正在 mongodb shell 中的分片集群上运行解释命令。stage
从输出中我看到第一个inputStage
是PROJECTION
. 不太明白这个阶段是什么意思。我在 mongodb 网站上找不到它。这个阶段有定义吗?
"winningPlan" : {
"stage" : "SUBPLAN",
"inputStage" : {
"stage" : "PROJECTION",
"transformBy" : {
"body" : 1,
"headers.Date" : 1
},
"inputStage" : {
"stage" : "SHARDING_FILTER",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "OR",
"inputStages" : [
{
"stage" : "IXSCAN",
"keyPattern" : {
"filename" : 1
},
"indexName" : "filename_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"filename" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"filename" : [
"[\"1046\", \"1046\"]"
]
}
},
{
"stage" : "IXSCAN",
"keyPattern" : {
"headers.From" : 1
},
"indexName" : "headers.From_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"headers.From" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"headers.From" : [
"[\"[email protected]\", \"[email protected]\"]"
]
}
}
]
}
}
}
}
},
"rejectedPlans" : [ ]
}
我使用的查询命令是:
db.testmsg.find(
{
$or: [ { "filename": "1046"}, {"headers.From":"[email protected]"}]
},{
"body": 1, "headers.Date": 1
}).explain();
我已经设置了一个分片集群,但我的配置服务器副本出现了一些错误。以下是我的配置服务器副本状态:
cnf-serv:SECONDARY> rs.status()
{
"set" : "cnf-serv",
"date" : ISODate("2017-08-21T01:28:00.926Z"),
"myState" : 2,
"term" : NumberLong(1),
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1502320369, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1502320369, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.19.0.13:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-21T01:28:00.831Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "172.19.0.4:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-08-21T01:28:00.831Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "172.19.0.7:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 119601,
"optime" : {
"ts" : Timestamp(1502320369, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-08-09T23:12:49Z"),
"configVersion" : 185982,
"self" : true
}
],
"ok" : 1
}
我可以从上面的命令中看到两个副本成员不可访问,唯一可访问的成员是辅助成员。所以我需要让次要成员成为主要成员,以使配置副本正常工作。我在辅助中尝试了以下命令但没有用:
cnf-serv:SECONDARY> rs.slaveOk()
cnf-serv:SECONDARY> var cfg = rs.config();
cnf-serv:SECONDARY> cfg.members[0].priority=0.5
0.5
cnf-serv:SECONDARY> cfg.members[1].priority=0.5
0.5
cnf-serv:SECONDARY> cfg.members[2].priority=1
1
cnf-serv:SECONDARY> rs.reconfig(cfg, {force: true})
{ "ok" : 1 }
进行此更改后,次级仍然是次级。它没有成为主要的。我该如何解决这个副本集问题?
我有一个非常大的转储数据(18GB)需要恢复。我尝试使用mongorestore
将数据恢复到副本集。但由于error restoring from orders.bson: insertion error: not master
. 我认为这是因为 master 在副本集中发生了变化。有没有办法让我恢复数据并停止更改副本集角色的主节点?
我已经设置了一个副本集,并希望将其添加到 mongos 中。但我在 mongos 中遇到以下错误:
mongos> sh.addShard("rs3/172.19.0.12:27017,172.19.0.6:27017,172.19.0.5:27017")
{
"code" : 11000,
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: admin.system.version index: _id_ dup key: { : \"shardIdentity\" }"
}
从上面的消息中,它说admin.system.version
有重复的键错误。集合admin.system.version
应该由 mongodb 设置。我不明白为什么它有一个重复的键。
所有 mongo 实例都是 3.4.4 版本。
以下是副本集状态:
rs3:PRIMARY> rs.status()
{
"set" : "rs3",
"date" : ISODate("2017-07-09T02:13:37.146Z"),
"myState" : 1,
"term" : NumberLong(10),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1499566407, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1499563905, 1),
"t" : NumberLong(9)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.19.0.12:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2493,
"optime" : {
"ts" : Timestamp(1499566407, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2017-07-09T02:13:27Z"),
"electionTime" : Timestamp(1499563936, 1),
"electionDate" : ISODate("2017-07-09T01:32:16Z"),
"configVersion" : 414750,
"self" : true
},
{
"_id" : 1,
"name" : "172.19.0.5:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1403,
"optime" : {
"ts" : Timestamp(1499566407, 1),
"t" : NumberLong(10)
},
"optimeDurable" : {
"ts" : Timestamp(1499563905, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2017-07-09T02:13:27Z"),
"optimeDurableDate" : ISODate("2017-07-09T01:31:45Z"),
"lastHeartbeat" : ISODate("2017-07-09T02:13:35.870Z"),
"lastHeartbeatRecv" : ISODate("2017-07-09T02:13:35.854Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.19.0.12:27017",
"configVersion" : 414750
},
{
"_id" : 2,
"name" : "172.19.0.6:27017",
"health" : 1,
"state" : 3,
"stateStr" : "RECOVERING",
"uptime" : 2487,
"optime" : {
"ts" : Timestamp(1499070510, 1000),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1499070510, 1000),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2017-07-03T08:28:30Z"),
"optimeDurableDate" : ISODate("2017-07-03T08:28:30Z"),
"lastHeartbeat" : ISODate("2017-07-09T02:13:35.865Z"),
"lastHeartbeatRecv" : ISODate("2017-07-09T02:13:36.965Z"),
"pingMs" : NumberLong(0),
"configVersion" : 414750
}
],
"ok" : 1
}