Mongodb3.0.5 Replica set setup and configuration of spring and java connected replicas are introduced in detail

  • 2020-06-03 08:41:15
  • OfStack

Mongodb3.0.5 Replica set setup and configuration of spring and java connected replicas are introduced in detail

1. Basic Environment:

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

2. Build mongodb copy set:

1. Install mongodb on 3 linux systems respectively (to avoid conflicts with the original mongodb port on the machine, set it as 57017) :

192.168.0.160
192.168.0.211 (Virtual machine on 192.168.0.33)
192.168.0.213 (Virtual machine on 192.168.0.4)

I won't go into the details of each mongodb installation, but refer to my installation documentation, and be careful not to change user authentication for a while. In addition, if there are no 3 machines, you can use only 1 machine to open 3 ports and prepare 3 data storage directories.

2. Start 3 mongodb in the form of copy set:

Just add replica set parameters - replSet - on a single mongodb boot basis, such as boot 160:


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

Where reptest is the specified replica set name, and the other two machines also have to do the same. Such as:


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

3. Configure replica set on any one machine. Here, configure replica set on 160:

(1) Enter mongo sehll(data operation interface) on 160:


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

(2) Switch to admin database:


use admin 

(3) Configure replica sets:


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) Load the replica set configuration file:


rs.initiate(config) 

(5) Check the status of replicas:


rs.status() 

If it is, do the following. If not, switch to PRIMARY and do the following (switch to another mongo).

(6) Increase users:


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

(7) Change the user authentication method:


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

(8) Delete user:


db.dropUser( " admin " ) 

(9) Re-establish the user (the user authentication method established in the system and above is not the same) :


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

(10) Close 3 mongodb:


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

(11) Establish keyFile file in the data directory of database 160:


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

(12) Set 600 permissions for keyFile file (600 permissions must be set) :


chmod 600 keyFile 

(13) Upload this keyFile file to the data directory of mongodb on the other two machines:


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

(14) Add keyFile to mongodb.ES126en, for example, 160:


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

(15) Restart mongodb and use replSet and auth parameters:


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

(16) Set the priority of replica set members in priority, and set the highest priority to 160. The default priority is 1:


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

This way, as long as mongodb 160 is on, the primary server will be 160

3. Configuration of connection replicas in 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> 

You only need to configure 1 ip to switch automatically. User authentication format: username: ES154en@dbname.

4. Code for connecting replica sets in java:


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

The user authentication format is: username, dbname, password

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: