|
| 1 | +.. _laravel-connect-to-mongodb: |
| 2 | + |
| 3 | +================ |
| 4 | +Connection Guide |
| 5 | +================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, seedlist, dsn, data source name |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to connect your Laravel application to a |
| 24 | +MongoDB instance or replica set deployment by using {+odm-short+}. |
| 25 | + |
| 26 | +This guide includes the following sections: |
| 27 | + |
| 28 | +- :ref:`Connection URI <laravel-connection-uri>`, which explains connection |
| 29 | + URIs and their constituent parts |
| 30 | +- :ref:`laravel-database-config`, which explains how to set up your MongoDB |
| 31 | + database connection for your Laravel app. |
| 32 | +- :ref:`Connection Example <laravel-atlas-connection-example>`, which provides |
| 33 | + examples that show how to connect to MongoDB by using an Atlas connection |
| 34 | + string. |
| 35 | +- :ref:`laravel-other-ways-to-connect` describes ways to connect to MongoDB |
| 36 | + deployments that are not hosted on Atlas. |
| 37 | + |
| 38 | +.. _laravel-connection-uri: |
| 39 | + |
| 40 | +Connection URI |
| 41 | +-------------- |
| 42 | + |
| 43 | +A **connection URI**, also known as a connection string, specifies how |
| 44 | +{+odm-short+} connects to MongoDB and how to behave while connected. |
| 45 | + |
| 46 | +Parts of a Connection URI |
| 47 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +The following figure explains each part of a sample connection URI: |
| 50 | + |
| 51 | +.. figure:: /includes/figures/connection_uri_parts.png |
| 52 | + :alt: Parts of a connection URI |
| 53 | + |
| 54 | +In this connection URI, ``mongodb+srv`` is the protocol, which uses the |
| 55 | +:manual:`DNS Seed List Connection Format </reference/connection-string/#dns-seed-list-connection-format>` |
| 56 | +for greater flexibility in your deployment and the ability to change the |
| 57 | +servers in rotation without reconfiguring clients. |
| 58 | + |
| 59 | +If the machine that hosts your MongoDB deployment does not support this |
| 60 | +feature, use protocol for the |
| 61 | +:manual:`Standard Connection String Format </reference/connection-string/#std-label-connections-standard-connection-string-format>` |
| 62 | +instead. |
| 63 | + |
| 64 | +If you use a password-based authentication, the part of the connection |
| 65 | +string after the protocol contains your username and password. Replace the |
| 66 | +placeholder for ``user`` with your username and ``pass`` with your password. |
| 67 | +If you use an authentication mechanism that does not require a username |
| 68 | +and password, omit this part of the connection URI. |
| 69 | + |
| 70 | +The part of the connection string after the credentials specifies your MongoDB |
| 71 | +instance's hostname or IP address and port. The preceding example uses |
| 72 | +``sample.host`` as the hostname and ``27017`` as the port. Replace these values |
| 73 | +to point to your MongoDB instance. |
| 74 | + |
| 75 | +The last part of the connection string specifies connection and authentication |
| 76 | +options. In the example, we set the following connection options and values: |
| 77 | + |
| 78 | +- ``maxPoolSize=20`` |
| 79 | +- ``w=majority`` |
| 80 | + |
| 81 | +.. _laravel-database-config: |
| 82 | + |
| 83 | +Laravel Database Connection Configuration |
| 84 | +------------------------------------------ |
| 85 | + |
| 86 | +{+odm-short+} lets you configure your MongoDB database connection in the |
| 87 | +``config/database.php`` Laravel application file. You can specify the following |
| 88 | +connection details in this file: |
| 89 | + |
| 90 | +- ``default``, which specifies the database connection to use when unspecified |
| 91 | +- ``connections``, which contains database connection information to access |
| 92 | + one or more databases from your application |
| 93 | + |
| 94 | +You can use the following code in the configuration file to set the default |
| 95 | +connection to a corresponding ``mongodb`` entry in the ``connections`` array: |
| 96 | + |
| 97 | +.. code-block:: php |
| 98 | + |
| 99 | + 'default' => 'mongodb', |
| 100 | + |
| 101 | +For a MongoDB database connection, you can specify the following details: |
| 102 | + |
| 103 | +.. list-table:: |
| 104 | + :header-rows: 1 |
| 105 | + :widths: 25 75 |
| 106 | + |
| 107 | + * - Setting |
| 108 | + - Description |
| 109 | + |
| 110 | + * - ``driver`` |
| 111 | + - Specifies the database driver to use for the connection. |
| 112 | + |
| 113 | + * - ``dsn`` |
| 114 | + - The data source name (DSN) that specifies the MongoDB connection URI. |
| 115 | + |
| 116 | + * - ``host`` |
| 117 | + - | Specifies the network address and port of one or more MongoDB nodes |
| 118 | + in a deployment. You can use this setting instead of the ``dsn`` |
| 119 | + setting. |
| 120 | + | To specify a single host, pass the hostname and port as a string as |
| 121 | + shown in the following example: |
| 122 | + |
| 123 | + .. code-block:: php |
| 124 | + :copyable: false |
| 125 | + |
| 126 | + 'host' => 'myhost.example.com:27017', |
| 127 | + |
| 128 | + | To specify multiple hosts, pass them in an array as shown in the |
| 129 | + following example:: |
| 130 | + |
| 131 | + .. code-block:: php |
| 132 | + :copyable: false |
| 133 | + |
| 134 | + 'host' => ['node1.example.com:27017', 'node2.example.com:27017', 'node3.example.com:27017'], |
| 135 | + |
| 136 | + .. note:: |
| 137 | + |
| 138 | + This option does not accept hosts that use the DNS seedlist |
| 139 | + connection format. |
| 140 | + |
| 141 | + * - ``database`` |
| 142 | + - Specifies the name of the MongoDB database to read and write to. |
| 143 | + |
| 144 | + * - ``username`` |
| 145 | + - Specifies your database user's username credential to authenticate |
| 146 | + with MongoDB. |
| 147 | + |
| 148 | + * - ``password`` |
| 149 | + - Specifies your database user's password credential to authenticate |
| 150 | + with MongoDB. |
| 151 | + |
| 152 | + * - ``options`` |
| 153 | + - Specifies connection options to pass to MongoDB that determine the |
| 154 | + connection behavior. |
| 155 | + |
| 156 | + * - ``driverOptions`` |
| 157 | + - Specifies options specific to pass to the MongoDB PHP Library driver |
| 158 | + that determine the driver behavior for that connection. |
| 159 | + |
| 160 | +.. note:: |
| 161 | + |
| 162 | + You can specify the following settings in the ``dsn`` configuration |
| 163 | + as parameters in your MongoDB connection string instead of as array items: |
| 164 | + |
| 165 | + - ``host`` |
| 166 | + - ``username`` |
| 167 | + - ``password`` |
| 168 | + - ``options`` and ``driverOptions``, which are specified by the option name |
| 169 | + |
| 170 | +The following example shows how you can specify your MongoDB connection details |
| 171 | +in the ``connections`` array item: |
| 172 | + |
| 173 | +.. code-block:: php |
| 174 | + :caption: Example config/database.php MongoDB connection configuration |
| 175 | + |
| 176 | + 'connections' => [ |
| 177 | + 'mongodb' => [ |
| 178 | + 'driver' => 'mongodb', |
| 179 | + 'dsn' => 'mongodb+srv//myUser: [email protected]:27017/', |
| 180 | + 'database' => 'sample_mflix', |
| 181 | + 'options' => [ |
| 182 | + 'maxPoolSize' => 20, |
| 183 | + 'w' => 'majority', |
| 184 | + ], |
| 185 | + 'driverOptions' => [ |
| 186 | + 'serverApi' => 1, |
| 187 | + ], |
| 188 | + ], |
| 189 | + // ... |
| 190 | + ], |
| 191 | + |
| 192 | +The following sections provide common ways of specifying MongoDB connections. |
| 193 | + |
| 194 | +.. _laravel-atlas-connection-example: |
| 195 | + |
| 196 | +Connection Example |
| 197 | +------------------ |
| 198 | + |
| 199 | +This section shows how to configure your Laravel application's DSN by using a |
| 200 | +MongoDB Atlas connection string. |
| 201 | + |
| 202 | +To add your MongoDB DSN to your Laravel application, make the following changes: |
| 203 | + |
| 204 | +- Add the DSN as an environment variable in your project's ``.env`` environment |
| 205 | + configuration file. Set the variable value to your Atlas connection string. |
| 206 | +- Add a connection entry for your MongoDB connection in the ``connections`` |
| 207 | + array of your ``config/database.php`` configuration file. Set the ``dsn`` |
| 208 | + value of the connection entry to reference the environment variable that |
| 209 | + contains your DSN. |
| 210 | + |
| 211 | +The following examples show how to specify ``"mongodb+srv://myUser: [email protected]/"`` |
| 212 | +as the connection string in the relevant configuration files: |
| 213 | + |
| 214 | +.. code-block:: bash |
| 215 | + :caption: Sample .env environment configuration |
| 216 | + |
| 217 | + DB_URI="mongodb+srv://myUser: [email protected]/" |
| 218 | + |
| 219 | +.. code-block:: php |
| 220 | + :caption: Sample config/database.php connection entry |
| 221 | + :emphasize-lines: 3 |
| 222 | + |
| 223 | + 'connections' => [ |
| 224 | + 'mongodb' => [ |
| 225 | + 'dsn' => env('DB_URI'), // uses the value of the DB_URI environment variable |
| 226 | + 'driver' => 'mongodb', |
| 227 | + 'database' => 'sample_mflix', |
| 228 | + // ... |
| 229 | + ], |
| 230 | + // ... |
| 231 | + ] |
| 232 | + |
| 233 | +.. tip:: |
| 234 | + |
| 235 | + To retrieve your Atlas connection string, follow the |
| 236 | + :ref:`Create a Connection String <laravel-quick-start-connection-string>` |
| 237 | + step of the Quick Start tutorial. |
| 238 | + |
| 239 | +.. _laravel-other-ways-to-connect: |
| 240 | + |
| 241 | +Other Ways to Connect to MongoDB |
| 242 | +-------------------------------- |
| 243 | + |
| 244 | +The following sections show you how to connect to a single MongoDB server |
| 245 | +instance or a replica set not hosted on MongoDB Atlas. |
| 246 | + |
| 247 | +.. _laravel-connect-localhost: |
| 248 | + |
| 249 | +Connect to a MongoDB Server on Your Local Machine |
| 250 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 251 | + |
| 252 | +This section shows an example connection string you can use when running a |
| 253 | +Laravel application and MongoDB server from the same machine, such as your |
| 254 | +local development environment. |
| 255 | + |
| 256 | +To connect your application to a MongoDB instance hosted on the same machine, |
| 257 | +you must complete the following tasks: |
| 258 | + |
| 259 | +- Download, install, and run the MongoDB server. |
| 260 | +- Obtain the IP address and port on which your MongoDB server is running. If |
| 261 | + you use the default settings of a local installation of MongoDB server, |
| 262 | + the IP address is ``127.0.0.1``, and the port is ``27017``. |
| 263 | +- Set up your ``config/database.php`` connection to reference the environment |
| 264 | + variable ``DB_URI`` for the value of the ``dsn``, as shown in the |
| 265 | + :ref:`laravel-atlas-connection-example` section. |
| 266 | + |
| 267 | +The following example shows a sample connection string that you can add to the |
| 268 | +``.env`` file if your application connects to a MongoDB server running on the |
| 269 | +default IP address and port: |
| 270 | + |
| 271 | +.. code-block:: php |
| 272 | + :caption: Sample .env environment configuration to connect to a local MongoDB server. |
| 273 | + |
| 274 | + DB_URI="mongodb://127.0.0.1:27017/"; |
| 275 | + |
| 276 | +To learn how to download and install MongoDB server, see |
| 277 | +:manual:`Install MongoDB Community Edition </installation/#mongodb-installation-tutorials>` |
| 278 | +in the {+server-docs-name+}. |
| 279 | + |
| 280 | +.. _laravel-connect-replica-set: |
| 281 | + |
| 282 | +Connect to a Replica Set |
| 283 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 284 | + |
| 285 | +A MongoDB replica set deployment is a group of connected instances, or nodes, |
| 286 | +where the nodes store the same data set. This configuration of instances |
| 287 | +provides data redundancy and high data availability. |
| 288 | + |
| 289 | +To connect to a replica set deployment, specify each node's hostname and port |
| 290 | +number, separated by commas, and the replica set name as the value of the |
| 291 | +``replicaSet`` parameter in the connection string. |
| 292 | + |
| 293 | +This example, which shows the connection string you can add to your |
| 294 | +Laravel application's ``.env`` file to connect to a replica set, uses the |
| 295 | +following sample values: |
| 296 | + |
| 297 | +- ``host1``, ``host2``, and ``host3`` as the hostnames of the MongoDB nodes |
| 298 | +- ``27017`` as the port on which MongoDB runs on those hosts |
| 299 | +- ``myRS`` as the configured name of the replica set |
| 300 | +- ``myUser`` and ``myPass123`` as the credentials of a database user |
| 301 | + |
| 302 | +.. code-block:: bash |
| 303 | + |
| 304 | + DB_URI="mongodb://myUser:myPass123@host1:27017,host2:27017,host3:27017/?replicaSet=myRS" |
| 305 | + |
| 306 | +When connecting to a replica set, the library that {+odm-short+} uses to manage |
| 307 | +connections with MongoDB performs the following actions unless otherwise |
| 308 | +specified: |
| 309 | + |
| 310 | +- Discovers all replica set members when given the address of any one member. |
| 311 | +- Sends operations to the appropriate member, such as instructions |
| 312 | + to write against the **primary** node. To learn more about the replica |
| 313 | + set primary, see :manual:`Replica Set Primary </core/replica-set-primary/>` |
| 314 | + in the {+server-docs-name+}. |
| 315 | + |
| 316 | +.. tip:: |
| 317 | + |
| 318 | + You are required to specify only one host to connect to a replica set. |
| 319 | + However, to ensure connectivity when the selected host is unavailable, |
| 320 | + provide the full list of hosts. |
| 321 | + |
| 322 | +To learn more about setting up a MongoDB replica set, see |
| 323 | +:manual:`Deploy a Replica Set </tutorial/deploy-replica-set/>` in the |
| 324 | +{+server-docs-name+}. |
| 325 | + |
| 326 | +Direct Connection |
| 327 | +````````````````` |
| 328 | + |
| 329 | +To force operations to run on a specific node in a MongoDB replica set, |
| 330 | +specify the connection information for the node in the connection string and |
| 331 | +the ``directConnection`` parameter with a ``true`` value. |
| 332 | + |
| 333 | +Direct connections include the following limitations: |
| 334 | + |
| 335 | +- DNS seed list connection format connection strings cannot be used. |
| 336 | +- Write operations fail when the specified host is not the primary. |
| 337 | +- When the host is not the primary, you must specify the ``secondary`` read |
| 338 | + preference in your connection options. To learn more about this limitation, see the |
| 339 | + :manual:`secondary read preference entry </core/read-preference/#mongodb-readmode-secondary>` |
| 340 | + in the {+server-docs-name+}. |
| 341 | + |
| 342 | +The following example shows the connection string you can add to your |
| 343 | +Laravel application's ``.env`` file to establish a direct connection to a |
| 344 | +secondary node in a MongoDB replica set. The example uses the following sample |
| 345 | +values: |
| 346 | + |
| 347 | +- ``host2`` as the hostname of the secondary node |
| 348 | +- ``27017`` as the port on which the MongoDB node listens on |
| 349 | + |
| 350 | +.. code-block:: bash |
| 351 | + :caption: Sample .env environment configuration to enable a direct connection |
| 352 | + |
| 353 | + DB_URI="mongodb://host2:27017/?directConnection=true&readPreference=secondary" |
| 354 | + |
| 355 | + |
0 commit comments