|
| 1 | +========================== |
| 2 | +Install MongoDB on Windows |
| 3 | +========================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Synopsis |
| 8 | +-------- |
| 9 | + |
| 10 | +This tutorial outlines the basic installation process for |
| 11 | +:term:`MongoDB` on Microsoft Windows systems. This tutorial provides a |
| 12 | +basic method for installing and running the MongoDB server |
| 13 | +(i.e. ":program:`mongod.exe`") on Windows systems through the |
| 14 | +:guilabel:`Command Shell` and will also outline the process for |
| 15 | +setting up MongoDB as a :guilabel:`Windows Service`. |
| 16 | + |
| 17 | +MongoDB on Windows is similar to MongoDB in other environments: and |
| 18 | +most components have similar operations with similar operational |
| 19 | +patterns. |
| 20 | + |
| 21 | +Procedure |
| 22 | +--------- |
| 23 | + |
| 24 | +Download MongoDB for Windows |
| 25 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 | + |
| 27 | +Download the latest production release of MongoDB from the `MongoDB |
| 28 | +downloads page <http://www.mongodb.org/downloads>`_. |
| 29 | + |
| 30 | +.. note:: |
| 31 | + |
| 32 | + Ensure you download the proper version of MongoDB for your |
| 33 | + architecture of Windows. The 64-bit version of MongoDB will not |
| 34 | + work in 32-bit Windows. |
| 35 | + |
| 36 | + The 32-bit versions of MongoDB are suitable for testing and |
| 37 | + evaluation purposes but can only support small databases and are |
| 38 | + not appropriate for production deployments. |
| 39 | + |
| 40 | + You can find the architecture of your version of Windows |
| 41 | + installation using the following command in the :guilabel:`Command |
| 42 | + Shell` |
| 43 | + |
| 44 | + .. code-block:: powershell |
| 45 | + |
| 46 | + wmic os get osarchitecture |
| 47 | + |
| 48 | +In Windows Explorer, find the MongoDB download file, typically in the |
| 49 | +default Downloads directory, and extract the archive to ``C:\`` by |
| 50 | +right clicking on the archive and selecting :guilabel:`Extract All` |
| 51 | +and browsing to ``C:\``. |
| 52 | + |
| 53 | +.. note:: |
| 54 | + |
| 55 | + The folder name will be either: |
| 56 | + |
| 57 | + .. code-block:: powershell |
| 58 | + |
| 59 | + C:\mongodb-win32-i386-[version] |
| 60 | + |
| 61 | + Or: |
| 62 | + |
| 63 | + .. code-block:: powershell |
| 64 | + |
| 65 | + C:\mongodb-win32-x86_64-[version] |
| 66 | + |
| 67 | + In both examples, replace ``[version]`` with the version of MongoDB |
| 68 | + downloaded. |
| 69 | + |
| 70 | +Set up the Environment |
| 71 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | + |
| 73 | +Start the :guilabel:`Command Shell` by selecting the :guilabel:`Start |
| 74 | +Menu`, then :guilabel:`All Programs`, then :guilabel:`Accessories`, |
| 75 | +then right click :guilabel:`Command Shell`, and select :guilabel:`Run |
| 76 | +as Administrator` from the popup menu. In the :guilabel:`Command |
| 77 | +Shell`, issue the following commands: |
| 78 | + |
| 79 | +.. code-block:: powershell |
| 80 | + |
| 81 | + cd \ |
| 82 | + rename C:\mongodb-win32-* C:\mongodb |
| 83 | + |
| 84 | +.. note:: |
| 85 | + |
| 86 | + MongoDB is self-contained and does not have any other system |
| 87 | + dependencies. You can run MongoDB from any folder you choose. You |
| 88 | + may install MongoDB in any directory (e.g. ``D:\test\mongodb``) |
| 89 | + |
| 90 | +MongoDB requires a data folder to store its files. The default |
| 91 | +location for the MongoDB data directory is ``C:\data\db``. Create this |
| 92 | +folder using the :guilabel:`Command Shell` and issuing the following |
| 93 | +command sequence: |
| 94 | + |
| 95 | +.. code-block:: powershell |
| 96 | + |
| 97 | + md data |
| 98 | + md data\db |
| 99 | + |
| 100 | +.. note:: |
| 101 | + |
| 102 | + You may specify an alternate path for ``\data\db`` with the |
| 103 | + :setting:`dbpath` setting for :program:`mongod.ext`, as in the |
| 104 | + following example: |
| 105 | + |
| 106 | + .. code-block:: powershell |
| 107 | + |
| 108 | + C:\mongodb\bin\mongod.exe --dbpath d:\test\mongodb\data |
| 109 | + |
| 110 | + If your path includes spaces, enclose the entire path in double |
| 111 | + quotations, for example: |
| 112 | + |
| 113 | + .. code-block:: powershell |
| 114 | + |
| 115 | + C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data" |
| 116 | + |
| 117 | + |
| 118 | +Start MongoDB |
| 119 | +~~~~~~~~~~~~~ |
| 120 | + |
| 121 | +To start MongoDB, execute from the :guilabel:`Command Shell`: |
| 122 | + |
| 123 | +.. code-block:: powershell |
| 124 | + |
| 125 | + C:\mongodb\bin\mongod.exe |
| 126 | + |
| 127 | +This will start the main MongoDB database process. |
| 128 | + |
| 129 | +.. note:: |
| 130 | + |
| 131 | + Depending on the security level of your system, Windows will issue |
| 132 | + a :guilabel:`Security Alert` dialog box about blocking "some |
| 133 | + features" of ``C:\\mongodb\bin\mongod.exe`` from communicating on |
| 134 | + networks. All users should select ``Private Networks, such as my |
| 135 | + home or work network`` and click ``Allow access``. For additional |
| 136 | + information on security and MongoDB, please read the |
| 137 | + :wiki:`Security and Authentication <Security+and+Authentication>` |
| 138 | + wiki page. |
| 139 | + |
| 140 | +.. warning:: |
| 141 | + |
| 142 | + Do not make :program:`mongod.exe` accessible to public networks |
| 143 | + without running in "Secure Mode" (i.e. :setting:`auth`) on a public |
| 144 | + network. Usually, MongoDB runs in "trusted environments" and the |
| 145 | + database does not enable authentication or "Secure Mode" by |
| 146 | + default. |
| 147 | + |
| 148 | +To connect to MongoDB using the :program:`mongo.exe`, open another |
| 149 | +:guilabel:`Command Shell` and issue command: |
| 150 | + |
| 151 | +.. code-block:: powershell |
| 152 | + |
| 153 | + C:\mongodb\bin\mongo.exe |
| 154 | + |
| 155 | +.. note:: |
| 156 | + |
| 157 | + Executing the command ``start C:\mongodb\bin\mongo.exe`` will |
| 158 | + automatically start the :program:`mongo` shell in a separate |
| 159 | + :guilabel:`Command Shell` window. |
| 160 | + |
| 161 | +This :program:`mongo` shell will connect to the database running on |
| 162 | +the localhost interface and port ``27017`` by default. At the |
| 163 | +:program:`mongo.exe` prompt, issue the following two commands to |
| 164 | +insert a record in the ``test`` :term:`collection` of the default |
| 165 | +``test`` database and then retrieve that record: |
| 166 | + |
| 167 | +.. code-block:: javascript |
| 168 | + |
| 169 | + > db.test.save( { a: 1 } ) |
| 170 | + > db.test.find() |
| 171 | + |
| 172 | +.. seealso:: ":program:`mongo`" and ":doc:`/reference/javascript`" |
| 173 | + |
| 174 | +MongoDB as a Windows Service |
| 175 | +---------------------------- |
| 176 | + |
| 177 | +.. versionadded:: 2.1.1 |
| 178 | + |
| 179 | +Setup MongoDB as a :guilabel:`Windows Service`, so that the database |
| 180 | +will start automatically following each reboot cycle. |
| 181 | + |
| 182 | +Configure the System |
| 183 | +~~~~~~~~~~~~~~~~~~~~ |
| 184 | + |
| 185 | +You should specify two options when running MongoDB as a Windows |
| 186 | +Service: a path for the log output (i.e. :setting:`logpath`) and a |
| 187 | +:doc:`configuration file </reference/configuration-options>`. |
| 188 | + |
| 189 | +#. Create a specific directory for MongoDB log files: |
| 190 | + |
| 191 | + .. code-block:: powershell |
| 192 | + |
| 193 | + md C:\mongodb\log |
| 194 | + |
| 195 | +#. Create a configuration file for the :setting:`logpath` option for |
| 196 | + MongoDB in the :guilabel:`Command Shell` by issuing this command: |
| 197 | + |
| 198 | + .. code-block:: powershell |
| 199 | + |
| 200 | + echo logpath=C:\mongodb\log > C:\mongodb\mongodb.cfg |
| 201 | + |
| 202 | +While these optional steps are optional, using the configuration file, |
| 203 | +and creating a specific location for log files are good practice. |
| 204 | + |
| 205 | +.. note:: |
| 206 | + |
| 207 | + Also consider setting the :setting:`logappend` option, otherwise, |
| 208 | + :program:`mongod.exe` will delete the contents of the existing log |
| 209 | + file when starting. |
| 210 | + |
| 211 | + .. versionchanged:: 2.2 |
| 212 | + This behavior will change in the 2.2 release. |
| 213 | + |
| 214 | +Install and Run the MongoDB Service |
| 215 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 216 | + |
| 217 | +Run all of the following commands in :guilabel:`Command Shell` with |
| 218 | +"Administrative Privileges:" |
| 219 | + |
| 220 | +#. To install the MongoDB service: |
| 221 | + |
| 222 | + .. code-block:: powershell |
| 223 | + |
| 224 | + C:\mongodb\bin\mongod.exe --config C:\mongodb\mongodb.cfg --install |
| 225 | + |
| 226 | +#. To run the MongoDB Service: |
| 227 | + |
| 228 | + .. code-block:: powershell |
| 229 | + |
| 230 | + net start MongoDB |
| 231 | + |
| 232 | +Stop or Remove the MongoDB Service |
| 233 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 234 | + |
| 235 | +- To stop MongoDB Service: |
| 236 | + |
| 237 | + .. code-block:: powershell |
| 238 | + |
| 239 | + net stop MongoDB |
| 240 | + |
| 241 | +- To remove MongoDB Service: |
| 242 | + |
| 243 | + .. code-block:: powershell |
| 244 | + |
| 245 | + C:\mongodb\bin\mongod.exe --remove |
0 commit comments