【MongoDB學習筆記31】MongoDB配置複本集

來源:互聯網
上載者:User

標籤:mongodb   複本集   

一、配置環境

1.兩台安裝了mongodb的CentOS伺服器;(安裝參考http://281816327.blog.51cto.com/907015/1598270)

2.兩台伺服器的ip分別為192.168.1.112、192.168.1.113;

3.兩台伺服器防火牆放開mongodb的預設27017的連接埠;

 

二、設定管理員

兩台伺服器都做如下的配置

建立/data/db檔案夾

mkdir /date && mkdir /data/db

修改設定檔

vim /etc/mongodb.conf

其中的重要參數修改如下:

#where to log   logpath=/var/log/mongodb/mongod.loglogappend=true# fork and run in background   fork=truedbpath=/data/db# location of pidfile   pidfilepath=/var/run/mongodb/mongod.pid# Listen to local interface only. Comment out to listen on all interfaces.   #bind_ip=127.0.0.1# Enable the HTTP interface (Defaults to port 28017).   httpinterface=true# in replicated mongo databases, specify the replica set name here   replSet=spock

啟動MongoDB:

mongod –f /etc/mongodb.conf –-rest

 

三、配置複本集

在192.168.1.122上進入mongodb的shell,測試連接192.168.1.113並建立一個config文檔:

[[email protected] ~]# mongo   MongoDB shell version: 2.6.6    connecting to: test    > db=(new Mongo("192.168.1.113:27017")).getDB("test")    test    > config={"_id":"spock",    ... "members":[    ... {"_id":0,"host":"192.168.1.112:27017"},    ... {"_id":1,"host":"192.168.1.113:27017"}]}    {            "_id" : "spock",            "members" : [                    {                            "_id" : 0,                            "host" : "192.168.1.112:27017"                    },                    {                            "_id" : 1,                            "host" : "192.168.1.113:27017"                    }            ]    }

初始化複本集:  

> rs.initiate(config)    {            "info" : "Config now saved locally.  Should come online in about a minute.",            "ok" : 1    }

四、驗證結果

在192.168.1.112上進入MongoDB shell:

[[email protected] ~]# mongo   MongoDB shell version: 2.6.6    connecting to: test    spock:SECONDARY>

在192.168.1.112上進入MongoDB shell:

[[email protected] ~]# mongo   MongoDB shell version: 2.6.7    connecting to: test    spock:PRIMARY>

可以看得出spock:SECONDARY和spock:PRIMARY關鍵字,說明複本集配置完成;


本文出自 “緣隨心愿” 部落格,請務必保留此出處http://281816327.blog.51cto.com/907015/1610153

【MongoDB學習筆記31】MongoDB配置複本集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.