Mongodb3.0.5 副本集搭建及spring和java連接副本集配置詳細介紹
Mongodb3.0.5 副本集搭建及spring和java連接副本集配置詳細介紹
一、基本環(huán)境:
mongdb3.0.5數(shù)據(jù)庫
spring-data-MongoDB-1.7.2.jar
mongo-Java-driver-3.0.2.jar
Linux-redhat6.3
tomcat7
二、搭建mongodb副本集:
1、 分別在三臺linux系統(tǒng)機上安裝mongodb,(為避免和機器上原有的mongodb端口沖突,這里設(shè)為57017):
192.168.0.160
192.168.0.211(192.168.0.33上的虛擬機)
192.168.0.213(192.168.0.4上的虛擬機)
每個mongodb的安裝這里就不細說了,可以參考我的安裝方面的文檔,注意先不要更改用戶驗證方式。另外,這里如果沒有三臺機,也可以只用一臺機開三個端口,同時準備三個數(shù)據(jù)存儲目錄。
2、 以副本集的方式啟動三個mongodb:
只是在單機mongodb啟動的基礎(chǔ)上加入副本集參數(shù)—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(數(shù)據(jù)操作界面):
/home/admin/mongodb3051/mongodb305/bin/mongo –port 57017
(2)、切換到admin數(shù)據(jù)庫:
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)、查看副本集狀態(tài):
rs.status()
正常情況下可以看到160會是主服務(wù)器,顯示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)、重新建立用戶(系統(tǒng)中和上邊建立的用戶驗證方式不一樣):
db.createUser({“user”:”admin”,”pwd”:”admin”,”roles”:[“root”]})
(10)、關(guān)閉三個mongodb:
db.shutDownServer()或者kill命令
(11)、在160的數(shù)據(jù)庫的data目錄中建立keyFile文件:
cd /home/admin/mongodb3051/data openssl rand –base64 753 > keyFile
(12)、給keyFile文件設(shè)置600權(quán)限(必須設(shè)置600權(quán)限):
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參數(shù):
/home/admin/mongodb3051/mongodb305/bin/mongod –f /home/admin/mongo3051/conf/mongodb.conf --replSet reptest --auth
(16)、在priority中設(shè)置副本集成員的優(yōu)先級,給160設(shè)置最高優(yōu)先級,優(yōu)先級默認都是1:
config=rs.conf() config.members[0].priority=2 rs.reconfig(config)
這樣的話,只要160的mongodb是開著的,那么主服務(wù)器就會是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數(shù)據(jù)庫錯誤", e); } return mongoDB; }
用戶驗證格式是:username,dbname,password
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
MongoDB快速入門筆記(一)之windows下安裝MongoDB方法
MongoDB 是一個基于分布式文件存儲的數(shù)據(jù)庫。由 C++ 語言編寫。本文重點給大家介紹MongoDB快速入門筆記(一)之windows下安裝MongoDB方法,非常不錯具有參考借鑒價值,感興趣的朋友一起看下吧2016-06-06Ubuntu系統(tǒng)中安裝MongoDB及其啟動命令mongod的教程
這篇文章主要介紹了Ubuntu系統(tǒng)中安裝MongoDB及其啟動命令mongod的教程,包括設(shè)置MongoDB開機啟動的腳本示例,非常推薦,需要的朋友可以參考下2016-01-01對標mongodb存儲類JSON數(shù)據(jù)文檔統(tǒng)計分析詳解
這篇文章主要介紹了對標mongodb存儲類JSON數(shù)據(jù)文檔統(tǒng)計分析,只是介紹了簡單的查詢,其實針對各種統(tǒng)計分析場景,clickhouse提供了超級多的統(tǒng)計分析函數(shù)、窗口函數(shù)等等,當然針對數(shù)組的數(shù)據(jù)類型也有很多的統(tǒng)計分析函數(shù),需要的朋友可以參考下2022-06-06MongoDB 3.6版本中bind_ip設(shè)置詳解
這篇文章主要給大家介紹了關(guān)于MongoDB 3.6版本中bind_ip設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12Spring Boot中使用MongoDB數(shù)據(jù)庫的方法
MongoDB是一個高性能,開源,無模式的,基于分布式文件存儲的文檔型數(shù)據(jù)庫,由C++語言編寫,其名稱來源取自“humongous”,是一種開源的文檔數(shù)據(jù)庫──NoSql數(shù)據(jù)庫的一種。這篇文章主要介紹了Spring Boot中使用MongoDB數(shù)據(jù)庫的方法,需要的朋友可以參考下2017-12-12Mongodb在UPDATE操作中使用$push向數(shù)組中插入數(shù)據(jù)的方法
在update操作中,使用$push操作符向數(shù)組中插入新的元素,按照相應的語法,使用$push操作符,下面通過本文給大家分享Mongodb在UPDATE操作中使用$push向數(shù)組中插入數(shù)據(jù)的方法,感興趣的朋友一起看看吧2024-06-06