Skip to content

Commit a8aaf7f

Browse files
author
Bob Grabar
committed
DOCS-275 redirect master-slave documentation: draft 1
1 parent 1c1a23a commit a8aaf7f

File tree

1 file changed

+366
-0
lines changed

1 file changed

+366
-0
lines changed
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
============
2+
Master Slave
3+
============
4+
5+
.. default-domain:: mongodb
6+
7+
*Use only for versions prior to 1.6*
8+
9+
.. important::
10+
11+
Use :doc:`replica sets </replication>` rather than the master-slave
12+
configuration. Replica sets are a functional superset of master/slave
13+
and are more robust. Master-slave configuration preceded the
14+
introduction of replica sets in version 1.6. This documentation is
15+
intended for users of MongoDB versions prior to MongoDB 1.6.
16+
17+
To download a newer version of MongoDB, see `MongoDB Downloads <http://mongodb.org/downloads>`_.
18+
19+
.. warning::
20+
21+
Master-slave configuration is a legacy configuration and should not
22+
be used with MongoDB version 1.6 or higher. Instead use :doc:`replica
23+
sets </replication>`, which provide automated failover and backups
24+
and which ensure high availability.
25+
26+
Configuration and Setup
27+
-----------------------
28+
29+
*Use only for versions prior to 1.6*
30+
31+
To configure an instance of :program:`mongod` to be a master database in
32+
a master-slave configuration, start two instances of the database, one
33+
in *master* mode, and the other in *slave* mode.
34+
35+
.. note:: The following examples explicitly specify the location of
36+
the data files on the command line. This is unnecessary if you are
37+
running the master and slave on separate machines. But for readers
38+
who are going try this setup on a single node, the examples specify
39+
the data files in the interest of safety.
40+
41+
.. code-block:: javascript
42+
43+
bin/mongod --master [--dbpath /data/masterdb/]
44+
45+
As a result, the master server process will create a
46+
``local.oplog.$main`` collection. This is the "transaction log", which
47+
queues operations that will be applied at the slave.
48+
49+
To configure an instance of :program:`mongod` to be a slave database in a
50+
master-slave configuration:
51+
52+
.. code-block:: javascript
53+
54+
bin/mongod --slave --source <masterhostname>[:<port>] [--dbpath
55+
/data/slavedb/]
56+
57+
Details of the source server are then stored in the slave's
58+
``local.sources`` collection. Instead of specifying the ``--source``
59+
parameter, one can add an object to ``local.sources`` that specifies
60+
information about the master server:
61+
62+
.. code-block:: javascript
63+
64+
$ bin/mongo <slavehostname>/local
65+
66+
> db.sources.find(); // confirms the collection is empty. then:
67+
68+
> db.sources.insert( { host: <masterhostname> } );
69+
70+
- ``host: masterhostname`` is the IP address or FQDN of the master
71+
database machine. Append ``:port`` to the server hostname if you want
72+
to run on a nonstandard port number.
73+
74+
- ``only: databasename`` (optional) if specified, indicates that only
75+
the specified database should replicate. NOTE: A bug with ``only`` is
76+
fixed in v1.2.4+
77+
78+
A slave may become out of sync with a master if it falls far behind the
79+
data updates available from that master, or if the slave is terminated
80+
and then restarted some time later when relevant updates are no longer
81+
available from the master. If a slave becomes out of sync, replication
82+
will terminate and operator intervention is required by default if
83+
replication is to be restarted. An operator may restart replication
84+
using the ``{resync:1}`` command. Alternatively, the command line option
85+
``--autoresync`` causes a slave to restart replication automatically
86+
(after ten second pause) if it becomes out of sync. If the
87+
``--autoresync`` option is specified, the slave will not attempt an
88+
automatic resync more than once in a ten minute period.
89+
90+
The ``--oplogSize`` command line option may be specified (along with
91+
``--master``) to configure the amount of disk space in megabytes which will
92+
be allocated for storing updates to be made available to slave nodes. If
93+
the ``--oplogSize`` option is not specified, the amount of disk space for
94+
storing updates will be 5% of available disk space (with a minimum of
95+
1GB) for 64bit machines, or 50MB for 32bit machines.
96+
97+
Command Line Options
98+
--------------------
99+
100+
*Use the master-slave configuration only for versions prior to 1.6*
101+
102+
Master
103+
~~~~~~
104+
105+
.. code-block:: javascript
106+
107+
--master master mode
108+
--oplogSize arg size limit (in MB) for op log
109+
110+
Slave
111+
~~~~~
112+
113+
.. code-block:: javascript
114+
115+
--slave slave mode
116+
--source arg arg specifies master as <server:port>
117+
--only arg arg specifies a single database to replicate
118+
--slavedelay arg arg specifies delay (in seconds) to be used
119+
when applying master ops to slave
120+
--autoresync automatically resync if slave data is stale
121+
122+
--slavedelay
123+
~~~~~~~~~~~~
124+
125+
Sometimes it is beneficial to have a slave that is purposefully many hours
126+
behind to prevent human error. In MongoDB 1.3.3+, you can specify this
127+
with the ``--slavedelay`` command line option. Specify the delay in
128+
seconds to be used when applying master operations to the slave.
129+
130+
Specify this option at the slave. Example command line:
131+
132+
.. code-block:: javascript
133+
134+
mongod --slave --source mymaster.foo.com --slavedelay 7200
135+
136+
Diagnostics
137+
~~~~~~~~~~~
138+
139+
Check master status from the :program:`mongo` shell with:
140+
141+
.. code-block:: javascript
142+
143+
// inspects contents of local.oplog.$main on master and reports status:
144+
db.printReplicationInfo()
145+
146+
Check slave status from the :program:`mongo` with:
147+
148+
.. code-block:: javascript
149+
150+
// inspects contents of local.sources on the slave and reports status:
151+
db.printSlaveReplicationInfo()
152+
153+
(Note you can evaluate the above functions without the parenthesis above
154+
to see their javascript source and a bit on the internals.)
155+
156+
As of 1.3.2, you can do this on the slave
157+
158+
.. code-block:: javascript
159+
160+
db._adminCommand( { serverStatus : 1 , repl : N } )
161+
162+
N is the level of diagnostic information and can have the following
163+
values:
164+
165+
- 0 - none
166+
- 1 - local (doesn't have to connect to other server)
167+
- 2 - remote (has to check with the master)
168+
169+
Security
170+
--------
171+
172+
*Use the master-slave configuration only for versions prior to 1.6*
173+
174+
When security is enabled, one must configure a user account for the
175+
local database that exists on both servers.
176+
177+
The slave-side of a replication connection first looks for a user repl
178+
in local.system.users. If present, that user is used to authenticate
179+
against the local database on the source side of the connection. If repl
180+
user does not exist, the first user object in local.system.users is
181+
tried.
182+
183+
The local database works like the admin database: an account for local
184+
has access to the entire server.
185+
186+
Example security configuration when security is enabled:
187+
188+
.. code-block:: javascript
189+
190+
$ mongo <slavehostname>/admin -u <existingadminusername> -p<adminpassword>
191+
> use local
192+
> db.addUser('repl', <replpassword>);
193+
^c
194+
$ mongo <masterhostname>/admin -u <existingadminusername> -p<adminpassword>
195+
> use local
196+
> db.addUser('repl', <replpassword>);
197+
198+
Master Slave vs. Replica Sets
199+
-----------------------------
200+
201+
*Use the master-slave configuration only for versions prior to 1.6*
202+
203+
Master/slave and replica sets are alternative ways to achieve
204+
replication with MongoDB.
205+
206+
Replica sets are newer (v1.6+) and more flexible, although a little more
207+
work to set up and learn at first.
208+
209+
The following replica set configuration is equivalent to a two node
210+
master/slave setup with hosts M (master) and S (slave):
211+
212+
.. code-block:: javascript
213+
214+
$ # run mongod instances with "--replSet mysetname" parameter
215+
$ # then in the shell:
216+
$ mongo --host M
217+
> cfg = {
218+
> _id : 'mysetname',
219+
> members : [
220+
> { _id : 0, host : 'M', priority : 1 },
221+
> { _id : 1, host : 'S', priority : 0, votes : 0 }
222+
> ]
223+
> };
224+
> rs.initiate(cfg);
225+
226+
Administrative Tasks for Master-Slave Configurations
227+
----------------------------------------------------
228+
229+
*Use the master-slave configuration only for versions prior to 1.6*
230+
231+
Failing over to a Slave (Promotion)
232+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
233+
234+
To permanently fail over from a down master (A) to a slave (B):
235+
236+
1. Shut down A.
237+
238+
2. Stop :program:`mongod` on B.
239+
240+
3. Back up or delete local.* datafiles on B.
241+
242+
4. Restart :program:`mongod` on B with the ``--master`` option.
243+
244+
.. note:: This is a one time cutover, and the "mirror" is broken. A
245+
cannot be brought back in sync with B without a full resync.
246+
247+
Inverting Master and Slave
248+
~~~~~~~~~~~~~~~~~~~~~~~~~~
249+
250+
If you have a master (A) and a slave (B) and you would like to reverse
251+
their roles, this is the recommended sequence of steps. Note the
252+
following assumes A is healthy, up-to-date and up.
253+
254+
1. Halt writes on A (using the fsync command).
255+
256+
2. Make sure B is caught up.
257+
258+
3. Shut down B.
259+
260+
4. Wipe local.* on B to remove old local.sources.
261+
262+
5. Start up B with the --master option.
263+
264+
6. Do a write on B (primes the oplog to provide a new sync start point).
265+
266+
7. Shut down B. B will now have a new set of local.* files.
267+
268+
8. Shut down A and replaceA's local.* files with a copy of B's new
269+
local.* files. Remember to compress the files before/while copying them
270+
– they can be quite large.
271+
272+
9. Start B with the --master option.
273+
274+
10. Start A with all the usual slave options plus --fastsync
275+
276+
If A is not healthy but the hardware is okay (power outage, server
277+
crash, etc.):
278+
279+
- Skip the first two steps
280+
281+
- Replace all of A's files with B's files in step 8.
282+
283+
If the hardware is not okay, replace A with a new machine and then
284+
follow the instructions in the previous paragraph.
285+
286+
Creating a slave from an existing master's disk image
287+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288+
289+
If you can stop write operations to the master for an indefinite period,
290+
you can copy the data files from the master to the new slave, and then
291+
start the slave with --fastsync.
292+
293+
.. warning::
294+
295+
Be careful with --fastsync. If the data is not perfectly in sync, a
296+
discrepancy will exist forever.
297+
298+
--fastsync is a way to start a slave starting with an existing master
299+
disk image/backup. This option declares that the adminstrator guarantees
300+
the image is correct and completely up to date with that of the master.
301+
If you have a full and complete copy of data from a master you can use
302+
this option to avoid a full synchronization upon starting the slave.
303+
304+
Creating a slave from an existing slave's disk image
305+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306+
307+
You can just copy the other slave's data file snapshot without any
308+
special options. Note data snapshots should only be taken when a mongod
309+
process is down or in fsync-and-lock state.
310+
311+
Resyncing a slave that is too stale to recover
312+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313+
314+
Slaves asynchronously apply write operations from the master. These
315+
operations are stored in the master's oplog. The oplog is finite in
316+
length. If a slave is too far behind, a full resync will be necessary.
317+
See the Halted Replication page.
318+
319+
Slave chaining
320+
~~~~~~~~~~~~~~
321+
322+
Slaves cannot be "chained", they must all connect to the master
323+
directly. If a slave is chained to another slave you may see the
324+
following in the logs: assertion 13051 tailable cursor requested on non
325+
capped collection ns:local.oplog.$main
326+
327+
Correcting a slave's source
328+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
329+
330+
If you accidentally type the wrong host for the slave's source or wish
331+
to change it, you can do so by manually modifying the slave's
332+
local.sources collection. For example, say you start the slave with:
333+
334+
.. code-block:: javascript
335+
336+
$ mongod --slave --source prod.missisippi
337+
338+
Restart the slave without the --slave and --source arguments.
339+
340+
.. code-block:: javascript
341+
342+
$ mongod
343+
344+
Now start the shell and update the local.sources collection.
345+
346+
.. code-block:: javascript
347+
348+
> use local
349+
350+
switched to db local
351+
352+
> db.sources.update({host : "prod.missisippi"}, {$set : {host :
353+
"prod.mississippi"}})
354+
355+
Restart the slave with the correct command line arguments or no --source
356+
argument (once local.sources is set, no --source is necessary).
357+
358+
.. code-block:: javascript
359+
360+
$ ./mongod --slave --source prod.mississippi
361+
362+
$ # or
363+
364+
$ ./mongod --slave
365+
366+
Now your slave will be pointing at the correct master.

0 commit comments

Comments
 (0)