码迷,mamicode.com
首页 > 数据库 > 详细

mongodb高可用Replica Set

时间:2015-08-06 18:17:51      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:mongodb高可用replica se

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

---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
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

mongodb高可用Replica Set

标签:mongodb高可用replica se

原文地址:http://blog.csdn.net/yangzhawen/article/details/47320677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!