標籤:
mongoDB有一個主要特性就是複製,有多種複製形式,其中,主從複製是比較常用的一種。
主從複製的工作原理:首先要有兩個或更多的伺服器,其中一個是主節點,負責處理用戶端的請求,其他的都是從節點,負責映射主節點的資料。主節點記錄在其上執行的所有操作,從節點定期輪詢主節點獲得的這些操作,然後執行這些操作。由於從節點和主節點執行了相同的操作,從節點就能保持與主節點的資料同步。
實戰講解一下mongodb的搭建和主從複製,因為只有一台伺服器,所以只能通過修改連接埠號碼實現mongodb的主從,而不是通過多台伺服器實現。其實原理是一樣的~,只是這樣做實際意義不大~
之前已經裝了一個版本的mongodb(2.6.11),既然現在mongodb已經更新到了3.0.6,那麼從庫就特意使用3.0.6,反正是測試用途,就不管那麼多了~
安裝
mongodb
下載mongodb3.0.6執行檔案包,並解壓,把二進位執行檔案放在/usr/local/mongo3.0.6目錄
配置 主庫配置
# mongod.conf#where to loglogpath=/var/log/mongodb/mongod-27017.loglogappend=true# fork and run in backgroundfork=trueport=27017#dbpath=/var/lib/mongodbpath=/data/mongodb/mongo-27017# location of pidfilepidfilepath=/var/run/mongodb/mongod-27017.pidbind_ip=127.0.0.1,192.168.56.61#noauth=true# Ignore query hints#nohints=true# Disable the HTTP interface (Defaults to localhost:27017).nohttpinterface = falserest = truemaster = true
從庫配置
# mongod.conf#where to loglogpath=/var/log/mongodb/mongod-27018.loglogappend=true# fork and run in backgroundfork=trueport=27018#dbpath=/var/lib/mongodbpath=/data/mongodb/mongo-27018# location of pidfilepidfilepath=/var/run/mongodb/mongod-27018.pid# Listen to local interface only. Comment out to listen on all interfaces. bind_ip=127.0.0.1,192.168.56.61#noauth=true# Ignore query hints#nohints=true# Disable the HTTP interface (Defaults to localhost:27018).nohttpinterface = falserest = trueslave = truesource = 127.0.0.1:27017
啟動
# 主庫啟動/usr/local/mongo2.6.11/mongod -f /etc/mongod/mongod-27017.conf# 從庫啟動/usr/local/mongo3.0.6/mongod -f /etc/mongod/mongod-27018.conf
認證
不同版本的mongodb開始auth模式可以通過下面的方法來實現
1. 先把所有主從mongodb設為unauth模式
2. 在master添加一個帳號,這樣slave也就自動同步了該帳號
3. 把所有主從mongodb開啟auth模式
mongodb不同版本實現主從複製