|
| 1 | +title: Connect and authenticate |
| 2 | +level: 4 |
| 3 | +stepnum: 1 |
| 4 | +ref: auth-as-admin |
| 5 | +content: | |
| 6 | + Using :binary:`~bin.mongosh`, connect to your primary |
| 7 | + :binary:`~bin.mongod` or, in a sharded cluster, connect to your |
| 8 | + :binary:`~bin.mongos` and authenticate as a user administrator or a |
| 9 | + user with the :ref:`required privileges <add-user-prereq>`: |
| 10 | +
|
| 11 | + .. tabs:: |
| 12 | +
|
| 13 | + tabs: |
| 14 | + - id: cmdline |
| 15 | + name: Authenticate during Connection |
| 16 | + content: | |
| 17 | + Start :binary:`~bin.mongosh` with the :option:`-u |
| 18 | + \<username\> <mongosh -u>`, :option:`-p <mongosh -p>`, and the |
| 19 | + :option:`--authenticationDatabase \<database\> <mongo |
| 20 | + --authenticationDatabase>` command line options: |
| 21 | +
|
| 22 | + .. code-block:: bash |
| 23 | +
|
| 24 | + mongosh --port 27017 --authenticationDatabase \ |
| 25 | + "admin" -u "myUserAdmin" -p |
| 26 | +
|
| 27 | + Enter your password when prompted. |
| 28 | +
|
| 29 | + - id: authafter |
| 30 | + name: Authenticate after Connection |
| 31 | + content: | |
| 32 | +
|
| 33 | + Using :binary:`~bin.mongosh`, connect to your database |
| 34 | + deployment: |
| 35 | +
|
| 36 | + .. code-block:: bash |
| 37 | +
|
| 38 | + mongosh --port 27017 |
| 39 | +
|
| 40 | + In :binary:`~bin.mongosh`, switch to the |
| 41 | + authentication database (in this case, ``admin``), and |
| 42 | + use the :method:`db.auth(\<username\>, \<pwd\>) |
| 43 | + <db.auth()>` method to authenticate: |
| 44 | +
|
| 45 | + .. code-block:: javascript |
| 46 | +
|
| 47 | + use admin |
| 48 | + db.auth("myUserAdmin", passwordPrompt()) // or cleartext password |
| 49 | +
|
| 50 | + .. tip:: |
| 51 | +
|
| 52 | + .. include:: /includes/extracts/mongosh-password-prompt.rst |
| 53 | +
|
| 54 | + Enter the password when prompted. |
| 55 | +--- |
| 56 | +title: Create additional users for your deployment |
| 57 | +level: 4 |
| 58 | +stepnum: 2 |
| 59 | +ref: create-additionalusers |
| 60 | +pre: | |
| 61 | +
|
| 62 | + .. note:: |
| 63 | +
|
| 64 | + The following step uses :ref:`authentication-scram` authentication. |
| 65 | + For additional information on other authentication mechanisms, see |
| 66 | + :ref:`create-users-examples`. |
| 67 | +
|
| 68 | + After authenticating as the user administrator, use the |
| 69 | + :method:`db.createUser()` method to create additional users. You can assign |
| 70 | + any :doc:`built-in roles </reference/built-in-roles>` or |
| 71 | + :doc:`user-defined roles </core/security-user-defined-roles>` to the |
| 72 | + users. |
| 73 | +
|
| 74 | +action: |
| 75 | + pre: | |
| 76 | + The following operation adds a user ``myTester`` to the ``test`` |
| 77 | + database who has the :authrole:`readWrite` role in the ``test`` |
| 78 | + database as well as the :authrole:`read` role in the ``reporting`` |
| 79 | + database. |
| 80 | +
|
| 81 | + language: javascript |
| 82 | + code: | |
| 83 | + use test |
| 84 | + db.createUser( |
| 85 | + { |
| 86 | + user: "myTester", |
| 87 | + pwd: passwordPrompt(), // or cleartext password |
| 88 | + roles: [ { role: "readWrite", db: "test" }, |
| 89 | + { role: "read", db: "reporting" } ] |
| 90 | + } |
| 91 | + ) |
| 92 | +
|
| 93 | +post: | |
| 94 | +
|
| 95 | + .. tip:: |
| 96 | +
|
| 97 | + .. include:: /includes/extracts/mongosh-password-prompt.rst |
| 98 | +
|
| 99 | + The database where you create the user (in this example, ``test``) is |
| 100 | + that user's :ref:`authentication database |
| 101 | + <user-authentication-database>`. Although the user authenticates to |
| 102 | + this database, the user can have roles in other databases. The |
| 103 | + user's authentication database does not limit the user's privileges. |
| 104 | +
|
| 105 | + After creating the additional users, exit :binary:`~bin.mongosh`. |
| 106 | +
|
| 107 | +--- |
| 108 | +title: Connect to the instance and authenticate as ``myTester`` |
| 109 | +level: 4 |
| 110 | +ref: auth-as-mytester |
| 111 | +content: | |
| 112 | +
|
| 113 | + .. important:: |
| 114 | +
|
| 115 | + It is not possible to switch between users in the same |
| 116 | + :binary:`~bin.mongosh` session. Authenticating as a different user |
| 117 | + means the session has the privileges of **both** authenticated |
| 118 | + users. To switch between users exit and relaunch |
| 119 | + :binary:`~bin.mongosh`. |
| 120 | +
|
| 121 | + After exiting :binary:`~bin.mongosh` as ``myUserAdmin``, reconnect as |
| 122 | + ``myTester``: |
| 123 | +
|
| 124 | + .. tabs:: |
| 125 | +
|
| 126 | + tabs: |
| 127 | + - id: cmdline2 |
| 128 | + name: Authenticate during Connection |
| 129 | + content: | |
| 130 | + Start :binary:`~bin.mongosh` with the :option:`-u |
| 131 | + \<username\> <mongosh --username>`, :option:`-p <mongosh -p>`, and the |
| 132 | + :option:`--authenticationDatabase \<database\> <mongo |
| 133 | + --authenticationDatabase>` command line options: |
| 134 | +
|
| 135 | + .. code-block:: bash |
| 136 | +
|
| 137 | + mongosh --port 27017 -u "myTester" \ |
| 138 | + --authenticationDatabase "test" -p |
| 139 | +
|
| 140 | + Enter the password for the user when prompted. |
| 141 | +
|
| 142 | + - id: authafter2 |
| 143 | + name: Authenticate after Connection |
| 144 | + content: | |
| 145 | +
|
| 146 | + Using :binary:`~bin.mongosh`, connect to your database |
| 147 | + deployment: |
| 148 | +
|
| 149 | + .. code-block:: bash |
| 150 | +
|
| 151 | + mongosh --port 27017 |
| 152 | +
|
| 153 | + In :binary:`~bin.mongosh`, switch to the |
| 154 | + authentication database (in this case, ``admin``), and |
| 155 | + use the :method:`db.auth(\<username\>, \<pwd\>) |
| 156 | + <db.auth()>` method to authenticate: |
| 157 | +
|
| 158 | + .. code-block:: javascript |
| 159 | +
|
| 160 | + use test |
| 161 | + db.auth("myTester", passwordPrompt()) // or cleartext password |
| 162 | +
|
| 163 | + .. tip:: |
| 164 | +
|
| 165 | + .. include:: /includes/extracts/mongosh-password-prompt.rst |
| 166 | +
|
| 167 | + Enter the password for the user when prompted. |
| 168 | +--- |
| 169 | +title: Insert a document as ``myTester`` |
| 170 | +level: 4 |
| 171 | +ref: insert-as-mytester |
| 172 | +content: | |
| 173 | +
|
| 174 | + As the user ``myTester``, you have privileges to perform read and |
| 175 | + write operations in the ``test`` database (as well as perform read |
| 176 | + operations in the ``reporting`` database). Once authenticated as |
| 177 | + ``myTester``, insert a document into a collection in the ``test`` |
| 178 | + database. For example, you can perform the following insert |
| 179 | + operation in the ``test`` database: |
| 180 | +
|
| 181 | + .. code-block:: javascript |
| 182 | +
|
| 183 | + db.foo.insert( { x: 1, y: 1 } ) |
| 184 | +... |
0 commit comments