中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB主從+php實現

發布時間:2020-05-30 11:02:13 來源:網絡 閱讀:1585 作者:huangzp168 欄目:MongoDB數據庫

環境

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擴展模塊

     wget https://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

    內容:

<?php
// 連接到mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// 選擇一個數據庫
$db = $m->mydb;
echo "Database mydb selected";
?>


4. 測試 curl localhost/1.php

 MongoDB主從+php實現





向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黄龙县| 怀来县| 仁寿县| 阿坝县| 侯马市| 句容市| 堆龙德庆县| 华池县| 玉山县| 安西县| 仙居县| 大埔县| 香河县| 本溪市| 濉溪县| 施秉县| 九寨沟县| 台北县| 三都| 积石山| 巴林左旗| 浙江省| 当涂县| 耒阳市| 繁昌县| 土默特右旗| 济南市| 石柱| 龙川县| 铅山县| 湟中县| 东莞市| 乌苏市| 项城市| 贵德县| 阿拉善右旗| 保定市| 都匀市| 河北区| 内乡县| 枝江市|