@@ -8,7 +8,8 @@ MongoDB runs as a standard program. You can start MongoDB from a command
8
8
line by issuing the :program:`mongod` command and specifying options.
9
9
Fora list of options, see :doc:`/reference/mongod`. MongoDB can also run
10
10
as a Windows service. For details, see
11
- :ref:`tutorial-mongod-as-windows-service`.
11
+ :ref:`tutorial-mongod-as-windows-service`. To install MongoDB, see
12
+ :doc:`/installation`.
12
13
13
14
The following examples assume the directory containing the
14
15
:program:`mongod` process is included in your system paths. The
@@ -35,13 +36,13 @@ To start MongoDB in default mode, issue the following command:
35
36
Specify a Data Directory
36
37
~~~~~~~~~~~~~~~~~~~~~~~~
37
38
38
- To specify the directory where MongoDB stores data files, you must
39
- create the directory and set its permissions appropriately. MongoDB does
40
- not create the directory if the directory does not exist.
39
+ When you specify a data directory, the directory must already exist. If
40
+ it does not, create it and set its permissions appropriately.
41
41
42
- You then use the ``--dbpath`` option when starting MongoDB. The
43
- following command starts :program:`mongod` and stores data in the
44
- ``/var/lib/mongodb/`` directory:
42
+ To specify a data directory when starting MongoDB, use the
43
+ :option:`--dbpath <mongod --dbpath>` option. The following command
44
+ starts :program:`mongod` and stores data in the ``/var/lib/mongodb/``
45
+ directory:
45
46
46
47
.. code-block:: sh
47
48
@@ -51,19 +52,17 @@ Specify a TCP Port
51
52
~~~~~~~~~~~~~~~~~~
52
53
53
54
If you run several :program:`mongod` processes on a single machine, you
54
- must assign each a different port. Only one can listen on the default
55
- port of ``27017``.
55
+ must assign each a different port to listen on for client connections.
56
+ Only one can listen on the default port of ``27017``.
56
57
57
- To specify the port that MongoDB listens on for client connections, use
58
- the ``--port`` option. The following command starts :program:`mongod`
59
- listening on port ``12345``:
58
+ To specify the port, use the ``--port`` option. The following command
59
+ starts :program:`mongod` listening on port ``12345``:
60
60
61
61
.. code-block:: sh
62
62
63
63
mongod --port 12345
64
64
65
- It is highly recommended that you use the default port number whenever
66
- possible, to avoid any confusion.
65
+ Use the default port number whenever possible, to avoid any confusion.
67
66
68
67
If you use the :option:`--configsvr <mongod --configsvr>` or
69
68
:option:`--shardsvr <mongod --shardsvr>` options, those options select an
@@ -73,114 +72,134 @@ Run ``mongod`` as a Daemon
73
72
~~~~~~~~~~~~~~~~~~~~~~~~~~
74
73
75
74
To fork the :program:`mongod` process *and* redirect its output to a log
76
- file, use the ``--fork`` and ``--logpath`` options. You must create the
77
- log path directory ahead of time; however not the log file. MongoDB will
78
- create the log file if it does not exist.
75
+ file, use the :option:`--fork <mongod --fork>` and :option:`--logpath <mongod
76
+ --logpath>` options. You must create the log directory ahead of time.
77
+ However, you need not create the log file. MongoDB will create the log
78
+ file if it does not exist.
79
79
80
80
The following command runs :program:`mongod` as a daemon and records log
81
81
output to ``/var/log/mongodb.log``. The command also uses the
82
- `` --logappend`` option to ensure that :program:`mongod` appends new
83
- entries to the end of the log file rather than overwriting the content
84
- when the process restarts:
82
+ :option:` --logappend <mongod --logappend>` option to ensure that
83
+ :program:`mongod` appends new entries to the end of the log file rather
84
+ than overwriting the content when the process restarts:
85
85
86
- --logappend
87
86
.. code-block:: sh
88
87
89
88
mongod --fork --logpath /var/log/mongodb.log --logappend
90
89
90
+ Additional Configuration Options
91
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
+
93
+ For an overview of common configurations and examples of best-practice
94
+ configurations for common use cases, see
95
+ :doc:`/administration/configuration`.
96
+
91
97
Stop ``mongod``
92
98
---------------
93
99
94
- If :program:`mongod` is running in the foreground, then to stop :program:`mongod` press ``Control+C``.
95
- MongoDB does a clean exit when all ongoing operations are complete and flushes and closes all data files.
100
+ To stop a :program:`mongod` instance that is running in the foreground,
101
+ press ``Control+C``. MongoDB stops when all ongoing operations are
102
+ complete and does a clean exit, flushing and closing all data files.
103
+
104
+ To stop a :program:`mongod` instance running in the background or foreground,
105
+ issue the :method:`shutdownServer() <db.shutdownServer()>` method. Use the following sequence:
106
+
107
+ 1. To open the :program:`mongo` shell for a :program:`mongod` instance
108
+ running on the default port of ``27017``, issue the following command:
109
+
110
+ .. code-block:: sh
111
+
112
+ mongo
113
+
114
+ #. To switch to the ``admin`` database and shutdown the :program:`mongod`
115
+ instance, issue the following commands:
96
116
97
- Send shutdownServer() message from the mongo shell
117
+ .. code-block:: javascript
98
118
99
- The shell can request that the server terminate.
119
+ use admin
120
+ db.shutdownServer()
100
121
101
- $ ./mongo
102
- > use admin
103
- > db.shutdownServer()
122
+ This command works only from ``localhost`` or if the user is
123
+ authenticated.
104
124
105
- This command only works from localhost or if one is authenticated.
125
+ Alternately, you can shut down the :program:`mongod` instance:
106
126
107
- From a driver (where the helper function may not exist), one can run the
108
- command
127
+ - using the :option:`--shutdown` option
109
128
110
- { "shutdown" : 1 }
129
+ - from a driver using the :dbcommand:`shutdown`. For details, see the
130
+ :doc:`drivers documentation </applications/drivers>` for your driver.
111
131
112
- If this server is the primary in a replica set, it will go through the
113
- following process (version 1.9.1+):
132
+ ``mongod`` Shutdown and Replica Sets
133
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114
134
115
- Check how up-to-date the secondaries are
116
- ----------------------------------------
135
+ If the :program:`mongod` is the :term:`primary` in a :term:`replica
136
+ set`, the :program:`mongod` will go through the following process:
117
137
118
- If no secondary within 10 seconds of the primary, return that we won't
119
- shut down (optionally pass the timeoutSecs option to wait for a
120
- secondary to catch up.
138
+ 1. Check how up-to-date the :term:`secondaries <secondary>` are.
121
139
122
- If there is a secondary within 10 seconds of the primary, the primary
123
- will step down and wait for the secondary to catch up.
140
+ #. If no secondary is within 10 seconds of the primary,
141
+ :program:`mongod` will return a message that it will not shut down.
142
+ You can pass the the :dbcommand:`shutdown` command a ``timeoutSecs``
143
+ argument to wait for a secondary to catch up.
124
144
125
- After 60 seconds or once the secondary has caught up, the primary will
126
- shut down.
145
+ #. If there is a secondary within 10 seconds of the primary, the primary
146
+ will step down and wait for the secondary to catch up.
147
+
148
+ #. After 60 seconds or once the secondary has caught up, the primary
149
+ will shut down.
127
150
128
151
If there is no up-to-date secondary and you want the primary to shut
129
- down, you can use ``force : true``:
152
+ down, issue the :dbcommand:`shutdown` command with the ``force``
153
+ argument, as show in the following command:
130
154
131
155
.. code-block:: javascript
132
156
133
157
db.adminCommand({shutdown : 1, force : true})
134
158
135
- You can also specify timeoutSecs : N, which will keep checking the
136
- secondaries for N seconds if none are immediately up-to-date. If any of
137
- the secondaries catch up within N seconds, the primary will shut down.
138
- If no secondaries catch up, it will not shut down.
159
+ To keep checking the secondaries for a specified number of seconds if
160
+ none are immediately up-to-date, issue :dbcommand:`shutdown` with the
161
+ ``timeoutSecs`` argument. MongoDB will keep checking the secondaries for
162
+ the specified number of seconds if none are immediately up-to-date. If
163
+ any of the secondaries catch up within the allotted time, the primary
164
+ will shut down. If no secondaries catch up, it will not shut down.
165
+
166
+ The following command issues :dbcommand:`shutdown` with ``timeoutSecs``
167
+ set to ``5``:
139
168
140
169
.. code-block:: javascript
141
170
142
171
db.adminCommand({shutdown : 1, timeoutSecs : 5})
143
172
144
- or
173
+ Alternately you can use the ``timeoutSecs`` argument with the
174
+ :method:`shutdownServer() <db.shutdownServer()>` method:
145
175
146
176
.. code-block:: javascript
147
177
148
178
db.shutdownServer({timeoutSecs : 5})
149
179
150
- This generates output similar to the following:
151
-
152
- .. code-block:: javascript
153
-
154
- {
155
- "closest" : NumberLong(1307651781),
156
- "difference" : NumberLong(1307651808),
157
- "errmsg" : "no secondaries within 10 seconds of my optime",
158
- "ok" : 0
159
- }
160
-
161
- Sending a Unix INT or TERM Signal
180
+ Sending a UNIX INT or TERM Signal
162
181
---------------------------------
163
182
164
- You can cleanly stop :program:`mongod` using a SIGINT or SIGTERM signal on
165
- Unix-like systems. Either ^C, "kill -2 PID," or kill -15 PID will work.
183
+ You can cleanly stop :program:`mongod` using a SIGINT or SIGTERM signal
184
+ on UNIX-like systems. Either ``^C``, ``kill -2 PID``, or ``kill -15
185
+ PID`` will work.
166
186
167
- Sending a KILL signal kill -9 will probably cause damage if :program:`mongod` is
168
- not running with Journaling. (Journaling is on by default for 64 bit
169
- :program:`mongod`.) See also: repairDatabase command.)
187
+ Sending ``kill -9`` will probably cause damage if :program:`mongod` is
188
+ not running with :term:`journaling <journal>`.
170
189
171
- After a hard crash, when not using --journal, MongoDB will say it was
172
- not shutdown cleanly, and ask you to do a repair of the database.
190
+ To recover data if MongoDB does not shut down cleanly and if
191
+ :term:`journaling <journal>` is disabled, see
192
+ :doc:`/tutorial/recover-data-following-unexpected-shutdown`.
173
193
174
194
Memory Usage
175
195
------------
176
196
177
- MongoDB uses memory mapped files to access data, which results in large
178
- numbers being displayed in tools like top for the :program:`mongod` process. This
179
- is not a concern, and is normal when using memory-mapped files.
180
- Basically, the size of mapped data is shown in the virtual size
181
- parameter, and resident bytes shows how much data is being cached in
182
- RAM.
197
+ MongoDB uses memory-mapped files to access data, which results in large
198
+ numbers being displayed in tools like ``top`` for the :program:`mongod`
199
+ process. This is not a concern and is normal when using memory-mapped
200
+ files. The size of mapped data is shown in the virtual size parameter.
201
+ Resident bytes shows how much data is being cached in RAM.
183
202
184
203
You can get a feel for the "inherent" memory footprint of MongoDB by
185
- starting it fresh, with no connections, with an empty /data/db directory
186
- and looking at the resident bytes.
204
+ starting it fresh, with no connections, with an empty `` /data/db``
205
+ directory and looking at the resident bytes.
0 commit comments