重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MongoDB主从+php实现

环境

创新互联公司是专业的湟中网站建设公司,湟中接单;提供成都做网站、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行湟中网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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实现


当前标题:MongoDB主从+php实现
转载来于:http://cqcxhl.cn/article/ggipjd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP