Mongodb3.0.5 副本集搭建及spring和java連接副本集配置詳細(xì)介紹
一、基本環(huán)境:
mongdb3.0.5數(shù)據(jù)庫(kù)
spring-data-MongoDB-1.7.2.jar
mongo-Java-driver-3.0.2.jar
Linux-redhat6.3
tomcat7
二、搭建mongodb副本集:
1、 分別在三臺(tái)linux系統(tǒng)機(jī)上安裝mongodb,(為避免和機(jī)器上原有的mongodb端口沖突,這里設(shè)為57017):
192.168.0.160
192.168.0.211(192.168.0.33上的虛擬機(jī))
192.168.0.213(192.168.0.4上的虛擬機(jī))
每個(gè)mongodb的安裝這里就不細(xì)說(shuō)了,可以參考我的安裝方面的文檔,注意先不要更改用戶驗(yàn)證方式。另外,這里如果沒(méi)有三臺(tái)機(jī),也可以只用一臺(tái)機(jī)開(kāi)三個(gè)端口,同時(shí)準(zhǔn)備三個(gè)數(shù)據(jù)存儲(chǔ)目錄。
2、 以副本集的方式啟動(dòng)三個(gè)mongodb:
只是在單機(jī)mongodb啟動(dòng)的基礎(chǔ)上加入副本集參數(shù)—replSet,例如啟動(dòng)160的:
1
|
/home/admin/mongodb3051/mongodb305/bin/mongod –f /home/admin/mongo3051/conf/mongodb .conf --replSet reptest |
其中,reptest是指定的副本集名稱,另外兩臺(tái)機(jī)也也要和這個(gè)一樣。如:
1
|
/mongodb3051/mongodb305/bin/mongod –f /mongodb3051/conf/mongodb .conf --replSet repTest |
3、 在任意一臺(tái)機(jī)上配置副本集,這里在160上配置:
(1)、進(jìn)入160上的mongo sehll(數(shù)據(jù)操作界面):
1
|
/home/admin/mongodb3051/mongodb305/bin/mongo –port 57017 |
(2)、切換到admin數(shù)據(jù)庫(kù):
1
|
use admin |
(3)、配置副本集:
1
|
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)、加載副本集配置文件:
1
|
rs.initiate(config) |
(5)、查看副本集狀態(tài):
1
|
rs.status() |
正常情況下可以看到160會(huì)是主服務(wù)器,顯示PRIMARY,如果是,就直接進(jìn)行以下操作,如果不是,就切換到PRIMARY上進(jìn)行以下操作(換到另一個(gè)mongo);
(6)、增加用戶:
1
|
db.createUser({“user”:”admin”,” pwd ”:”admin”,”roles”:[“root”]}) |
(7)、更改用戶驗(yàn)證方式:
1
2
3
|
varschema=db.system.version.findOne({“_id”:”authSchema”}) schema.currentVersion=3 db.system.version.save(schema) |
(8)、刪除用戶:
1
|
db.dropUser(“admin”) |
(9)、重新建立用戶(系統(tǒng)中和上邊建立的用戶驗(yàn)證方式不一樣):
1
|
db.createUser({“user”:”admin”,” pwd ”:”admin”,”roles”:[“root”]}) |
(10)、關(guān)閉三個(gè)mongodb:
1
|
db.shutDownServer()或者 kill 命令 |
(11)、在160的數(shù)據(jù)庫(kù)的data目錄中建立keyFile文件:
1
2
|
cd /home/admin/mongodb3051/data openssl rand –base64 753 > keyFile |
(12)、給keyFile文件設(shè)置600權(quán)限(必須設(shè)置600權(quán)限):
1
|
chmod 600 keyFile |
(13)、把這個(gè)keyFile文件上傳到另外兩臺(tái)機(jī)上mongodb的data目錄中:
1
2
|
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:
1
|
keyFile= /home/admin/mongodb3051/data/keyFile |
(15)、重新啟動(dòng)mongodb,使用replSet和auth參數(shù):
1
|
/home/admin/mongodb3051/mongodb305/bin/mongod –f /home/admin/mongo3051/conf/mongodb .conf --replSet reptest --auth |
(16)、在priority中設(shè)置副本集成員的優(yōu)先級(jí),給160設(shè)置最高優(yōu)先級(jí),優(yōu)先級(jí)默認(rèn)都是1:
1
2
3
|
config=rs.conf() config.members[0].priority=2 rs.reconfig(config) |
這樣的話,只要160的mongodb是開(kāi)著的,那么主服務(wù)器就會(huì)是160
三、Spring中連接副本集的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans <!-- 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 > |
只需要配置一個(gè)ip,就會(huì)自動(dòng)切換。用戶驗(yàn)證格式:username:password@dbname。
四、java中連接副本集的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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ù)庫(kù)錯(cuò)誤" , e); } return mongoDB; } |
用戶驗(yàn)證格式是:username,dbname,password
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!