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
}

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

时间: 2024-10-17 07:14:38

mongodb高可用Replica Set的相关文章

MongoDB 高可用集群架构简介

在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. 转载自严澜的博文--<如何搭建高效的MongoDB集群> NOSQL有这些优势: 大数据量,可以通过廉价服务器存储大量的数据,轻松摆脱传统mysql单表存储量级限制. 高扩展性,Nosql去掉了关系数据库的关系型特性,很容易横向扩展,摆脱了以往老是纵向扩展的诟病. 高性能,Nosql通过简单的key-value方式获取数据,非常快速.还有

MongoDB 高可用切换

MongoDB 高可用集群切换 MongoDB最简单的集群模式是三节点搭建Replica Set(副本集),这样可以保证一个节点故障后,其余节点还可以继续提供服务. 在MongoDB集群中,也存在主节点和备用节点的角色,如主节点出现问题,会通过选举在备用节点中产生一个新的主节点,其主备用节点会自动向新的主节点进行同步. 在新旧主节点完成切换后,对前端应用几乎是透明的,原因在于MongoDB特殊的连接字符串配置方式: mongodb://[username:[email protected]]ho

MongoDB 高可用集群副本集+分片搭建

MongoDB 高可用集群搭建 一.架构概况 192.168.150.129192.168.150.130192.168.150.131 参考文档:https://www.cnblogs.com/vadim/p/7100683.html mongos mongos    mongos Config   server      Config server  Config serverShared1 server 1 Shared1 server 1 副本 Shared1 server 1 仲裁/隐

Mongodb高可用架构—Replica Set 集群实战

Replica Set使用的是n个mongod节点,构建具备自动的容错功能(auto-failover),自动恢复的(auto-recovery)的高可用方案. 使用Replica Set来实现读写分离.通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作. 对于Replica Set中的secondary 节点默认是不可读的. 架构图: 分别在各服务器上运行两个mongod实例: shard11 + shard12 + shard13 -

MongoDB高可用复制集分片集群搭建

1     逻辑架构 1.1     逻辑架构图 1.2     组件说明 一.mongos(query routers):查询路由,负责client的连接,并把任务分给shards,然后收集结果.一个集群可以有多个query routers(replica sets),以分担客户端请求(负载均衡). 二.config server:配置服务器.保存了集群的元数据(比如数据放在哪个shards上),query router通过config server中的配置信息决定把任务分配到哪个shards

MongoDB高可用集群配置的方案

>>高可用集群的解决方案 高可用性即HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性. 计算机系统的高可用在不同的层面上有不同的表现: (1)网络高可用 由于网络存储的快速发展,网络冗余技术被不断提升,提高IT系统的高可用性的关键应用就是网络高可用性,网络高可用性与网络高可靠性是有区别的,网络高可用性是通过匹配冗余的网络设备实现网络设备的冗余,达到高可用的目的.比如冗余的交换机,冗余的路由器等

MongoDB高可用方案之副本集(Replica Set)

一.介绍 Replicat Set比起传统的Master - Slave结构而言,应用场景更加多,也有了自动failover的能力 二.副本集结构图(参考:https://www.linuxidc.com/Linux/2017-03/142379.htm) 类似于"MySQL中1主2从+mha_manager"的结构. Replication通过Oplog实现语句复现 三.副本集成员的属性 Replication通过Oplog实现语句复现分别为Primary.Secondary(Sec

mongodb高可用集群02---副本集

本人根据此文章进行学习:http://blog.jobbole.com/72624/ 会不断更新内容主要分为四大模块: mongodb各种方式的部署 常用使用[工作不用就没必要学了,精力有限] 性能优化 故障排除 很多会和网上资料一样,主要是自己学习不断梳理资料,追求:提及精华 1)副本集概念: 副本集合(Replica Sets),是一个基于主/从复制机制的复制功能,但增加了自动故障转移和恢复特性.一个集群最多可以支持7个服务器,并且任意节点都可以是主节点.所有的写操作都被分发到主节点,而读操

CentOS MongoDB 高可用实战

原文:https://www.sunjianhua.cn/archives/centos-mongodb.html 一.MongoDB 单节点 1.1.Windows 版安装 1.1.1 获取社区版本A. 查看Windows版本 cmd输入: B. 下载对应Windows版本地址: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 1.1.2 安装Windows社区版本 mongodb-win32-x86_6