MongoDB has been widely used as a NoSQL Database in recent years. More and more enterprises are trying to use MongoDB instead of the original Database. MongoDB also performs well in clusters, sharding, and replication. I will introduce various MongoDB deployment experiments.
Article 2 MongoDB Replica Set automatic replication of Replica Set, which consists of seven parts
1. initialize the file directory
2. Start the Replica Set
3. Failed to simulate PRIMARY and SECONDARY Automatic Switch
4. Repair failed nodes
5. Restore failed nodes and add them to SECONDARY
6. delete a Replica Set Node
7. Add a new Replica Set Node
System Environment Introduction:
Ubuntu 12.04. LTS 64bit Server
Initialize the file directory
~ Pwd
/Home/conan/dbs
~ Mkdir node1 node2 node3
~ Ls-l
Drwxrwxr-x 2 conan 4096 May 31 14:21 node1
Drwxrwxr-x 2 conan 4096 May 31 14:21 node2
Drwxrwxr-x 2 conan 4096 May 31 14:21 node3
Start Replica Set
Start node1, node2, node3
Mongod -- dbpath/home/conan/dbs/node1 -- port 10001 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node1.log
Mongod -- dbpath/home/conan/dbs/node2 -- port 10002 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node2.log
Mongod -- dbpath/home/conan/dbs/node3 -- port 10003 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node3.log
Replica set Initialization
~ Mongo localhost: 10001
MongoDB shell version: 2.4.3
Connecting to: localhost: 10001/test
> Rs. initiate ({_ id: "blort", members :[
{_ Id: 1, host: "localhost: 10001 "},
{_ Id: 2, host: "localhost: 10002 "},
{_ Id: 3, host: "localhost: 10003 "},
]})
{
"Info": "Config now saved locally. shocould come online in about a minute .",
"OK": 1
}
View log information: node1 is changed to PRIMARY, node2, and node3 are two SECONDARY
Fri May 31 14:26:44. 728 [conn2] ******
Fri May 31 14:26:44. 728 [conn2] replSet info saving a newer config version to local. system. replset
Fri May 31 14:26:44. 741 [conn2] replSet saveConfigLocally done
Fri May 31 14:26:44. 741 [conn2] replSet replSetInitiate config now saved locally. shocould come online in about a minute.
Fri May 31 14:26:44. 741 [conn2] command admin. $ cmd command: {replSetInitiate: {_ id: "blort", members: [{_ id: 1.0, host: "localhost: 10001"}, {_ id: 2.0, host: "localhost: 10002"}, {_ id: 3.0, host: "localhost: 10003"}]} ntoreturn: 1 keyUpdates: 0 locks (micros) W: 646741 reslen: 112 652 ms
Fri May 31 14:26:53. 682 [rsStart] replSet I am localhost: 10001
Fri May 31 14:26:53. 682 [rsStart] replSet STARTUP2
Fri May 31 14:26:53. 683 [rsHealthPoll] replSet member localhost: 10002 is up
Fri May 31 14:26:53. 684 [rsHealthPoll] replSet member localhost: 10003 is up
Fri May 31 14:26:54. 285 [initandlisten] connection accepted from 127.0.0.1: 46469 #3 (3 connections now open)
Fri May 31 14:26:54. 683 [rsSync] replSet SECONDARY
Use the mongo client to view settings
Node1 connection
~ Mongo localhost: 10001
MongoDB shell version: 2.4.3
Connecting to: localhost: 10001/test
Blort: PRIMARY> rs. status ()
{
"Set": "blort ",
"Date": ISODate ("2013-05-31T06: 34: 12Z "),
"MyState": 1,
"Members ":[
{
"_ Id": 1,
"Name": "localhost: 10001 ",
"Health": 1,
"State": 1,
"StateStr": "PRIMARY ",
"Uptime": 659,
"Optime ":{
"T": 1369981604,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 26: 44Z "),
"Self": true
},
{
"_ Id": 2,
"Name": "localhost: 10002 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 439,
"Optime ":{
"T": 1369981604,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 26: 44Z "),
"LastHeartbeat": ISODate ("2013-05-31T06: 34: 11Z "),
"LastHeartbeatRecv": ISODate ("1970-01-01T00: 00: 00Z "),
"PingMs": 0,
"SyncingTo": "localhost: 10001"
},
{
"_ Id": 3,
"Name": "localhost: 10003 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 439,
"Optime ":{
"T": 1369981604,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 26: 44Z "),
"LastHeartbeat": ISODate ("2013-05-31T06: 34: 11Z "),
"LastHeartbeatRecv": ISODate ("1970-01-01T00: 00: 00Z "),
"PingMs": 0,
"SyncingTo": "localhost: 10001"
}
],
"OK": 1
}
Node2 connection
~ Mongo localhost: 10002
MongoDB shell version: 2.4.3
Connecting to: localhost: 10002/test
Blort: SECONDARY>
Insert data in primary:
~ Mongo localhost: 10001
MongoDB shell version: 2.4.3
Connecting to: localhost: 10001/test
Blort: PRIMARY> show dbs
Local 1.078125 GB
Blort: PRIMARY> use fensme
Switched to db fensme
Blort: PRIMARY> db. user. insert ({uid: 10001 })
Blort: PRIMARY> db. user. find ()
{"_ Id": ObjectId ("51a8454813321f05df62a8c8"), "uid": 10001}
Connect to node2 and SECONDARY to query data
~ Mongo localhost: 10002
MongoDB shell version: 2.4.3
Connecting to: localhost: 10002/test
Blort: SECONDARY> show dbs
Fensme 0.203125 GB
Local 1.078125 GB
Blort: SECONDARY> use fensme
Switched to db fensme
Blort: SECONDARY> show collections
Fri May 31 14:39:22. 276 JavaScript execution failed: error: {"$ err": "not master and slaveOk = false", "code": 13435} at src/mongo/shell/query. js: L128
Blort: SECONDARY> db. user. find ()
Error: {"$ err": "not master and slaveOk = false", "code": 13435}
The system prompts whether the master/slave structure is correct. SECONDARY does not allow queries.
Failed to simulate PRIMARY, SECONDARY Automatic Switch
Ps-aux | grep 0000d
Conan 5110 0.4 2.1 1723616 44608? Sl mongod -- dbpath/home/conan/dbs/node1 -- port 10001 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node1.log
Conan 5173 0.4 2.2 1693812 45960? Sl mongod -- dbpath/home/conan/dbs/node2 -- port 10002 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node2.log
Conan 5218 0.4 2.2 1692800 45548? Sl mongod -- dbpath/home/conan/dbs/node3 -- port 10003 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node3.log
Kill-9 5110
Ps-aux | grep 0000d
Conan 5173 0.4 2.2 1703048 46096? Sl mongod -- dbpath/home/conan/dbs/node2 -- port 10002 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node2.log
Conan 5218 0.4 2.2 1702036 45696? Sl mongod -- dbpath/home/conan/dbs/node3 -- port 10003 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node3.log
Connect to node2, and the SECONDARY query node is re-elected
~ Mongo localhost: 10002
MongoDB shell version: 2.4.3
Connecting to: localhost: 10002/test
Blort: SECONDARY> rs. status ()
{
"Set": "blort ",
"Date": ISODate ("2013-05-31T06: 42: 51Z "),
"MyState": 2,
"SyncingTo": "localhost: 10003 ″,
"Members ":[
{
"_ Id": 1,
"Name": "localhost: 10001 ",
"Health": 0,
"State": 8,
"StateStr": "(not reachable/healthy )",
"Uptime": 0,
"Optime ":{
"T": 1369982280,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 38: 00Z "),
"LastHeartbeat": ISODate ("2013-05-31T06: 42: 51Z "),
"LastHeartbeatRecv": ISODate ("1970-01-01T00: 00: 00Z "),
"PingMs": 0
},
{
"_ Id": 2,
"Name": "localhost: 10002 ",
"Health": 1,
"State": 2,
"StateStr": "SECONDARY ",
"Uptime": 1088,
"Optime ":{
"T": 1369982280,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 38: 00Z "),
"Errmsg": "syncing to: localhost: 10003 ",
"Self": true
},
{
"_ Id": 3,
"Name": "localhost: 10003 ",
"Health": 1,
"State": 1,
"StateStr": "PRIMARY ",
"Uptime": 946,
"Optime ":{
"T": 1369982280,
"I": 1
},
"OptimeDate": ISODate ("2013-05-31T06: 38: 00Z "),
"LastHeartbeat": ISODate ("2013-05-31T06: 42: 51Z "),
"LastHeartbeatRecv": ISODate ("1970-01-01T00: 00: 00Z "),
"PingMs": 0,
"SyncingTo": "localhost: 10001"
}
],
"OK": 1
}
View results:
Localhost: 10001: not reachable/healthy
Localhost: 10002: SECONDARY
Localhost: 10003: PRIMARY
Connect node3, localhost: 10003 to view Data
Mongo localhost: 10003
MongoDB shell version: 2.4.3
Connecting to: localhost: 10003/test
Blort: PRIMARY> show dbs
Fensme 0.203125 GB
Local 1.078125 GB
Blort: PRIMARY> use fensme
Switched to db fensme
Blort: PRIMARY> show collections
System. indexes
User
Blort: PRIMARY> db. user. find ()
{"_ Id": ObjectId ("51a8454813321f05df62a8c8"), "uid": 10001}
After node1 becomes invalid, node3 is selected as the PRIMARY and data can be queried in node3.
Repair failed nodes
Restart node1 directly. Failed to start:
~ Mongod -- dbpath/home/conan/dbs/node1 -- port 10001 -- replSet blort -- nojournal -- fork -- logpath/home/conan/dbs/node1_restart.log
MongoDB starting: pid = 8544 port = 10001 dbpath =/home/conan/dbs/node1 64-bit host = u1
Fri May 31 14:49:37. 280 [initandlisten] db version v2.4.3
Fri May 31 14:49:37. 280 [initandlisten] git version: fe1743177a5ea03e91e0052fb5e2cb2945f6d95f
Fri May 31 14:49:37. 280 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2. ec2.v1.2. fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION = listen 49
Fri May 31 14:49:37. 280 [initandlisten] allocator: tcmalloc
Fri May 31 14:49:37. 280 [initandlisten] options: {dbpath: "/home/conan/dbs/node1", fork: true, logpath: "/home/conan/dbs/node1_restart.log", nojournal: true, port: 10001, replSet: "blort "}
**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Fri May 31 14:49:37. 281 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Fri May 31 14:49:37. 281 dbexit:
Fri May 31 14:49:37. 281 [initandlisten] shutdown: going to close listening sockets...
Fri May 31 14:49:37. 281 [initandlisten] shutdown: going to flush diaglog...
Fri May 31 14:49:37. 281 [initandlisten] shutdown: going to close sockets...
Fri May 31 14:49:37. 281 [initandlisten] shutdown: waiting for fs preallocator...
Fri May 31 14:49:37. 281 [initandlisten] shutdown: closing all files...
Fri May 31 14:49:37. 281 [initandlisten] closeAllFiles () finished
Fri May 31 14:49:37. 281 dbexit: really exiting now
MongoDB details: click here
MongoDB: click here
MongoDB backup and recovery
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
How to create a new database and set in MongoDB
MongoDB beginners must read (both concepts and practices)
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]