数据库编程——MongoDB json( 六 )


$mkdir –p /data/node1
$mkdir –p /data/node2
$mkdir –p /data/
--启动三个服务
$nohup /usr/local//bin/ ---- /data/node1/ --port 9927 -- 1024 &
$nohup /usr/local//bin/ ---- /data/node2/ --port 9928 -- 1024 &
$nohup /usr/local//bin/ ---- /data// --port 9929 -- 1024 &
其中是副本集的名称,节点必须相同,–指定数据库储存路径,–port指定侦听端口,–指定数据同步之前的缓存的空间大小,暂时指定1G 。(可以设置小点,如512 。)
可以使用(shell>ps aux|grep )查看启动三个服务进程

数据库编程——MongoDB json

文章插图
选择9927端口的实例为主节点,进入9927的shell:
$mongo :9927
--初始化副本集需要配置表,申明配置表如下:
> = {_id: "", : []}
注意_id和副本集启动的共享名称一致 。下面来逐步添加节点的数据信息:
>..push({_id: 0, host: ":9927"})
>..push({_id: 1, host: ":9928"})
>..push({_id: 2, host: ":9929", : true})
也可以使用rs.add和rs.函数来实现同样的操作 。然后需要用这个表作为参数初始化副本集,在9927端口的shell执行:
> rs.()
"info" : " now saved .comein about a .",
"ok" : 1
返回ok为1表示初始化成功,三个节点互相检测通信,需要1分钟左右(反应够慢的 ^-),可以查看三个终端窗口的信息确认,完成通信后,在9927端口的shell回车执行命令确认配置:
复制代码
> rs.()
{"setName" : "application","ismaster" : true,"secondary" : false,"hosts" : ["localhost:9927","localhost:9928"],"arbiters" : ["localhost:9929"],"primary" : "localhost:9927","me" : "localhost:9927","maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"localTime" : ISODate("2016-09-04T05:23:09.359Z"),"ok" : 1}
> rs.()
注意到9927端口的实例是true,为false,hosts有2个实例,有1个元素,关键key表示了主节点,通信完成几次回车后可以看到9927的端口的实例shell的提示符已经改变,更改为:,查看更详细的信息:
:> rs.()
application:PRIMARY> rs.status(){"set" : "application","date" : ISODate("2016-09-04T05:24:16Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "localhost:9927","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 430,"optime" : Timestamp(1472966482, 1),"optimeDate" : ISODate("2016-09-04T05:21:22Z"),"self" : true},{"_id" : 1,"name" : "localhost:9928","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 170,"optime" : Timestamp(1472966482, 1),"optimeDate" : ISODate("2016-09-04T05:21:22Z"),"lastHeartbeat" : ISODate("2016-09-04T05:24:14Z"),"lastHeartbeatRecv" : ISODate("2016-09-04T05:24:15Z"),"pingMs" : 0,"syncingTo" : "localhost:9927"},{"_id" : 2,"name" : "localhost:9929","health" : 1,"state" : 7,"stateStr" : "ARBITER","uptime" : 170,"lastHeartbeat" : ISODate("2016-09-04T05:24:14Z"),"lastHeartbeatRecv" : ISODate("2016-09-04T05:24:15Z"),"pingMs" : 0}],"ok" : 1}
:> rs.()
显示了每个节点的健康状况,名称,启动的时间,节点的类型等 。查看当前副本集的配置表:
:> rs.conf()
pplication:PRIMARY> rs.conf(){"_id" : "application","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:9927"},{"_id" : 1,"host" : "localhost:9928"},{"_id" : 2,"host" : "localhost:9929","arbiterOnly" : true}]}
:> rs.conf()
》开始插入测试数据
:> db.users.({:'yekai',age:34})
:> db.users.find()
{ "_id" : ("f587"), "" : "yekai", "age" : 34 }
进入9928从节点,执行查看集合
$mongo :9928
:> show ;
Sun Sep 4 13:39:46.607 error: { "$err" : "notand =false", "code" : 13435 } at src/mongo/shell/query.js:128
发现shell抛出了异常,显示为false,当前副本集需要明确从节点参数,执行函数:
:> rs.()
:> show ;
.