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
}





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

本页内容版权归属为原作者,如有侵犯您的权益,请通知我们删除。
DB Name         DB Id    Instance     Inst Num Release     Cluster Host ------------ ----------- ------------ -------- ----------- ------- ------------ LIXORA          1409317108 LIXORA                1 9.2.0.1.0   NO      lixora-DATA             Snap Id  
这是有生之年系列的填坑_(:з」∠)_ 前作第一篇:http://blog.itpub.net/29510932/viewspace-1664499/ 前作第二篇:http://blog.itpub.net/29510932/viewspace-1667814/ 前作第三篇:http://blog.itpub.net/29510932/viewspace-1678591/ MyCAT基准测试:http://blog.itpub.net/29510932/viewspace-1726924/和http://
业务人员告诉我某系统磁盘IO持续高达300MB/s,系统平台为AIX,遂 topas 查看果然如此。 用下面脚本到Oracle数据库中看了一下: SELECT Disk_Reads DiskReads, Executions, SQL_ID, SQL_Text SQLText, SQL_FullText SQLFullText FROM( SELECT Disk_Reads, Executions, SQL_ID, LTRIM(SQL_Text) SQL_Text, SQL_FullText, Operat

gc服务器慢的原因分析 - 2015-08-07 14:08:38

在工作环境中有一台gc的服务器,已经好几年没有动过了,上面安装着gc的服务和数据库,也就说gc里面的HttpServer,数据库,webcache都在这台服务器上。 大家在访问gc的时候,感觉有些时候访问很慢,尽管是内网,但是还是有很大的延迟的感觉,大家认为可能是监控的机器比较多了,也就没有在意,今天我抽空查看了下这台机器,还是发现了一些问题。 首先看看gc的服务是否正常。我们也可以使用opmn来检测。 $ ./opmnctl status Processes in Instance: Enterpris
Linux 下 m ysql 编译 安装教程 # 查看 linux 发行版本号 cat /etc/issue # 查看 linux 内核版本 uname -r 本文测试环境 centOS6.6   一、 L inux 下编译安装 MySQL 前的准备工作 在线安装编译源码所需的工具和库 ( 无法联网的话要事先准备软件 , 或者系统盘 ) yum install gcc gcc-c++ ncurses-devel perl 从 mysql5.5 起源代码编译需要借助 cmake 这款编译工具。从 http:

实例讲解SQL Server加密功能 - 2015-08-07 14:08:09

SQL Server中加密是层级的,每一个上层为下提供保护。如图: 实例: /** SMK(Service Master Key)在SQL Server安装时生成,由Windows DPAPI(Data Protection API)提供保护 **/ /**创建数据库级别DMK(Database Master Key),受SMK保护**/ create master key encryption by password=N'Passw0rd' go /**数据库内的加密对象受DMK保护 支持的对称加密算法
假设我们有如下一个Excel表格:    现在要使用C#程序读取其内容:  using  System;  using  System.Data.OleDb;    namespace  Skyiv.Ben.Test  {     sealed   class  ExcelTest     {       static   void  Main()       {         try          {           using  (OleDbConnection conn  =   new
/* 因为工作中一个奇怪的需求,要用SQL语言计算两个日期间有多少个工作日。 设定是一个星期有5个工作日,从星期一到星期五 说明: 第一个星期的工作日数:DATEPART(dw, @begdt)-DATEPART(dw, @begdt),最少0天 末一个星期的工作日数:DATEPART(dw, @enddt),最多5天 计算方法: 如果两个日期处在同一个星期内,直接计算“第一个星期的工作日数” 否则按下面的公式计算 (两个日期间的总天数 - 第一个星期的天数 - 末一个星期的天数) / 7 * 5 + 第

关于mysql delete的问题小结 - 2015-08-07 04:08:30

由于mysql数据库的相关内部问题 导致delete from table where col not in (select col from table group by xx) 会提示报错 我们要做的是 复制代码 代码如下: create table tmp selete col from table group by xx; delete from table where col not in (select col from tmp); drop table tmp; 今天,在开发自己的项目中需要把

详解MySQL性能优化(二) - 2015-08-07 04:08:21

七、MySQL数据库Schema设计的性能优化 高效的模型设计 适度冗余-让Query尽两减少Join 大字段垂直分拆-summary表优化 大表水平分拆-基于类型的分拆优化 统计表-准实时优化 合适的数据类型 时间存储格式总类并不是太多,我们常用的主要就是DATETIME,DATE和TIMESTAMP这三种了。从存储空间来看TIMESTAMP最少,四个字节,而其他两种数据类型都是八个字节,多了一倍。而TIMESTAMP的缺点在于他只能存储从1970年之后的时间,而另外两种时间类型可以存放最早从1001年