天天看點

mongodb高可用Replica Set

***************************************************************
第一部分:系統配置
***************************************************************

---0.配置yum源

cd /etc/yum.repos.d

mv CentOS-Base.repo CentOS-Base.repo.old


wget http://mirrors.163.com/.help/CentOS6-Base-163.repo


yum makecache


檢查可更新的rpm包
#yum check-update
    
更新所有的rpm包
#yum update


---1.安全加強

1.1 SELinux
/usr/sbin/sestatus -v | grep "SELinux status"

vi /etc/selinux/config

SELINUX=disabled







1.2 限制哪些賬戶能切換到root

1) #vi /etc/pam.d/su

auth required /lib/security/pam_wheel.so group=dba

groupadd dba
useradd -g dba  sysadmin

或将使用者修改組:
#usermod -Gdba sysadmin  将sysadmin使用者加入到dba組

passwd sysadmin


1.3 修改ssh端口、禁止root賬戶遠端登入

#vi /etc/ssh/sshd_config 
Port  16335
PermitRootLogin no
PermitEmptyPasswords no

# /etc/init.d/sshd restart





1.4 允許普通使用者執行sudo
vi /etc/sudoers

98 root    ALL=(ALL)       ALL
99 sysadmin    ALL=(ALL)       ALL
109 sysadmin    ALL=(ALL) NOPASSWD:      ALL  #不需輸入密碼,直接切換即可



$ sudo su -


-----設定别名讓普通使用者快速切換到root
echo "alias sudor=\"sudo su -\"" >> ~/.bash_profile 

source ~/.bash_profile 


1.5 rz 
yum install lrzsz -y 




---2.在proc中關閉NUMA
rpm   -qa | grep numactl

yum install -y numactl

# echo 0 > /proc/sys/vm/zone_reclaim_mode  
# sysctl -w vm.zone_reclaim_mode=0


---3.修改最大連接配接數

#vi /etc/security/limits.conf 

*            soft    nofile  25000
*            hard    nofile  25000


---4.關閉防火牆

chkconfig --level 123456 iptables off

service iptables stop


---5.修改hosts

vi /etc/hosts

192.168.50.110 mg01 mg01.atalas.com
192.168.50.120 mg02 mg02.atalas.com
192.168.50.130 mg03 mg03.atalas.com




***************************************************************
第二部分:mongodb安裝
***************************************************************

---1.安裝mongodb

--1.1 安裝openssl
yum install -y openssl-devel openssl


--1.2 安裝mongodb

mkdir /soft && cd /soft && ls && rz

tar xvzf mongodb-linux-x86_64-rhel62-3.0.5.gz 

mkdir -p /data/mongodb
mkdir -p /data/mongodb/db
mkdir -p /data/mongodb/logs
mkdir -p /data/mongodb/apps

mkdir -p /data/mongodb/{db,logs,apps} 

touch /data/mongodb/logs/mongodb.log
chmod -R 777 /data/mongodb/logs/mongodb.log

mv mongodb-linux-x86_64-rhel62-3.0.5  /data/mongodb/apps/mongodb





cd /data/mongodb/apps/mongodb/bin
vi /data/mongodb/apps/mongodb/bin/mongodb.conf

#mg01
port=27017 #端口
dbpath= /data/mongodb/db #資料檔案存放目錄
logpath= /data/mongodb/logs/mongodb.log #日志檔案存放目錄
logappend=true #使用追加的方式寫日志
fork=true #以守護程式的方式啟用,即在背景運作
maxConns=5000 #最大同時連接配接數 預設2000
bind_ip=127.0.0.1,192.168.50.110 #隻允許通過區域網路IP192.168.50.110及本機通路
noauth=true #不啟用驗證
nohttpinterface=true
rest=false
syncdelay=60



#mg02
port=27017 #端口
dbpath= /data/mongodb/db #資料檔案存放目錄
logpath= /data/mongodb/logs/mongodb.log #日志檔案存放目錄
logappend=true #使用追加的方式寫日志
fork=true #以守護程式的方式啟用,即在背景運作
maxConns=5000 #最大同時連接配接數 預設2000
bind_ip=127.0.0.1,192.168.50.120 #隻允許通過區域網路IP192.168.50.120及本機通路
noauth=true #不啟用驗證
nohttpinterface=true
rest=false
syncdelay=60


#mg03
port=27017 #端口
dbpath= /data/mongodb/db #資料檔案存放目錄
logpath= /data/mongodb/logs/mongodb.log #日志檔案存放目錄
logappend=true #使用追加的方式寫日志
fork=true #以守護程式的方式啟用,即在背景運作
maxConns=5000 #最大同時連接配接數 預設2000
bind_ip=127.0.0.1,192.168.50.130 #隻允許通過區域網路IP192.168.50.130及本機通路
noauth=true #不啟用驗證
nohttpinterface=true
rest=false
syncdelay=60

--1.3禁用hugepage

echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

--1.4 啟動mongodb

所有節點:

#numactl --interleave=all /data/mongodb/apps/mongodb/bin/mongod \
--config /data/mongodb/apps/mongodb/bin/mongodb.conf --replSet wind 

about to fork child process, waiting until server is ready for connections.
forked process: 3765
child process started successfully, parent exiting




---1.5mongodb連接配接

# /data/mongodb/apps/mongodb/bin/mongo
MongoDB shell version: 3.0.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings: 
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] 
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] 
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] 
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-08-03T12:39:33.996+0800 I CONTROL  [initandlisten] 
> 

/******************解決辦法

不重新開機伺服器的情況下解決辦法,在Linux下執行:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag


伺服器重新開機後立即生效辦法:
# vi /etc/rc.local 
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

********************/



----1.6正常停止方法:
# ps aux | grep mongod
root      3765  1.9  0.1 500832 58224 ?        Sl   12:38   0:02 /data/mongodb/apps/mongodb/bin/mongod --config /data/mongodb/apps/mongodb/bin/mongodb.conf


# kill  -2 3765
或
# /data/mongodb/apps/mongodb/bin/mongo -port 27107
> use  admin;  
> db.shutdownServer(); 






----1.7 開機自動啟動mongodb 

# vi /etc/rc.d/rc.local

#啟動mongodb 
rm -rf /data/mongodb/db/mongod.lock
numactl --interleave=all /data/mongodb/apps/mongodb/bin/mongod --config /data/mongodb/apps/mongodb/bin/mongodb.conf --replSet wind 

#hugepage
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

說明:啟動時必須加numactl --interleave=all




***************************************************************
第三部分:副本集的配置
***************************************************************
0.變量
echo "alias date='date \"+%Y-%m-%d %H:%M:%S\" ' ">>~/.bash_profile && source ~/.bash_profile

echo "export PATH=/data/mongodb/apps/mongodb/bin:$PATH" >>/etc/profile && source /etc/profile


1.等到三台機器都啟動完了之後。使用mongo用戶端登入其中一台mongod伺服器


mongo --port 27017

use admin;
config = {_id:"wind",members:[
... {_id:0,host:"192.168.50.110:27017"},
... {_id:1,host:"192.168.50.120:27017"},
... {_id:2,host:"192.168.50.130:27017"}]
};

#輸出結果:
{
	"_id" : "wind",
	"members" : [
		{
			"_id" : 0,
			"host" : "192.168.50.110:27017"
		},
		{
			"_id" : 1,
			"host" : "192.168.50.120:27017"
		},
		{
			"_id" : 2,
			"host" : "192.168.50.130:27017"
		}
	]
}





#初始化副本集配置
rs.initiate(config);

#輸出結果:
{ "ok" : 1 }


#檢視叢集節點的狀态
rs.status();
{
	"set" : "wind",
	"date" : ISODate("2015-08-04T06:08:38.151Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.50.110:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 1604,
			"optime" : Timestamp(1438668391, 1),
			"optimeDate" : ISODate("2015-08-04T06:06:31Z"),
			"electionTime" : Timestamp(1438668395, 1),
			"electionDate" : ISODate("2015-08-04T06:06:35Z"),
			"configVersion" : 1,
			"self" : true
		},
		{
			"_id" : 1,
			"name" : "192.168.50.120:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 126,
			"optime" : Timestamp(1438668391, 1),
			"optimeDate" : ISODate("2015-08-04T06:06:31Z"),
			"lastHeartbeat" : ISODate("2015-08-04T06:08:36.784Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T06:08:37.012Z"),
			"pingMs" : 6,
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.50.130:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 126,
			"optime" : Timestamp(1438668391, 1),
			"optimeDate" : ISODate("2015-08-04T06:06:31Z"),
			"lastHeartbeat" : ISODate("2015-08-04T06:08:37.010Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T06:08:37.006Z"),
			"pingMs" : 3,
			"configVersion" : 1
		}
	],
	"ok" : 1
}






***************************************************************
第四部分:副本集的驗證測試
***************************************************************
-------1.測試副本集資料複制功能

---1.1 主節點192.168.50.110:
mongo  --host 127.0.0.1 --port 27017



#建立test 資料庫。
use test;
 
#tblorders表插入資料
db.tblorders.insert( { orderno: "A2014089901", pname: "tblorders", scity:"beijing",price : 670 } );
db.tblorders.insert( { orderno: "A2014089902", pname: "snow", scity:"成都" ,price : 1270} );
db.tblorders.insert( { orderno: "A2014089903", pname: "kiki", scity:"重慶",price : 9780 } );

wind:PRIMARY> db.tblorders.find().forEach(printjson);
{
	"_id" : ObjectId("55c05976985cda7c357bccd0"),
	"orderno" : "A2014089901",
	"pname" : "tblorders",
	"scity" : "beijing",
	"price" : 670
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd1"),
	"orderno" : "A2014089902",
	"pname" : "snow",
	"scity" : "成都",
	"price" : 1270
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd2"),
	"orderno" : "A2014089903",
	"pname" : "kiki",
	"scity" : "重慶",
	"price" : 9780
}


 
--1.2 副本節點192.168.5.120
mongo 192.168.50.120:27017
mongo  --host 127.0.0.1 --port 27017

 
#使用jinri資料庫。
wind:SECONDARY> use jinri;
switched to db jinri
wind:SECONDARY> show tables;
2015-08-04T14:22:48.436+0800 E QUERY    Error: listCollections failed: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
    at Error (<anonymous>)
    at DB._getCollectionInfosCommand (src/mongo/shell/db.js:646:15)
    at DB.getCollectionInfos (src/mongo/shell/db.js:658:20)
    at DB.getCollectionNames (src/mongo/shell/db.js:669:17)
    at shellHelper.show (src/mongo/shell/utils.js:625:12)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/db.js:646



#mongodb預設是從主節點讀寫資料的,副本節點上不允許讀,需要設定副本節點可以讀。
wind:SECONDARY>  db.getMongo().setSlaveOk();
wind:SECONDARY> show tables;
system.indexes
tblorders

wind:SECONDARY> db.tblorders.find().forEach(printjson)
{
	"_id" : ObjectId("55c05976985cda7c357bccd0"),
	"orderno" : "A2014089901",
	"pname" : "tblorders",
	"scity" : "beijing",
	"price" : 670
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd1"),
	"orderno" : "A2014089902",
	"pname" : "snow",
	"scity" : "成都",
	"price" : 1270
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd2"),
	"orderno" : "A2014089903",
	"pname" : "kiki",
	"scity" : "重慶",
	"price" : 9780
}


--1.3 副本節點192.168.5.130

mongo 192.168.50.130:27017
mongo  --host 127.0.0.1 --port 27017

wind:SECONDARY> use jinri;
switched to db jinri
wind:SECONDARY> show tables;
2015-08-04T14:37:56.222+0800 E QUERY    Error: listCollections failed: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
    at Error (<anonymous>)
    at DB._getCollectionInfosCommand (src/mongo/shell/db.js:646:15)
    at DB.getCollectionInfos (src/mongo/shell/db.js:658:20)
    at DB.getCollectionNames (src/mongo/shell/db.js:669:17)
    at shellHelper.show (src/mongo/shell/utils.js:625:12)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/db.js:646
wind:SECONDARY> db.getMongo().setSlaveOk();
wind:SECONDARY> show tables;
system.indexes
tblorders


wind:SECONDARY> db.tblorders.find().forEach(printjson)
{
	"_id" : ObjectId("55c05976985cda7c357bccd0"),
	"orderno" : "A2014089901",
	"pname" : "tblorders",
	"scity" : "beijing",
	"price" : 670
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd1"),
	"orderno" : "A2014089902",
	"pname" : "snow",
	"scity" : "成都",
	"price" : 1270
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd2"),
	"orderno" : "A2014089903",
	"pname" : "kiki",
	"scity" : "重慶",
	"price" : 9780
}



--------2.測試副本集故障轉移功能

2.1 關閉110節點

mongo 192.168.50.110:27017
use admin;
db.shutdownServer(); 

2.2自動選舉一個節點為主節點130

2015-08-04T14:52:43.014+0800 I NETWORK  [ReplExecNetThread-4] Socket recv() timeout  192.168.50.110:27017
2015-08-04T14:52:43.014+0800 I NETWORK  [ReplExecNetThread-4] SocketException: remote: 192.168.50.110:27017 error: 9001 socket exception [RECV_TIMEOUT] server [192.168.50.110:27017] 
2015-08-04T14:52:43.014+0800 I NETWORK  [ReplExecNetThread-4] DBClientCursor::init call() failed
2015-08-04T14:52:43.062+0800 I REPL     [ReplicationExecutor] Error in heartbeat request to 192.168.50.110:27017; Location10276 DBClientBase::findN: transport error: 192.168.50.110:27017 ns: admin.$cmd query: { replSetHeartbeat: "wind", pv: 1, v: 1, from: "192.168.50.130:27017", fromId: 2, checkEmpty: false }
2015-08-04T14:52:43.062+0800 I REPL     [ReplicationExecutor] Standing for election
2015-08-04T14:52:43.064+0800 I REPL     [ReplicationExecutor] replSet possible election tie; sleeping 90ms until 2015-08-04T14:52:43.154+0800
2015-08-04T14:52:43.154+0800 I REPL     [ReplicationExecutor] Standing for election
2015-08-04T14:52:43.163+0800 I REPL     [ReplicationExecutor] replSet info electSelf
2015-08-04T14:52:43.165+0800 I REPL     [ReplicationExecutor] received vote: 1 votes from 192.168.50.120:27017
2015-08-04T14:52:43.165+0800 I REPL     [ReplicationExecutor] replSet election succeeded, assuming primary role
2015-08-04T14:52:43.165+0800 I REPL     [ReplicationExecutor] transition to PRIMARY
2015-08-04T14:52:44.387+0800 I NETWORK  [conn167] end connection 192.168.50.120:51506 (2 connections now open)
2015-08-04T14:52:44.388+0800 I NETWORK  [initandlisten] connection accepted from 192.168.50.120:51507 #169 (3 connections now open)
2015-08-04T14:52:50.112+0800 W NETWORK  [ReplExecNetThread-5] Failed to connect to 192.168.50.110:27017 after 5000 milliseconds, giving up.


2.3 新的主節點130插入資料

db.tblorders.insert( { orderno: "110", pname: "jyl", scity:"重慶",price : 2780 } );

2.4 檢查120節點資料是否同步


mongo 192.168.50.120:27017

db.getMongo().setSlaveOk();

wind:SECONDARY> db.tblorders.find().forEach(printjson)
{
	"_id" : ObjectId("55c05976985cda7c357bccd0"),
	"orderno" : "A2014089901",
	"pname" : "tblorders",
	"scity" : "beijing",
	"price" : 670
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd1"),
	"orderno" : "A2014089902",
	"pname" : "snow",
	"scity" : "成都",
	"price" : 1270
}
{
	"_id" : ObjectId("55c05976985cda7c357bccd2"),
	"orderno" : "A2014089903",
	"pname" : "kiki",
	"scity" : "重慶",
	"price" : 9780
}
{
	"_id" : ObjectId("55c05e3a985cda7c357bccd3"),
	"orderno" : "A2014089904",
	"pname" : "atalas",
	"scity" : "烏魯木齊",
	"sdate" : "2015-08-08"
}
{
	"_id" : ObjectId("55c062ad929d01d23682d297"),
	"orderno" : "110",
	"pname" : "jyl",
	"scity" : "重慶",
	"price" : 2780
}
wind:SECONDARY> 


2.5  重新啟動110節點

#建議将下面的内容設定為自動啟動
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

#啟動節點
numactl --interleave=all /data/mongodb/apps/mongodb/bin/mongod --config /data/mongodb/apps/mongodb/bin/mongodb.conf --replSet wind 


mongo 192.168.50.110:27017

db.getMongo().setSlaveOk();


db.tblorders.find().forEach(printjson)





--------3.副本節點删除

wind:PRIMARY> rs.status();
{
	"set" : "wind",
	"date" : ISODate("2015-08-04T07:24:17.085Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 0,
			"name" : "192.168.50.110:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 414,
			"optime" : Timestamp(1438671533, 1),
			"optimeDate" : ISODate("2015-08-04T06:58:53Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:24:15.313Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:24:15.113Z"),
			"pingMs" : 9,
			"configVersion" : 1
		},
		{
			"_id" : 1,
			"name" : "192.168.50.120:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1062,
			"optime" : Timestamp(1438671533, 1),
			"optimeDate" : ISODate("2015-08-04T06:58:53Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:24:15.766Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:24:15.111Z"),
			"pingMs" : 1,
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.50.130:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 4753,
			"optime" : Timestamp(1438671533, 1),
			"optimeDate" : ISODate("2015-08-04T06:58:53Z"),
			"electionTime" : Timestamp(1438671994, 1),
			"electionDate" : ISODate("2015-08-04T07:06:34Z"),
			"configVersion" : 1,
			"self" : true
		}
	],
	"ok" : 1
}



#删除節點110
rs.remove("192.168.50.110:27017");

wind:PRIMARY> rs.status();
{
	"set" : "wind",
	"date" : ISODate("2015-08-04T07:25:11.988Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.50.120:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1117,
			"optime" : Timestamp(1438671533, 1),
			"optimeDate" : ISODate("2015-08-04T06:58:53Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:25:10.003Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:25:11.545Z"),
			"pingMs" : 12,
			"configVersion" : 1
		},
		{
			"_id" : 2,
			"name" : "192.168.50.130:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 4807,
			"optime" : Timestamp(1438673110, 1),
			"optimeDate" : ISODate("2015-08-04T07:25:10Z"),
			"electionTime" : Timestamp(1438671994, 1),
			"electionDate" : ISODate("2015-08-04T07:06:34Z"),
			"configVersion" : 2,
			"self" : true
		}
	],
	"ok" : 1
}



#測試130主節點加入資料是否同步到120

use wind;
db.tblorders.insert( { orderno: "1001", pname: "jinri", scity:"pek",price : 1650 } );


mongo 192.168.50.120:27017

db.getMongo().setSlaveOk();

wind:SECONDARY> db.tblorders.find().forEach(printjson)
{
	"_id" : ObjectId("55c0693a0bef81df34afc6d2"),
	"orderno" : "1001",
	"pname" : "jinri",
	"scity" : "pek",
	"price" : 1650
}




--------4.副本節點添加

wind:PRIMARY> rs.status();
{
	"set" : "wind",
	"date" : ISODate("2015-08-04T07:34:01.731Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.50.120:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1647,
			"optime" : Timestamp(1438673359, 2),
			"optimeDate" : ISODate("2015-08-04T07:29:19Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:34:01.589Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:34:01.338Z"),
			"pingMs" : 1,
			"syncingTo" : "192.168.50.130:27017",
			"configVersion" : 2
		},
		{
			"_id" : 2,
			"name" : "192.168.50.130:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 5337,
			"optime" : Timestamp(1438673359, 2),
			"optimeDate" : ISODate("2015-08-04T07:29:19Z"),
			"electionTime" : Timestamp(1438671994, 1),
			"electionDate" : ISODate("2015-08-04T07:06:34Z"),
			"configVersion" : 2,
			"self" : true
		}
	],
	"ok" : 1
}

#增加節點
rs.add("192.168.50.110:27017");

wind:PRIMARY> rs.status();
{
	"set" : "wind",
	"date" : ISODate("2015-08-04T07:34:39.529Z"),
	"myState" : 1,
	"members" : [
		{
			"_id" : 1,
			"name" : "192.168.50.120:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 1685,
			"optime" : Timestamp(1438673676, 1),
			"optimeDate" : ISODate("2015-08-04T07:34:36Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:34:38.789Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:34:39.514Z"),
			"pingMs" : 6,
			"configVersion" : 3
		},
		{
			"_id" : 2,
			"name" : "192.168.50.130:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 5375,
			"optime" : Timestamp(1438673676, 1),
			"optimeDate" : ISODate("2015-08-04T07:34:36Z"),
			"electionTime" : Timestamp(1438671994, 1),
			"electionDate" : ISODate("2015-08-04T07:06:34Z"),
			"configVersion" : 3,
			"self" : true
		},
		{
			"_id" : 3,
			"name" : "192.168.50.110:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 0,
			"optime" : Timestamp(1438673676, 1),
			"optimeDate" : ISODate("2015-08-04T07:34:36Z"),
			"lastHeartbeat" : ISODate("2015-08-04T07:34:38.791Z"),
			"lastHeartbeatRecv" : ISODate("2015-08-04T07:34:38.790Z"),
			"pingMs" : 6,
			"syncingTo" : "192.168.50.130:27017",
			"configVersion" : 3
		}
	],
	"ok" : 1
}


#130插入資料驗證
use wind;
db.tblorders.insert( { orderno: "1002", pname: "jinri", scity:"pvg",price : 1750 } );
wind:PRIMARY> db.tblorders.find().forEach(printjson);
{
	"_id" : ObjectId("55c069cf0bef81df34afc6d3"),
	"orderno" : "1001",
	"pname" : "jinri",
	"scity" : "pek",
	"price" : 1650
}
{
	"_id" : ObjectId("55c06dcd1449f1bbe0a56e9b"),
	"orderno" : "1002",
	"pname" : "jinri",
	"scity" : "pvg",
	"price" : 1750
}


#110和120驗證

mongo 192.168.50.110:27017

wind:SECONDARY> rs.slaveOk();
wind:SECONDARY> use wind;
switched to db wind
wind:SECONDARY> db.tblorders.find().forEach(printjson);
{
	"_id" : ObjectId("55c069cf0bef81df34afc6d3"),
	"orderno" : "1001",
	"pname" : "jinri",
	"scity" : "pek",
	"price" : 1650
}
{
	"_id" : ObjectId("55c06dcd1449f1bbe0a56e9b"),
	"orderno" : "1002",
	"pname" : "jinri",
	"scity" : "pvg",
	"price" : 1750
}


wind:SECONDARY> rs.slaveOk();
wind:SECONDARY> use wind;
switched to db wind
wind:SECONDARY> db.tblorders.find().forEach(printjson);
{
	"_id" : ObjectId("55c069cf0bef81df34afc6d3"),
	"orderno" : "1001",
	"pname" : "jinri",
	"scity" : "pek",
	"price" : 1650
}
{
	"_id" : ObjectId("55c06dcd1449f1bbe0a56e9b"),
	"orderno" : "1002",
	"pname" : "jinri",
	"scity" : "pvg",
	"price" : 1750
}