MongoDB主從複製

來源:互聯網
上載者:User

在生產環境中單台資料庫已不能滿足業務需求,資料庫主從複製在架構中使用很常見,用於備份、高可用、讀寫分離來提高資料處理效能和冗餘,常用架構模式是一主一從、一主多從、雙主。MongoDB資料庫特點我就不多說了,百度上說的更詳細。我們這次架構採用的是一主一從。

環境說明:

OS:CentOS6.5_x64

主Master):192.168.0.201

從Slave):192.168.0.202

主從資料庫目錄都在 /usr/local/mongodb/data

日誌目錄 /usr/local/mongodb/logs

1、主mongodb配置

[root@localhost ~]# mkdir /usr/local/mongodb/data[root@localhost ~]# mkdir /usr/local/mongodb/logs[root@localhost ~]# mkdir /usr/local/mongodb/conf

[root@localhost ~]# vi /usr/local/mongodb/conf/mongod.confport=27017       #連接埠號碼fork=true        #以守護進程方式運行logpath=/usr/local/mongodb/logs/mongodb.log  #記錄檔logappend=true   #日誌輸出方式dbpath=/usr/local/mongodb/data  #資料庫位置maxConns=1024    #資料庫最大串連數master=true      #主模式oplogSize=2048   #日誌滾動,單位M

2、從Mongodb配置

[root@localhost ~]# mkdir /usr/local/mongodb/data[root@localhost ~]# mkdir /usr/local/mongodb/logs[root@localhost ~]# mkdir /usr/local/mongodb/conf

[root@localhost ~]# vi /usr/local/mongodb/conf/mongod.confport=27017fork=truelogpath=/usr/local/mongodb/logs/mongodb.loglogappend=truedbpath=/usr/local/mongodb/datamaxConns=1024slave=true                  #從模式source=192.168.0.201:27017  #指定主Mongodbautoresync=true             #自動同步

3、添加環境變數並啟動Mongodb

[root@localhost ~]# echo "PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile[root@localhost ~]# source /etc/profile[root@localhost ~]# mongod -f /usr/local/mongodb/conf/mongod.conf #啟動[root@localhost ~]# netstat -tupln | grep mongodtcp     0  0 0.0.0.0:28017  0.0.0.0:*     LISTEN     1923/mongod tcp     0  0 0.0.0.0:27017  0.0.0.0:*     LISTEN     1923/mongod

#主上日誌資訊看出主要資料庫允許讓192.168.0.202從資料庫45458連接埠串連

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R044N94-0.jpg" title="QQ圖片20140105110532.jpg" alt="wKioL1LIzS7yUi5ZAADOoAuUd2s661.jpg" />

#從上日誌資訊看出開始同步192.168.0.201主要資料庫

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R0442Q3-1.jpg" title="QQ圖片20140105110336.jpg" alt="wKiom1LIzSfzEVdvAAC24wIiJrU519.jpg" />

4、測試Mongodb主從複製

#建立一個資料庫test,集合名也是test,插入一個欄位AGE:18。然後再從資料庫showdbs;查看已經同步過去。文法與Mysql有很大區別。

[root@localhost ~]# mongoMongoDB shell version:2.4.9-rc0> use test> db.test.save({AGE:18})> db.test.find(){ "_id" :ObjectId("52addd66124c02eb8b2d1a5a"), "AGE" : 18 }> show dbslocal   2.0771484375GBtest    0.203125GB>

主日誌

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R04425D-2.jpg" title="1.png" alt="wKiom1LIzdbh614lAADq-fpf3yk473.jpg" />

從日誌

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R0444056-3.jpg" title="2.png" alt="wKiom1LIzf_w7fOIAAClPWySRiM169.jpg" />

建立資料庫後,由上主從日誌看出,主允許從串連,從同步主要資料applied 1 operations)。主要資料庫操作記錄為OpLog日誌,說明主從資料庫原理是定期從主伺服器中擷取oplog記錄,然後在本機上執行。

5、查看主從複製狀態

#查看主

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R044LF-4.jpg" title="3.png" alt="wKioL1LIziuC2LXOAAGv_bRs8cA553.jpg" />

[root@localhost ~]# mongoMongoDB shell version:2.4.9-rc0connecting to: test>db.printReplicationInfo()configured oplog size:   2048MBlog length start to end:1494secs (0.42hrs)oplog first event time:  Sun Dec 15 2013 10:55:37 GMT-0500 (EST)oplog last event time:   Sun Dec 15 2013 11:20:31 GMT-0500 (EST)now:                     Sun Dec 15 2013 11:20:33GMT-0500 (EST)>

#查看從

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/140107/0R0445635-5.jpg" title="4.png" alt="wKioL1LIzjehSOsgAAFfgtITTFE089.jpg" />

[root@localhost ~]# mongoMongoDB shell version: 2.4.9-rc0connecting to: test>db.printReplicationInfo()this is a slave, printingslave replication info.source:   192.168.0.201:27017         syncedTo: Sun Dec 15 2013 11:18:31GMT-0500 (EST)                 = 21 secs ago (0.01hrs)>

#關閉Mongodb資料庫

[root@localhost ~]# mongod -f/usr/local/mongodb/conf/mongod.conf --shutdown

#寫一個簡單的SysV管理指令碼

[root@localhost ~]# vim /etc/init.d/mongod#!/bin/bash#chkconfig: 35 80 20#description: Mongodb service control scriptPROG="/usr/local/mongodb/bin/mongod"CONF="/usr/local/mongodb/conf/mongod.conf"case $1 in        start)        $PROG -f $CONF &> /dev/null        echo "Starting mongodb:                 [ OK ]"        ;;        stop)        $PROG -f $CONF --shutdown &> /dev/null        echo "Shutting down mongodb:                    [ OK ]"        ;;        restart)        $0 stop        $0 start        ;;        *)        echo "Usage: $0 {start|stop|restart}"esac

[root@localhost ~]# chmod +x /etc/init.d/mongod[root@localhost ~]# chkconfig --add mongod[root@localhost ~]# chkconfig mongod on

Mongdb主從複製就完事了,比Myslq簡單吧,比較靈活。


本文出自 ““企鵝”那點事兒” 部落格,請務必保留此出處http://going.blog.51cto.com/7876557/1348589

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.