真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

MongoDB主從+php實(shí)現(xiàn)

環(huán)境

創(chuàng)新互聯(lián)公司是專業(yè)的湟中網(wǎng)站建設(shè)公司,湟中接單;提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行湟中網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

172.16.115.202   primary

172.16.111.171   secondary

172.16.115.157   secondary

一、MongoDB安裝

1. yum下載安裝mongodb

    擴(kuò)展源http://repo.mongodb.org/yum/redhat/mongodb-org-3.0.repo

     mongodb-org-3.0.7-1.el6.x86_64.rpm #元數(shù)據(jù)包,它可以實(shí)現(xiàn)自動(dòng)安裝下面的4個(gè)組件包

     mongodb-org-mongos-3.0.7-1.el6.x86_64.rpm # mongod守護(hù)程序,以及相關(guān)的配置和初始化腳本

     mongodb-org-server-3.0.7-1.el6.x86_64.rpm # mongos守護(hù)程序

     mongodb-org-shell-3.0.7-1.el6.x86_64.rpm # ongo shell環(huán)境

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

2. 編輯配置文件vim  /etc/mongod.conf

    1)去掉以下兩行以#開頭的注釋,以防重啟報(bào)錯(cuò)

         fork:true

         pidFilePath:/var/mongodb/mongod.pid

    2)bind_ip綁定多個(gè)IP,用逗號(hào)隔開,如監(jiān)聽全部,留空即可

3. 修改配置參數(shù),啟動(dòng)mongd

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

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

    編輯vim /etc/security/limits.conf,加入內(nèi)容:

    mongod soft nofile 64000

    mongod hard nofile 64000

    mongod soft nproc 32000

    mongod hard nproc 32000

    service mongd start

    注:啟動(dòng)過程較慢,這是它在寫數(shù)據(jù)/var/lib/mongo,如果啟動(dòng)不了,mongod -f /etc/mongod.conf方式,可以看到報(bào)錯(cuò)

4. 編輯三臺(tái)機(jī)器的MongoDB配置文件vim /etc/mongod.conf,重啟

    內(nèi)容:

    ##1)去掉replication的注釋

    ##2)加入下面參數(shù)(注意首行留空2個(gè)空格,固定格式):

    ## 類似于二進(jìn)制日志

       oplogSizeMB: 20

    ## 副本集名字

       replSetName: huangzhenping

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

 MongoDB主從+php實(shí)現(xiàn)

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

    use admin # 主上進(jìn)入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"}]}  #配置機(jī)器

    rs.initiate(config)  #刷新

    rs.status()   #查看狀態(tài)

 如下結(jié)果:出現(xiàn)三臺(tái)機(jī)器的信息,主的狀態(tài)已變?yōu)镻RIMARY

> 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)如執(zhí)行rs.status()命令,沒有發(fā)現(xiàn)三臺(tái)機(jī)器,需手動(dòng)執(zhí)行添加

            rs.add("172.16.115.157")

            rs.add("172.16.111.171")

       2)如從機(jī)的狀態(tài)為"stateStr":"STARTUP",則需執(zhí)行:

            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. 從上登錄,狀態(tài)已變?yōu)椋篠ECONDARY

 MongoDB主從+php實(shí)現(xiàn)



二、MongoDB主從測(cè)試

1. mongodb副本集測(cè)試

    1)主上建庫,建集合

     use mydb

     db.createCollection('testA')

     show dbs

     show tables

 MongoDB主從+php實(shí)現(xiàn)

    2)從上查看,已成功同步庫和集合

     rs.slaveOk()

     show dbs

 MongoDB主從+php實(shí)現(xiàn)

2. 副本集更改權(quán)重,模擬主宕機(jī)

   1)主上設(shè)置不同的權(quán)重,并查看

       cfg = rs.conf()

       cfg.members[0].priority = 3

       cfg.members[1].priority = 2

       cfg.members[2].priority = 1

       rs.reconfig(cfg)

       rs.config()

       查看生效的權(quán)重:

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端口,模擬主宕機(jī)

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

        原主上:已連接失敗

  MongoDB主從+php實(shí)現(xiàn)

       原一臺(tái)從機(jī):狀態(tài)已變?yōu)镻RIMARY

  MongoDB主從+php實(shí)現(xiàn)  

       rs.status()查看狀態(tài):

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)新主上,創(chuàng)建一個(gè)庫和集合

     use mydb2

     db.createCollection('testB')

 MongoDB主從+php實(shí)現(xiàn)

 

4)恢復(fù)舊主,則自動(dòng)切換為MASTER狀態(tài),并同步自己離線時(shí)新主上創(chuàng)建的庫

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

 MongoDB主從+php實(shí)現(xiàn)




三、MongoDB結(jié)合php

1. 下載安裝mongo擴(kuò)展模塊

     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實(shí)現(xiàn)

2. 編輯php配置文件vim /etc/php.ini,加載擴(kuò)展模塊

 MongoDB主從+php實(shí)現(xiàn) 

 MongoDB主從+php實(shí)現(xiàn)

3. 編輯測(cè)試文件 vim /usr/local/apache2/htdocs/1.php

    內(nèi)容:

mydb;
echo "Database mydb selected";
?>

4. 測(cè)試 curl localhost/1.php

 MongoDB主從+php實(shí)現(xiàn)


新聞名稱:MongoDB主從+php實(shí)現(xiàn)
URL網(wǎng)址:http://weahome.cn/article/ggipjd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部