Mongodb3.0.5 replica set setup and spring and Java connection replica set configuration details
First, the basic environment:
mongdb3.0.5 Database
Spring-data-mongodb-1.7.2.jar
Mongo-java-driver-3.0.2.jar
linux-redhat6.3
Tomcat7
Second, build MongoDB replica set:
1. Install MongoDB on three Linux systems respectively (to avoid conflicts with the original MongoDB port on the machine, set to 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)
The installation of each MongoDB here is not in detail, you can refer to my installation documents, note that you do not change the user authentication method. In addition, if there are no three machines, you can only use one machine to open three ports, while preparing three data storage directory.
2, in the form of a replica set to start three mongodb:
Simply add the replica set parameter-replset on the basis of a stand-alone MongoDB startup, for example, starting 160:
Where the reptest is the specified replica set name, and the other two machines are also similar to this one. Such as:
3. Configure the replica set on any computer, CONFIGURED here on 160:
(1), into the 160 on the MONGO SEHLL (data manipulation Interface):
(2), switch to admin database:
(3), configure the replica set:
(4), loading the replica set configuration file:
(5), view replica set status:
Normally you can see 160 will be the primary server, display primary, if it is, directly to the following operations, if not, switch to the primary on the following operations (to another MONGO);
(6), increase the user:
(7), change the user authentication method:
Varschema=db.system.version.findone ({"_id": "Authschema"})
schema.currentversion=3
(8), delete User:
(9), re-establish the user ( the system and the above established user authentication method is not the same ):
(10), close three mongodb:
(11), in 160 of the database data directory to establish keyfile files:
Cd/home/admin/mongodb3051/data
(12), set 600 permissions on the KeyFile file (600 permissions must be set):
(13), upload the KeyFile file to the other two computers on the MongoDB Data directory:
Scp–r keyfile Root@192.168.0.211/mongodb3051/data
(14) Add keyfile to the mongodb.conf file, such as 160:
(15), reboot MongoDB, use Replset and auth parameters:
(16), set the priority of the replica set member in priority, set the highest priority to 160, and the priority by default is 1:
Config=rs.conf ()
config.members[0].priority=2
In this case, as long as 160 of the MongoDB is open, then the primary server will be 160
Configuration of the connection replica set in spring:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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 C Reates the mongoinstance--> <mongo:mongo-client replica-set= "192.168.0.160:57017" admin: Admin@admin "id=" MONGO "> <mongo:client-options write-concern=" SAFE "connections-per-host=" threads-" Allowed-to-block-for-connection-multiplier= "/>" </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>
Only need to configure one IP, will automatically switch. User authentication format: username:password@dbname.
Four, Java in the connection to the replica set of code:
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 ("Connection MongoDB database Error", e);
}
return MongoDB;
}
The user authentication format is: Username,dbname,password
Thank you for reading, I hope to help you, thank you for your support for this site!