Mongodb3.0.5 複本集搭建及spring和java串連複本集配置詳細介紹_MongoDB

來源:互聯網
上載者:User

Mongodb3.0.5 複本集搭建及spring和java串連複本集配置詳細介紹

一、基本環境:

mongdb3.0.5資料庫
spring-data-MongoDB-1.7.2.jar
mongo-Java-driver-3.0.2.jar
Linux-redhat6.3
tomcat7

二、搭建mongodb複本集:

1、  分別在三台linux系統機上安裝mongodb,(為避免和機器上原有的mongodb連接埠衝突,這裡設為57017):

192.168.0.160
192.168.0.211(192.168.0.33上的虛擬機器)
192.168.0.213(192.168.0.4上的虛擬機器)

每個mongodb的安裝這裡就不細說了,可以參考我的安裝方面的文檔,注意先不要更改使用者驗證方式。另外,這裡如果沒有三台機,也可以只用一台機開三個連接埠,同時準備三個資料存放區目錄。

2、  以複本集的方式啟動三個mongodb:

只是在單機mongodb啟動的基礎上加入複本集參數—replSet,例如啟動160的:

/home/admin/mongodb3051/mongodb305/bin/mongod –f /home/admin/mongo3051/conf/mongodb.conf --replSet reptest 

 其中,reptest是指定的複本集名稱,另外兩台機也也要和這個一樣。如:

/mongodb3051/mongodb305/bin/mongod –f /mongodb3051/conf/mongodb.conf --replSet repTest 

3、  在任意一台機上配置複本集,這裡在160上配置:

(1)、進入160上的mongo sehll(資料操作介面):

/home/admin/mongodb3051/mongodb305/bin/mongo –port 57017 

(2)、切換到admin資料庫:

use admin 

(3)、配置複本集:

config={_id:”reptest”,members:[{_id:0,host:”192.168.0.160:57017”},{_id:1,host:”192.168.0.211:57017”},{_id:,host:”192.168.0.213:57017”}]} 

(4)、載入複本集設定檔:

rs.initiate(config) 

(5)、查看複本集狀態:

rs.status() 

   正常情況下可以看到160會是主伺服器,顯示PRIMARY,如果是,就直接進行以下操作,如果不是,就切換到PRIMARY上進行以下操作(換到另一個mongo);

(6)、增加使用者:

db.createUser({“user”:”admin”,”pwd”:”admin”,”roles”:[“root”]}) 

(7)、更改使用者驗證方式: 

varschema=db.system.version.findOne({“_id”:”authSchema”}) schema.currentVersion=3 db.system.version.save(schema) 

(8)、刪除使用者:

db.dropUser(“admin”) 

(9)、重建立立使用者(系統中和上邊建立的使用者驗證方式不一樣):

db.createUser({“user”:”admin”,”pwd”:”admin”,”roles”:[“root”]}) 

(10)、關閉三個mongodb:

db.shutDownServer()或者kill命令 

(11)、在160的資料庫的data目錄中建立keyFile檔案:    

cd /home/admin/mongodb3051/data openssl rand –base64 753 > keyFile 

(12)、給keyFile檔案設定600許可權(必須設定600許可權):

chmod 600 keyFile 

(13)、把這個keyFile檔案上傳到另外兩台機上mongodb的data目錄中:    

scp –r keyFile root@192.168.0.211/mongodb3051/data scp –r keyFile root@192.168.0.213/mongodb3051/data 

(14)、在mongodb.conf檔案中加入keyFile,例如160:

keyFile=/home/admin/mongodb3051/data/keyFile 

(15)、重新啟動mongodb,使用replSet和auth參數:

/home/admin/mongodb3051/mongodb305/bin/mongod –f /home/admin/mongo3051/conf/mongodb.conf --replSet reptest --auth 

(16)、在priority中設定複本集成員的優先順序,給160設定最高優先順序,優先順序預設都是1:  

config=rs.conf() config.members[0].priority=2 rs.reconfig(config) 

這樣的話,只要160的mongodb是開著的,那麼主伺服器就會是160 

三、Spring中串連複本集的配置:

<?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"   xmlns:mongo="http://www.springframework.org/schema/data/mongo"   xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/data/mongo      http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">     <!-- Factory bean that creates the Mongoinstance -->   <mongo:mongo-client replica-set="192.168.0.160:57017" credentials="admin:admin@admin" id="mongo">      <mongo:client-options write-concern="SAFE" connections-per-host="100"       threads-allowed-to-block-for-connection-multiplier="50"        />    </mongo:mongo-client>      <mongo:db-factory id="mongoDbFactory"dbname="admin" mongo-ref="mongo"/>    <bean id="mongoTemplate"class="org.springframework.data.mongodb.core.MongoTemplate">      <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />    </bean>  </beans> 

只需要配置一個ip,就會自動切換。使用者驗證格式:username:password@dbname。 

四、java中串連複本集的代碼:

public DB getMongoDB() {    try {     ServerAddress sa = new ServerAddress("192.168.0.160", 57017);     ServerAddress sa1 = new ServerAddress("192.168.0.211", 57017);     ServerAddress sa2 = new ServerAddress("192.168.0.213", 57017);     List<ServerAddress> sends = new ArrayList<ServerAddress>();     sends.add(sa);     sends.add(sa1);     sends.add(sa2);     List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();     mongoCredentialList.add(MongoCredential.createMongoCRCredential("admin", "admin","admin".toCharArray()));     DB mongoDB = new MongoClient(sends,mongoCredentialList).getDB("admin");    } catch (Exception e) {     throw new RuntimeException("串連MongoDB資料庫錯誤", e);    }   return mongoDB;  } 

使用者驗證格式是:username,dbname,password

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

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