代码改变世界

mongodb报错not authorized on testdb to execute command

  abce  阅读(1679)  评论(0编辑  收藏  举报

用户反馈说没有权限,报错如下:

1
not authorized on testdb to execute command { find: "Schedule", filter: {}, skip: 0, limit: 20, maxTimeMS: 60000, lsid: { id: UUID("41a5218c-071f-41c4-9b06-a28fb60f8015") }, $db: "testdb" }

查看用户的权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> db.getUser('test_user')
{
        "_id" : "admin.test_user",
        "userId" : UUID("c041ed33-2dba-477c-8bb3-091955937f66"),
        "user" : "test_user",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "dbAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
>

角色dbAdminAnyDatabase与dbAdmin含有相同的权限,除了local和config库。从5.0开始,还包含了applyOps的权限。
查看一下该角色都有哪些特权:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
> db.getRole("dbAdminAnyDatabase", { showPrivileges: true })
{
        "db" : "admin",
        "role" : "dbAdminAnyDatabase",
        "roles" : [ ],
        "privileges" : [
                {
                        "resource" : {
                                "cluster" : true
                        },
                        "actions" : [
                                "listDatabases",
                                "applyOps"
                        ]
                },
                {
                        "resource" : {
                                "db" : "",
                                "collection" : ""
                        },
                        "actions" : [
                                "bypassDocumentValidation",
                                "collMod",
                                "collStats",
                                "compact",
                                "convertToCapped",
                                "createCollection",
                                "createIndex",
                                "dbStats",
                                "dropCollection",
                                "dropDatabase",
                                "dropIndex",
                                "enableProfiler",
                                "listCollections",
                                "listIndexes",
                                "planCacheIndexFilter",
                                "planCacheRead",
                                "planCacheWrite",
                                "reIndex",
                                "renameCollectionSameDB",
                                "storageDetails",
                                "validate"
                        ]
                },
                {
                        "resource" : {
                                "db" : "",
                                "collection" : "system.profile"
                        },
                        "actions" : [
                                "changeStream",
                                "collStats",
                                "convertToCapped",
                                "createCollection",
                                "dbHash",
                                "dbStats",
                                "dropCollection",
                                "find",
                                "killCursors",
                                "listCollections",
                                "listIndexes",
                                "planCacheRead"
                        ]
                },
                {
                        "resource" : {
                                "system_buckets" : ""
                        },
                        "actions" : [
                                "bypassDocumentValidation",
                                "collMod",
                                "collStats",
                                "compact",
                                "convertToCapped",
                                "createCollection",
                                "createIndex",
                                "dbStats",
                                "dropCollection",
                                "dropDatabase",
                                "dropIndex",
                                "enableProfiler",
                                "listCollections",
                                "listIndexes",
                                "planCacheIndexFilter",
                                "planCacheRead",
                                "planCacheWrite",
                                "reIndex",
                                "renameCollectionSameDB",
                                "storageDetails",
                                "validate"
                        ]
                }
        ],
        "inheritedRoles" : [ ],
        "inheritedPrivileges" : [
                {
                        "resource" : {
                                "cluster" : true
                        },
                        "actions" : [
                                "listDatabases",
                                "applyOps"
                        ]
                },
                {
                        "resource" : {
                                "db" : "",
                                "collection" : ""
                        },
                        "actions" : [
                                "bypassDocumentValidation",
                                "collMod",
                                "collStats",
                                "compact",
                                "convertToCapped",
                                "createCollection",
                                "createIndex",
                                "dbStats",
                                "dropCollection",
                                "dropDatabase",
                                "dropIndex",
                                "enableProfiler",
                                "listCollections",
                                "listIndexes",
                                "planCacheIndexFilter",
                                "planCacheRead",
                                "planCacheWrite",
                                "reIndex",
                                "renameCollectionSameDB",
                                "storageDetails",
                                "validate"
                        ]
                },
                {
                        "resource" : {
                                "db" : "",
                                "collection" : "system.profile"
                        },
                        "actions" : [
                                "changeStream",
                                "collStats",
                                "convertToCapped",
                                "createCollection",
                                "dbHash",
                                "dbStats",
                                "dropCollection",
                                "find",
                                "killCursors",
                                "listCollections",
                                "listIndexes",
                                "planCacheRead"
                        ]
                },
                {
                        "resource" : {
                                "system_buckets" : ""
                        },
                        "actions" : [
                                "bypassDocumentValidation",
                                "collMod",
                                "collStats",
                                "compact",
                                "convertToCapped",
                                "createCollection",
                                "createIndex",
                                "dbStats",
                                "dropCollection",
                                "dropDatabase",
                                "dropIndex",
                                "enableProfiler",
                                "listCollections",
                                "listIndexes",
                                "planCacheIndexFilter",
                                "planCacheRead",
                                "planCacheWrite",
                                "reIndex",
                                "renameCollectionSameDB",
                                "storageDetails",
                                "validate"
                        ]
                }
        ],
        "isBuiltin" : true
}

授权

1
db.grantRolesToUser("test_user", [ { role: "readWriteAnyDatabase", db: "admin" } ])

再次查看用户的权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> db.getUser('test_user')
{
        "_id" : "admin.test_user",
        "userId" : UUID("c041ed33-2dba-477c-8bb3-091955937f66"),
        "user" : "test_user",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "dbAdminAnyDatabase",
                        "db" : "admin"
                },
                {
                        "role" : "readWriteAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}
>

  

现在就可以插入和查看数据了。

 

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-08-31 【MySQL】MariaDB使用connect存储引擎访问SQLServer中的表
2021-08-31 Write Set In MySQL 5.7: Group Replication
点击右上角即可分享
微信分享提示