-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdb.createUser.txt
412 lines (271 loc) · 10.4 KB
/
db.createUser.txt
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
===============
db.createUser()
===============
.. default-domain:: mongodb
.. facet::
:name: programming_language
:values: shell
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.createUser(user, writeConcern)
Creates a new user for the database on which the method is run.
:method:`db.createUser()` returns a *duplicate user* error if the
user already exists on the database.
.. |dbcommand| replace:: :dbcommand:`createUser` command
.. include:: /includes/fact-mongosh-shell-method-alt.rst
The :method:`db.createUser()` method has the following syntax:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``user``
- document
- The document with authentication and access information about the
user to create.
* - ``writeConcern``
- document
- .. include:: /includes/fact-write-concern-spec-link.rst
The ``user`` document defines the user and has the following form:
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
{
user: "<name>",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { <any information> },
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
],
authenticationRestrictions: [
{
clientSource: ["<IP>" | "<CIDR range>", ...],
serverAddress: ["<IP>" | "<CIDR range>", ...]
},
...
],
mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
passwordDigestor: "<server|client>"
}
The ``user`` document has the following fields:
.. |local-cmd-name| replace:: :method:`db.createUser()`
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``user``
- string
- The name of the new user.
* - ``pwd``
- string
- The user's password. The ``pwd`` field is not
required if you run |local-cmd-name| on the ``$external``
database to create users who have credentials stored externally to
MongoDB.
The value can be either:
- the user's password in cleartext string, or
- :method:`passwordPrompt()` to prompt for the user's password.
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
* - ``customData``
- document
- Optional. Any arbitrary information. This field can be used to store any data
an admin wishes to associate with this particular user. For example,
this could be the user's full name or employee id.
* - ``roles``
- array
- The roles granted to the user. Can specify an empty array ``[]`` to
create users without roles.
* - :ref:`authenticationRestrictions <db-createUser-authenticationRestrictions>`
- array
- .. _db-createUser-authenticationRestrictions:
Optional. The authentication restrictions the server enforces on the created
user. Specifies a list of IP addresses and
:abbr:`CIDR (Classless Inter-Domain Routing)` ranges from which the
user is allowed to connect to the server or from which the server can
accept users.
* - ``mechanisms``
- array
- Optional. Specify the specific SCRAM mechanism or mechanisms for creating
SCRAM user credentials. If :parameter:`authenticationMechanisms` is
specified, you can only specify a subset of the
:parameter:`authenticationMechanisms`.
Valid values are:
- ``"SCRAM-SHA-1"``
- Uses the ``SHA-1`` hashing function.
- ``"SCRAM-SHA-256"``
- Uses the ``SHA-256`` hashing function.
- Requires passwordDigestor to be ``server``.
The default is both ``SCRAM-SHA-1`` and ``SCRAM-SHA-256``.
* - ``passwordDigestor``
- string
- Optional. Indicates whether the server or the client digests the password.
Available values are:
- ``"server"`` (Default)
The server receives undigested password from the client and
digests the password.
- ``"client"`` (Not compatible with ``SCRAM-SHA-256``)
The client digests the password and passes the digested
password to the server.
Compatibility
-------------
This method is available in deployments hosted in the following
environments:
.. include:: /includes/fact-environments-no-atlas-support.rst
.. include:: /includes/fact-environments-onprem-only.rst
Roles
~~~~~
.. include:: /includes/fact-roles-array-contents.rst
.. _method-createUser-authentication-restrictions:
Authentication Restrictions
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: /includes/fact-auth-restrictions-array-contents.rst
The :method:`db.createUser()` method wraps the :dbcommand:`createUser`
command.
Behavior
--------
User ID
~~~~~~~
MongoDB automatically assigns a unique ``userId`` to the user upon creation.
Replica set
~~~~~~~~~~~
.. |command| replace:: :method:`db.createUser()`
.. include:: /includes/fact-management-methods-write-concern.rst
Encryption
~~~~~~~~~~
.. include:: /includes/fact-cleartext-passwords-tls.rst
External Credentials
~~~~~~~~~~~~~~~~~~~~
Users created on the ``$external`` database should have credentials
stored externally to MongoDB, as, for example, with :doc:`MongoDB
Enterprise installations that use Kerberos
</tutorial/control-access-to-mongodb-with-kerberos-authentication>`.
.. include:: /includes/extracts/sessions-external-username-limit.rst
``local`` Database
~~~~~~~~~~~~~~~~~~
You cannot create users on the local database.
Required Access
---------------
.. include:: /includes/access-create-user.rst
Examples
--------
The following :method:`db.createUser()` operation creates the
``accountAdmin01`` user on the ``products`` database.
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use products
db.createUser( { user: "accountAdmin01",
pwd: passwordPrompt(), // Or "<cleartext password>"
customData: { employeeId: 12345 },
roles: [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"] },
{ w: "majority" , wtimeout: 5000 } )
The operation gives ``accountAdmin01`` the following roles:
- the ``clusterAdmin`` and ``readAnyDatabase`` roles on the ``admin``
database
- the ``readWrite`` role on the ``products`` database
Create User with Roles
~~~~~~~~~~~~~~~~~~~~~~
The following operation creates ``accountUser`` in the ``products`` database
and gives the user the ``readWrite`` and ``dbAdmin`` roles.
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use products
db.createUser(
{
user: "accountUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ "readWrite", "dbAdmin" ]
}
)
Create User without Roles
~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation creates a user named ``reportsUser`` in the ``admin``
database but does not yet assign roles:
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use admin
db.createUser(
{
user: "reportsUser",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ ]
}
)
Create Administrative User with Roles
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation creates a user named ``appAdmin`` in the
``admin`` database and gives the user ``readWrite`` access to the
``config`` database, which lets the user change certain settings for
sharded clusters, such as to the balancer setting.
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use admin
db.createUser(
{
user: "appAdmin",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles:
[
{ role: "readWrite", db: "config" },
"clusterAdmin"
]
}
)
Create User with Authentication Restrictions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation creates a user named ``restricted`` in the
``admin`` database. This user may only authenticate if connecting from
IP address ``192.0.2.0`` to IP address ``198.51.100.0``.
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use admin
db.createUser(
{
user: "restricted",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ { role: "readWrite", db: "reporting" } ],
authenticationRestrictions: [ {
clientSource: ["192.0.2.0"],
serverAddress: ["198.51.100.0"]
} ]
}
)
Create User with ``SCRAM-SHA-256`` Credentials Only
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. note::
To use :ref:`SCRAM-SHA-256 <authentication-scram-sha-256>`, the
``featureCompatibilityVersion`` must be set to ``4.0``. For more
information on featureCompatibilityVersion, see :ref:`view-fcv` and
:dbcommand:`setFeatureCompatibilityVersion`.
The following operation creates a user with only ``SCRAM-SHA-256``
credentials.
.. tip::
.. include:: /includes/extracts/4.2-changes-passwordPrompt.rst
.. code-block:: javascript
use reporting
db.createUser(
{
user: "reportUser256",
pwd: passwordPrompt(), // Or "<cleartext password>"
roles: [ { role: "readWrite", db: "reporting" } ],
mechanisms: [ "SCRAM-SHA-256" ]
}
)
If the :parameter:`authenticationMechanisms` parameter is set, the
``mechanisms`` field can only include values specified in the
:parameter:`authenticationMechanisms` parameter.