成都创新互联网站制作重庆分公司

MongoDB主从+php实现-创新互联

环境

右玉网站建设公司创新互联建站,右玉网站设计制作,有大型网站制作公司丰富经验。已为右玉1000+提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的右玉做网站的公司定做!

172.16.115.202  primary

172.16.111.171  secondary

172.16.115.157  secondary

一、MongoDB安装

1. yum下载安装mongodb

  扩展源http://repo.mongodb.org/yum/redhat/mongodb-org-3.0.repo

   mongodb-org-3.0.7-1.el6.x86_64.rpm #元数据包,它可以实现自动安装下面的4个组件包

   mongodb-org-mongos-3.0.7-1.el6.x86_64.rpm # mongod守护程序,以及相关的配置和初始化脚本

   mongodb-org-server-3.0.7-1.el6.x86_64.rpm # mongos守护程序

   mongodb-org-shell-3.0.7-1.el6.x86_64.rpm # ongo shell环境

   mongodb-org-tools-3.0.7-1.el6.x86_64.rpm # MongoDB工具

2. 编辑配置文件vim  /etc/mongod.conf

  1)去掉以下两行以#开头的注释,以防重启报错

     fork:true

     pidFilePath:/var/mongodb/mongod.pid

  2)bind_ip绑定多个IP,用逗号隔开,如监听全部,留空即可

3. 修改配置参数,启动mongd

  echo never > /sys/kernel/mm/transparent_hugepage/enabled

  echo never > /sys/kernel/mm/transparent_hugepage/defrag

  编辑vim /etc/security/limits.conf,加入内容:

  mongod soft nofile 64000

  mongod hard nofile 64000

  mongod soft nproc 32000

  mongod hard nproc 32000

  service mongd start

  注:启动过程较慢,这是它在写数据/var/lib/mongo,如果启动不了,mongod -f /etc/mongod.conf方式,可以看到报错

4. 编辑三台机器的MongoDB配置文件vim /etc/mongod.conf,重启

  内容:

  ##1)去掉replication的注释

  ##2)加入下面参数(注意首行留空2个空格,固定格式):

  ## 类似于二进制日志

    oplogSizeMB: 20

  ## 副本集名字

    replSetName: huangzhenping

5. 查看日志文件tail /var/log/mongodb/mongod.log

 MongoDB主从+php实现

6. 主上配置,搭建副本集

  use admin # 主上进入admin库

  config={_id:"huangzhenping",members:[{_id:0,host:"172.16.115.202:27017"},{_id:1,host:"172.16.115.157:27017"},{_id:2,host:"172.16.111.171:27017"}]}  #配置机器

  rs.initiate(config)  #刷新

  rs.status()  #查看状态

 如下结果:出现三台机器的信息,主的状态已变为PRIMARY

> rs.initiate(config) { "ok" : 1 } huangzhenping:OTHER> rs.status() {         "set" : "huangzhenping",         "date" : ISODate("2017-01-14T20:24:59.490Z"),         "myState" : 1,         "members" : [                 {                         "_id" : 0,                         "name" : "172.16.115.202:27017",                         "health" : 1,                         "state" : 1,                         "stateStr" : "PRIMARY",                         "uptime" : 297,                         "optime" : Timestamp(1484425453, 1),                         "optimeDate" : ISODate("2017-01-14T20:24:13Z"),                         "electionTime" : Timestamp(1484425455, 1),                         "electionDate" : ISODate("2017-01-14T20:24:15Z"),                         "configVersion" : 1,                         "self" : true                 },                 {                         "_id" : 1,                         "name" : "172.16.115.157:27017",                         "health" : 1,                         "state" : 2,                         "stateStr" : "SECONDARY",                         "uptime" : 45,                         "optime" : Timestamp(1484425453, 1),                         "optimeDate" : ISODate("2017-01-14T20:24:13Z"),                         "lastHeartbeat" : ISODate("2017-01-14T20:24:57.851Z"),                         "lastHeartbeatRecv" : ISODate("2017-01-14T20:24:58.343Z")                  ,                         "pingMs" : 0,                         "configVersion" : 1                 },                 {                         "_id" : 2,                         "name" : "172.16.111.171:27017",                         "health" : 1,                         "state" : 2,                         "stateStr" : "SECONDARY",                         "uptime" : 45,                         "optime" : Timestamp(1484425453, 1),                         "optimeDate" : ISODate("2017-01-14T20:24:13Z"),                         "lastHeartbeat" : ISODate("2017-01-14T20:24:57.863Z"),                         "lastHeartbeatRecv" : ISODate("2017-01-14T20:24:58.262Z")                  ,                         "pingMs" : 1,                         "configVersion" : 1                 }         ],         "ok" : 1 } huangzhenping:PRIMARY>

  注:

    1)如执行rs.status()命令,没有发现三台机器,需手动执行添加

      rs.add("172.16.115.157")

      rs.add("172.16.111.171")

    2)如从机的状态为"stateStr":"STARTUP",则需执行:

      var config={_id:"huangzhenping",members:[{_id:0,host:"172.16.115.202:27017"},{_id:1,host:"172.16.115.157:27017"},{_id:2,host:"172.16.111.171:27017"}]}

      rs.initiate(config)

7. 从上登录,状态已变为:SECONDARY

 MongoDB主从+php实现

二、MongoDB主从测试

1. mongodb副本集测试

  1)主上建库,建集合

   use mydb

   db.createCollection('testA')

   show dbs

   show tables

 MongoDB主从+php实现

  2)从上查看,已成功同步库和集合

   rs.slaveOk()

   show dbs

 MongoDB主从+php实现

2. 副本集更改权重,模拟主宕机

  1)主上设置不同的权重,并查看

    cfg = rs.conf()

    cfg.members[0].priority = 3

    cfg.members[1].priority = 2

    cfg.members[2].priority = 1

    rs.reconfig(cfg)

    rs.config()

    查看生效的权重:

huangzhenping:PRIMARY> rs.config() {         "_id" : "huangzhenping",         "version" : 2,         "members" : [                 {                         "_id" : 0,                         "host" : "172.16.115.202:27017",                         "arbiterOnly" : false,                         "buildIndexes" : true,                         "hidden" : false,                         "priority" : 3,                         "tags" : {                         },                         "slaveDelay" : 0,                         "votes" : 1                 },                 {                         "_id" : 1,                         "host" : "172.16.115.157:27017",                         "arbiterOnly" : false,                         "buildIndexes" : true,                         "hidden" : false,                         "priority" : 2,                         "tags" : {                         },                         "slaveDelay" : 0,                         "votes" : 1                 },                 {                         "_id" : 2,                         "host" : "172.16.111.171:27017",                         "arbiterOnly" : false,                         "buildIndexes" : true,                         "hidden" : false,                         "priority" : 1,                         "tags" : {                         },                         "slaveDelay" : 0,                         "votes" : 1                 }         ],         "settings" : {                 "chainingAllowed" : true,                 "heartbeatTimeoutSecs" : 10,                 "getLastErrorModes" : {                 },                 "getLastErrorDefaults" : {                         "w" : 1,                         "wtimeout" : 0                 }         } }

2) 主上禁用27017端口,模拟主宕机

    iptables -I INPUT -p tcp --dport 27017 -j DROP

    原主上:已连接失败

  MongoDB主从+php实现

    原一台从机:状态已变为PRIMARY

  MongoDB主从+php实现

    rs.status()查看状态:

huangzhenping:PRIMARY> rs.status() {         "set" : "huangzhenping",         "date" : ISODate("2017-01-22T15:48:15.453Z"),         "myState" : 1,         "members" : [                 {                         "_id" : 0,                         "name" : "172.16.115.202:27017",                         "health" : 0,                         "state" : 8,                         "stateStr" : "(not reachable/healthy)",                         "uptime" : 0,                         "optime" : Timestamp(0, 0),                         "optimeDate" : ISODate("1970-01-01T00:00:00Z"),                         "lastHeartbeat" : ISODate("2017-01-22T15:48:14.551Z"),                         "lastHeartbeatRecv" : ISODate("2017-01-22T15:48:14.632Z"),                         "pingMs" : 0,                         "lastHeartbeatMessage" : "Failed attempt to connect to 172.16.115.202:27017; couldn't connect to server 172.16.115.202:27017 (172.16.115.202), connection attempt failed",                         "configVersion" : -1                 },                 {                         "_id" : 1,                         "name" : "172.16.115.157:27017",                         "health" : 1,                         "state" : 1,                         "stateStr" : "PRIMARY",                         "uptime" : 16333,                         "optime" : Timestamp(1484429320, 1),                         "optimeDate" : ISODate("2017-01-14T21:28:40Z"),                         "electionTime" : Timestamp(1485100022, 1),                         "electionDate" : ISODate("2017-01-22T15:47:02Z"),                         "configVersion" : 2,                         "self" : true                 },                 {                         "_id" : 2,                         "name" : "172.16.111.171:27017",                         "health" : 1,                         "state" : 2,                         "stateStr" : "SECONDARY",                         "uptime" : 5989,                         "optime" : Timestamp(1484429320, 1),                         "optimeDate" : ISODate("2017-01-14T21:28:40Z"),                         "lastHeartbeat" : ISODate("2017-01-22T15:48:14.751Z"),                         "lastHeartbeatRecv" : ISODate("2017-01-22T15:48:14.749Z"),                         "pingMs" : 0,                         "lastHeartbeatMessage" : "could not find member to sync from",                         "configVersion" : 2                 }         ],         "ok" : 1 }

3)新主上,创建一个库和集合

   use mydb2

   db.createCollection('testB')

 MongoDB主从+php实现

4)恢复旧主,则自动切换为MASTER状态,并同步自己离线时新主上创建的库

   iptables -D INPUT -p tcp --dport 27017 -j DROP

 MongoDB主从+php实现

三、MongoDB结合php

1. 下载安装mongo扩展模块

   wgethttps://github.com/mongodb/mongo-php-driver-legacy/archive/master.zip

   unzip mongo-php-driver-legacy-master.zip

   cd mongo-php-driver-legacy-master

   /usr/bin/phpize

   ./configure --with-php-config=/usr/bin/php-config

   make;make install

   MongoDB主从+php实现

2. 编辑php配置文件vim /etc/php.ini,加载扩展模块

 MongoDB主从+php实现

 MongoDB主从+php实现

3. 编辑测试文件 vim /usr/local/apache2/htdocs/1.php

  内容:

mydb; echo "Database mydb selected"; ?>

4. 测试 curl localhost/1.php

 MongoDB主从+php实现

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站标题:MongoDB主从+php实现-创新互联
网页链接:http://cxhlcq.cn/article/dsehco.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部