符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
1、简介
创新互联建站是专业的承德县网站建设公司,承德县接单;提供做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行承德县网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
MongoDB复制是将数据同步在多个服务器的过程,类似MySQL Replication是一种异步复制;
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
2、MongoDB复制特点
(1) mongodb的节点有三种角色:primary,seconfary,arbiter,
primary负责处理客户端请求,读写数据;
secondary通过oplog复制主节点上的数据,也可读数据;
arbiter节点不存储数据,仅投票功能,所以不需要单独的硬件配置;
(2)如果primary不可以用,secondary会提升为新的primary; 由参数electionTimeoutMillis=10s控制
3、实验环境
MongoDB-3.0.6版本
一台物理机器上三个MongoDB实例,分别占用三个节点
172.16.101.54 27017
172.16.101.54 27018
172.16.101.54 27019
4、创建数据和日志目录
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/data/{db27017,db27018,db27019}
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/log
5、配置文件/etc/mongod.conf
三个节点分别创建配置文件,文件内容区别是节点不同,其他都相同,参数replSetname用来指定副本集名称为rs0
[root@sht-sgmhadoopcm-01 mongodb]# ll /etc/mongod2701*.conf
-rw-r--r-- 1 root root 405 Nov 2 20:21 /etc/mongod27017.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27018.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27019.conf
mongodb配置文件每个冒号后面要有一个空格,否则会报错
[root@sht-sgmhadoopcm-01 mongodb]# cat /etc/mongod27017.conf systemLog: destination: file path: "/usr/local/mongodb/log/mongod27017.log" logAppend: true storage: dbPath: /usr/local/mongodb/data/db27017 journal: enabled: true processManagement: fork: true pidFilePath: /usr/local/mongodb/data/db27017/mongod27017.pid net: port: 27017 bindIp: 0.0.0.0 setParameter: enableLocalhostAuthBypass: false replication: replSetName: rs0
6、启动MongoDB
分别启动三个节点的MongoDB服务,并指定配置文件
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27018.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27019.conf
7、初始化副本集
登录27017节点
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "sht-sgmhadoopcm-01:27017",
"ok" : 1
}
添加成员到副本集,priority大的节点提成为主的可能性就大
rs0:PRIMARY> rs.add('172.16.101.54:27018')
{ "ok" : 1 }
rs0:PRIMARY> rs.add('172.16.101.54:27019',priority:5)
{ "ok" : 1 }
查看副本集配置
rs0:PRIMARY> rs.conf() { "_id" : "rs0", "version" : 3, "members" : [ { "_id" : 0, "host" : "sht-sgmhadoopcm-01:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "172.16.101.54:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "172.16.101.54:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }
查看各个角色状态
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T12:27:44.570Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 626, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "electionTime" : Timestamp(1541161465, 2), "electionDate" : ISODate("2018-11-02T12:24:25Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 161, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.311Z"), "pingMs" : 0, "syncingTo" : "sht-sgmhadoopcm-01:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 75, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.430Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 3 } ], "ok" : 1 }
8、数据同步测试
向27017节点插入数据
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
rs0:PRIMARY> db.user.insert({"username":"test","age":12,"sex":"m"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
数据成功的同步到27018和27019节点
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27019/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
9、AutoFailover测试
kill当前的primary27017节点
[root@sht-sgmhadoopcm-01 mongodb]# ps -ef|grep mongod
root 5703 1 0 20:17 ? 00:00:52 bin/mongod --config /etc/mongod27017.conf
root 5747 1 0 20:17 ? 00:00:51 bin/mongod --config /etc/mongod27018.conf
root 5979 1 0 20:20 ? 00:00:52 bin/mongod --config /etc/mongod27019.conf
[root@sht-sgmhadoopcm-01 mongodb]# kill -9 5703
可以看到27019变成primary,此时27017不可用
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T15:10:18.746Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2018-11-02T15:10:18.631Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:09:28.583Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to sht-sgmhadoopcm-01:27017; couldn't connect to server sht-sgmhadoopcm-01:27017 (172.16.101.54), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 10349, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "configVersion" : 3, "self" : true }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 9827, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:10:18.611Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:10:18.592Z"), "pingMs" : 0, "electionTime" : Timestamp(1541171370, 1), "electionDate" : ISODate("2018-11-02T15:09:30Z"), "configVersion" : 3 } ], "ok" : 1 }
重新启动27017节点,会自动变成了27019的备库,说明auto failover成功了
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T15:13:27.035Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 42, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:13:26.764Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:25.055Z"), "pingMs" : 0, "configVersion" : 3 }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 10017, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:13:26.662Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:26.678Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 3 }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 10349, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "electionTime" : Timestamp(1541171370, 1), "electionDate" : ISODate("2018-11-02T15:09:30Z"), "configVersion" : 3, "self" : true } ], "ok" : 1 }
FAQ
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf &
[1] 4885
[root@sht-sgmhadoopcm-01 mongodb]# Error parsing YAML config file: yaml-cpp: error at line 8, column 11: illegal map value
try 'bin/mongod --help' for more information
[1]+ Exit 2 bin/mongod --config /etc/mongod27017.conf
原因是配置文件/etc/mongod.conf冒号后面没有跟空格。
参考链接
MongoDB Replication