single master and slave mode
1: Start Master
[jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ mongod --dbpath /home/jifeng/mongodb-linux-x86_64-2.6.4/data --port 10000 --master
2014-09-05T15:11:50.115+0800 [initandlisten] MongoDB starting : pid=23623 port=10000 dbpath=/home/jifeng/mongodb-linux-x86_64-2.6.4/data master=1 64-bit host=jifeng04
2014-09-05T15:11:50.116+0800 [initandlisten] db version v2.6.4
2014-09-05T15:11:50.116+0800 [initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
2014-09-05T15:11:50.117+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-09-05T15:11:50.117+0800 [initandlisten] allocator: tcmalloc
2014-09-05T15:11:50.117+0800 [initandlisten] options: { master: true, net: { port: 10000 }, storage: { dbPath: "/home/jifeng/mongodb-linux-x86_64-2.6.4/data" } }
2014-09-05T15:11:50.127+0800 [initandlisten] journal dir=/home/jifeng/mongodb-linux-x86_64-2.6.4/data/journal
2014-09-05T15:11:50.127+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-09-05T15:11:50.298+0800 [initandlisten] waiting for connections on port 10000
2: Start slave
[jifeng@jifeng04 ~]$ mongod --dbpath /home/jifeng/mongodb-linux-x86_64-2.6.4/data2 --port 10001 --slave --source localhost:10000
2014-09-05T15:12:48.411+0800 [initandlisten] MongoDB starting : pid=23636 port=10001 dbpath=/home/jifeng/mongodb-linux-x86_64-2.6.4/data2 slave=1 64-bit host=jifeng04
2014-09-05T15:12:48.412+0800 [initandlisten] db version v2.6.4
2014-09-05T15:12:48.412+0800 [initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
2014-09-05T15:12:48.412+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-09-05T15:12:48.412+0800 [initandlisten] allocator: tcmalloc
2014-09-05T15:12:48.413+0800 [initandlisten] options: { net: { port: 10001 }, slave: true, source: "localhost:10000", storage: { dbPath: "/home/jifeng/mongodb-linux-x86_64-2.6.4/data2" } }
2014-09-05T15:12:48.417+0800 [initandlisten] journal dir=/home/jifeng/mongodb-linux-x86_64-2.6.4/data2/journal
2014-09-05T15:12:48.417+0800 [initandlisten] recover : no journal files present, no recovery needed
2014-09-05T15:12:48.434+0800 [initandlisten] waiting for connections on port 10001
2014-09-05T15:12:49.438+0800 [replslave] repl: syncing from host:localhost:10000
3: Display data
[Jifeng@jifeng04 ~]$ MONGO localhost:10000
MongoDB shell version:2.6.4
connecting
> Db.master.find ()
{"_id": ObjectId ("540942bed89f094a5fbd9b5a"), "UID": 1000}
{"_id": ObjectId ("540946bcd89f094a5fbd9b5b"), "UID": 1001}
{"_id": ObjectId ("540956b7789903d8baf6b1b3"), "UID": 1002}
From
[Jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ MONGO localhost:10001
mongodb Shell version:2.6.4
To:localhost:10001/test
> Db.master.find ()
{"_id": ObjectId ("540942bed89f094a5fbd9b5a"), "UID": 1000}< c4/>{"_id": ObjectId ("540946bcd89f094a5fbd9b5b"), "UID": 1001}
{"_id": ObjectId ("540956b7789903d8baf6b1b3"), "U ID ": 1002}
4: Primary Write Data
> Db.master.insert ({uid:1004})
Writeresult ({"ninserted": 1})
> Db.master.find ()
{"_id": ObjectId ("540942bed89f094a5fbd9b5a"), "UID": 1000}
{"_id": ObjectId ("540946bcd89f094a5fbd9b5b"), "UID": 1001}
{"_i D ": ObjectId (" 540956b7789903d8baf6b1b3 ")," UID ": 1002}
{" _id ": ObjectId (" 5409638c0a6617467df195ec ")," UID ": 100 4}
From query data
[jifeng@jifeng04 ~]$ cd mongodb-linux-x86_64-2.6.4
[jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ MONGO : 10001
MongoDB shell version:2.6.4
connecting to:localhost:10001/test
> Db.master.find ()
{"_id" : ObjectId ("540942bed89f094a5fbd9b5a"), "UID": 1000}
{"_id": ObjectId ("540946bcd89f094a5fbd9b5b"), "UID": 1001}< c6/>{"_id": ObjectId ("540956b7789903d8baf6b1b3"), "UID": 1002}
> Db.master.find ()
{"_id": ObjectId ("5409 42BED89F094A5FBD9B5A ")," UID ": 1000}
{" _id ": ObjectId (" 540946bcd89f094a5fbd9b5b ")," UID ": 1001}
{" _id ": O Bjectid ("540956b7789903d8baf6b1b3"), "UID": 1002}
{"_id": ObjectId ("5409638c0a6617467df195ec"), "UID": 1004}
From the machine log
2014-09-05t15:15:48.791+0800 [Initandlisten] connection accepted from 127.0.0.1:35622 #1 (1 connection now OPEN)
2014-09-05t15:16:34.336+0800 [Replslave] REPL: checkpoint applied Operations
0800 [Replslave] repl: syncedto:sep 5 15:16:24 54096348:1
2014-09-05t15:18:48.556+0800 [Clientcursormon ] Mem (MB) res:36 virt:585
2014-09-05t15:18:48.556+0800 [Clientcursormon] mapped (incl Journal view):
the 2014-09-05t15:18:48.556+0800 [Clientcursormon] connections:1
2014-09-05t15:18:54.357+0800 [Replslave] REPL: checkpoint applied Operations
2014-09-05t15:18:54.358+0800 [Replslave] repl: syncedto:sep 5 15:18:44 540963d4:1
Close MongoDB:
Method One:
[Jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ mongod --shutdown --dbpath/home/jifeng/mongodb-linux-x86 _64-2.6.4/data
killing process with pid:22745
Method Two:
<pre name="code" class="html"><pre name="code" class="html">[jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ mongo localhost:10000
MongoDB shell version: 2.6.4
connecting to: localhost:10001/test
> use admin;
switched to db admin
> db.shutdownServer();
2014-09-05T15:23:59.910+0800 DBClientCursor::init call() failed
server should be down...
2014-09-05T15:23:59.912+0800 trying reconnect to localhost:10000 (127.0.0.1) failed
2014-09-05T15:23:59.915+0800 warning: Failed to connect to 127.0.0.1:10000, reason: errno:111 Connection refused
2014-09-05T15:23:59.915+0800 reconnect localhost:10000 (127.0.0.1) failed failed couldn't connect to server localhost:10000 (127.0.0.1), connection attempt failed
> exit;
[jifeng@jifeng04 ~]$
Multi-machine master and slave mode:
[Jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ mkdir log
[jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ Mongod-- Dbpath/home/jifeng/mongodb-linux-x86_64-2.6.4/data--master--rest--nojournal--fork--logpath/home/jifeng/ Mongodb-linux-x86_64-2.6.4/log
2014-09-05t10:31:44.158+0800 * * WARNING:--rest is specified without-- Httpinterface,
2014-09-05t10:31:44.172+0800 * * enabling HTTP interface about to
fork child process, Waiting until server is ready for connections.
Forked process:22535
Error:child process failed, exited with ERROR number 1
Error, found log file does not exist
[Jifeng@jifeng04 mongodb-linux-x86_64-2.6.4]$ Mongod--dbpath/home/jifeng/mongodb-linux-x86_64-2.6.4/data--master --rest--nojournal--fork--logpath/home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log
2014-09-05T16 : 12:09.321+0800 * * WARNING:--rest is specified without--httpinterface,
2014-09-05t16:12:09.322+0800 * * Enabling HTTP interface about
to fork the child process, waiting until server are ready for connections.
Forked process:24061 Child
process started successfully, parent exiting
Boot successful, rest has problems
2: Start slave
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ mongod --dbpath /home/jifeng/mongodb-linux-x86_64-2.6.4/data --slave --source jifeng04:27017 --rest --nojournal --fork --logpath /home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log
2014-09-05T16:13:57.872+0800 ** WARNING: --rest is specified without --httpinterface,
2014-09-05T16:13:57.872+0800 ** enabling http interface
about to fork child process, waiting until server is ready for connections.
forked process: 2787
child process started successfully, parent exiting
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ cat log/mongod.log
2014-09-05T16:13:57.883+0800 [initandlisten] MongoDB starting : pid=2787 port=27017 dbpath=/home/jifeng/mongodb-linux-x86_64-2.6.4/data slave=1 64-bit host=jifeng05
2014-09-05T16:13:57.883+0800 [initandlisten] db version v2.6.4
2014-09-05T16:13:57.883+0800 [initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
2014-09-05T16:13:57.883+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-09-05T16:13:57.883+0800 [initandlisten] allocator: tcmalloc
2014-09-05T16:13:57.883+0800 [initandlisten] options: { net: { http: { RESTInterfaceEnabled: true, enabled: true } }, processManagement: { fork: true }, slave: true, source: "jifeng04:27017", storage: { dbPath: "/home/jifeng/mongodb-linux-x86_64-2.6.4/data", journal: { enabled: false } }, systemLog: { destination: "file", path: "/home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log" } }
2014-09-05T16:13:57.929+0800 [initandlisten] waiting for connections on port 27017
2014-09-05T16:13:57.929+0800 [websvr] admin web console waiting for connections on port 28017
2014-09-05T16:13:58.929+0800 [replslave] repl: --source jifeng04:27017 != jifeng04 from local.sources collection
2014-09-05T16:13:58.929+0800 [replslave] repl: for instructions on changing this slave's source, see:
2014-09-05T16:13:58.929+0800 [replslave] http://dochub.mongodb.org/core/masterslave
2014-09-05T16:13:58.929+0800 [replslave] repl: terminating mongod after 30 seconds
Error Tip: repl:--source jifeng04:27017!= jifeng04 from Local.sources collection
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ mongod --dbpath /home/jifeng/mongodb-linux-x86_64-2.6.4/data --slave --source 10.5.4.57:27017 --rest --nojournal --fork --logpath /home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log
2014-09-05T16:15:59.408+0800 ** WARNING: --rest is specified without --httpinterface,
2014-09-05T16:15:59.408+0800 ** enabling http interface
about to fork child process, waiting until server is ready for connections.
forked process: 2809
child process started successfully, parent exiting
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ cat log/mongod.log
2014-09-05T16:15:59.415+0800 [initandlisten] MongoDB starting : pid=2809 port=27017 dbpath=/home/jifeng/mongodb-linux-x86_64-2.6.4/data slave=1 64-bit host=jifeng05
2014-09-05T16:15:59.415+0800 [initandlisten] db version v2.6.4
2014-09-05T16:15:59.415+0800 [initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
2014-09-05T16:15:59.415+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-09-05T16:15:59.415+0800 [initandlisten] allocator: tcmalloc
2014-09-05T16:15:59.415+0800 [initandlisten] options: { net: { http: { RESTInterfaceEnabled: true, enabled: true } }, processManagement: { fork: true }, slave: true, source: "10.5.4.57:27017", storage: { dbPath: "/home/jifeng/mongodb-linux-x86_64-2.6.4/data", journal: { enabled: false } }, systemLog: { destination: "file", path: "/home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log" } }
2014-09-05T16:15:59.447+0800 [initandlisten] waiting for connections on port 27017
2014-09-05T16:15:59.449+0800 [websvr] admin web console waiting for connections on port 28017
2014-09-05T16:16:00.449+0800 [replslave] repl: --source 10.5.4.57:27017 != jifeng04 from local.sources collection
2014-09-05T16:16:00.449+0800 [replslave] repl: for instructions on changing this slave's source, see:
2014-09-05T16:16:00.449+0800 [replslave] http://dochub.mongodb.org/core/masterslave
2014-09-05T16:16:00.449+0800 [replslave] repl: terminating mongod after 30 seconds
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$
Error Tip: repl:--source 10.5.4.57:27017!= jifeng04 from Local.sources collection
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ mongod --dbpath /home/jifeng/mongodb-linux-x86_64-2.6.4/data --slave --source jifeng04 --rest --nojournal --fork --logpath /home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log
2014-09-05T16:19:02.564+0800 ** WARNING: --rest is specified without --httpinterface,
2014-09-05T16:19:02.564+0800 ** enabling http interface
about to fork child process, waiting until server is ready for connections.
forked process: 2832
child process started successfully, parent exiting
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$ cat log/mongod.log
2014-09-05T16:19:02.575+0800 [initandlisten] MongoDB starting : pid=2832 port=27017 dbpath=/home/jifeng/mongodb-linux-x86_64-2.6.4/data slave=1 64-bit host=jifeng05
2014-09-05T16:19:02.575+0800 [initandlisten] db version v2.6.4
2014-09-05T16:19:02.575+0800 [initandlisten] git version: 3a830be0eb92d772aa855ebb711ac91d658ee910
2014-09-05T16:19:02.575+0800 [initandlisten] build info: Linux build7.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2014-09-05T16:19:02.575+0800 [initandlisten] allocator: tcmalloc
2014-09-05T16:19:02.575+0800 [initandlisten] options: { net: { http: { RESTInterfaceEnabled: true, enabled: true } }, processManagement: { fork: true }, slave: true, source: "jifeng04", storage: { dbPath: "/home/jifeng/mongodb-linux-x86_64-2.6.4/data", journal: { enabled: false } }, systemLog: { destination: "file", path: "/home/jifeng/mongodb-linux-x86_64-2.6.4/log/mongod.log" } }
2014-09-05T16:19:02.608+0800 [initandlisten] waiting for connections on port 27017
2014-09-05T16:19:02.610+0800 [websvr] admin web console waiting for connections on port 28017
2014-09-05T16:19:03.612+0800 [replslave] repl: syncing from host:jifeng04
2014-09-05T16:19:03.613+0800 [replslave] warning: Failed to connect to 10.5.4.57:27017, reason: errno:113 No route to host
2014-09-05T16:19:03.613+0800 [replslave] repl: couldn't connect to server jifeng04:27017 (10.5.4.57), connection attempt failed
2014-09-05T16:19:03.613+0800 [replslave] repl: sleep 3 sec before next pass
[jifeng@jifeng05 mongodb-linux-x86_64-2.6.4]$
Error Tip:warning:failed to connect to 10.5.4.57:27017, reason:errno:113 No route to host
Not connected.