MongoDB的Master-Slave主從模式配置及主從複製要點解析_MongoDB

來源:互聯網
上載者:User

主從配置
mongodb的master-slave模式配置方式如下
1.keyFile
產生key_file

 openssl rand -base64 741 > mongo_key

將mongo_key 分別置於master 和 slave mongodb user可以access的地方。
設定許可權

chmod 700 mongo_key

設定

onwer chown mongodb:nogroup mongo_key

2.master配置
編輯/etc/mongodb.conf, 設定如下

master = truekeyFile = /path/to/mongo_key

3.slave 配置

slave = truesource = <ip of master>only = bookstorekeyFile = /usr/local/bookstore/mongo_key

如果只同步一個db,配置only項,如果多個DB需同步,注釋掉only項。
以上設定完畢後即可啟動master和slave了,如果配置有誤會啟動失敗,比如key_file許可權不正確,具體錯誤可以查看/var/log/mongodb/mongodb.log。如無誤,即可在master上修改一些記錄進行驗證了。
4.配置更改
master-slave配置比較簡單,但要修改已經運行正確的配置缺不是想象的那麼簡單。例如要更改master地址,或者去除only配置。因為mongodb啟動時,會將配置寫入DB中,master的配置在local DB的slaves表中, slave的配置在local DB的sources表中。如果單獨更改conf檔案,直接重啟會失敗,log中顯示和DB中的配置衝突。如果直接修改local DB,修改會很快被覆蓋。
5.操作辦法
取消slave配置重啟。即注釋掉slave = true這一行。
手動修改sources表,例如更改master 的ip或者去除only 項。
修改conf檔案,開啟slave = true,並修改其他項,例如更改master 的ip或者去除only 項。
再次重啟mongodb。
6.其他問題
手動強制同步:
如果slave因為特殊原因和master不同步,需要手動強制同步,方法為在slave上運行如下命令:

use admindb.runCommand( { resync: 1 } )

如果資料落後較多,則需要較長時間。

主從同步要點解析
主伺服器和從伺服器必須開啟安全認證:--auth, 主伺服器和從伺服器的admin資料庫中必須有全域使用者, 然後主伺服器的local資料庫和從伺服器的local資料均有名為repl且密碼相同的使用者名稱。
註:local:本機資料庫 這個資料庫不會同步,主要存放同步的資訊。在MongoDB2.0.2版本測試時,從伺服器的admin資料庫中沒有全域使用者時也能進行複製(Deven:我們就是採用這個方式,
從伺服器admin資料庫沒有建立使用者),儘管admin中無使用者,用戶端串連此伺服器不進行驗證(即--auth參數失效),但從伺服器的--auth必須指定。既然啟用了--auth,就應該在從伺服器的admin中增加一個使用者。

首先要在master的local資料庫中建立名為repl的賬戶,並設定密碼;
從伺服器上和master一樣 建立名為repl的賬戶,並設定和master上一樣的密碼;
然後建立一個全域賬戶(管理賬戶,開啟同步即可)
1.建立一個全域賬戶

> use adminswitched to db admin> db.addUser("root","zhuima")WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "root", "roles" : [ "root" ] }

2.分別在master/slave的local資料庫下建立一個名為repl的賬戶,並設定密碼相同

> use localswitched to db local> db.addUser("repl","zhuima")WARNING: The 'addUser' shell helper is DEPRECATED. Please use 'createUser' insteadSuccessfully added user: { "user" : "repl", "roles" : [ "dbOwner" ] }

3.master設定檔

[root@redis ~]# sed -e '/^$/d;/^#/d' /etc/mongodb.conf bind_ip = 192.168.58.30port = 27017fork = truepidfilepath = /var/run/mongodb/mongodb.pidlogpath = /var/log/mongodb/mongodb.logdbpath =/mydata/datajournal = trueauth = true# 開啟認證模式master = true          # 指定該mongodb為master模式

4.slave設定檔

[root@localhost ~]$ sed -e '/^$/d;/^#/d' /etc/mongodb.conf bind_ip = 192.168.58.10port = 27017fork = truepidfilepath = /var/run/mongodb/mongodb.pidlogpath = /var/log/mongodb/mongodb.logdbpath = /mydata/datajournal = trueauth = true  # 開啟認證模式slave = true  # 指定該mongodb為slave模式source = 192.168.58.30:27017  # 指定master伺服器

5.檢查同步情況

> db.printReplicationInfo()this is a slave, printing slave replication info.source:  192.168.58.30:27017 syncedTo: Fri Jul 18 2014 10:22:31 GMT+0200 (CEST) = 22 secs ago (0.01hrs)  # 這個是指延遲時間長度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.