This is a document written last year, the recent sudden discovery has not been issued, so now fill in, hoping to help some friends. Because there was no record at the time, it might seem monotonous here.
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, respectively, install MongoDB on three Linux system machines (to avoid conflicts with the original MongoDB port on the machine, this is 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)
Each MongoDB installation here is not to elaborate, you can refer to my installation documentation, note that the user does not change the authentication method first. 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. Start the three MongoDB as a replica set:
Just add the replica set parameter-replset on the basis of the standalone MongoDB boot, for example, start 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 are the same. Such as:
/mongodb3051/mongodb305/bin/mongod–f/mongodb3051/conf/mongodb.conf --replset RepTest
3, configure the replica set on any one machine, here on the 160 configuration:
(1), enter 160 on the MONGO SEHLL (Data operation interface):
/home/admin/mongodb3051/mongodb305/bin/mongo–port 57017
(2), switch to the admin database:
Use admin
(3), configure replica set:
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 replica set configuration file:
Rs.initiate (config)
(5), view replica set status:
Rs.status ()
Under normal circumstances you can see the will be the primary server, showing PRIMARY, if it is, do the following directly, and if not, switch to PRIMARY The following actions (change to another MONGO );
(6), increase the user:
Db.createuser ({"User": "admin", "pwd": "admin", "Roles": ["Root"]})
(7), change the user authentication method :
Varschema=db.system.version.findone ({"_id": "Authschema"}) Schema.currentversion=3db.system.version.save (Schema)
(8), delete the user:
Db.dropuser ("admin")
(9), re-establish the user (the system and the above established user authentication method is not the same ):
Db.createuser ({"User": "admin", "pwd": "admin", "Roles": ["Root"]})
(10), close three mongodb:
Db.shutdownserver () or KILL command
(11), set up the KeyFile file in the data directory of the 160 database:
Cd/home/admin/mongodb3051/dataopenssl Rand –base64 753 > keyfile
(12), set 600 permissions to the KeyFile file ( you must set the permissions ):
chmod keyfile
(13), upload this keyfile file to the other two computer MongoDB Data directory:
Scp–r keyfile [email protected]/mongodb3051/datascp–r keyfile [email protected]/mongodb3051/data
(14), add KeyFile in the mongodb.conf file, for example 160:
Keyfile=/home/admin/mongodb3051/data/keyfile
(15), restart MongoDB, use Replset and auth parameters:
/home/admin/mongodb3051/mongodb305/bin/mongod–f/home/admin/mongo3051/conf/mongodb.conf--replSet reptest -- Auth
(16), set the priority of the replica set members, set the highest priority for 160, the priority is 1 by default:
Config=rs.conf () config.members[0].priority=2 rs.reconfig (config)
In this case, as long as 160 of MongoDB is open, then the master server will be 160
Third, the configuration of the connection replica set 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.or G/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd "> <!--Factory bea n that creates the Mongoinstance--and <mongo:mongo-client replica-set= "192.168.0.160:57017" credentials= "admin:[e Mail protected] "id=" MONGO "> <mongo:client-options write-concern=" SAFE "connections-per-host=" 100 " 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>
You only need to configure one IP to automatically switch. User authentication format: Username:[email protected].
Iv. code for connecting replica sets in 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 ("Connect MongoDB Database Error", e); return MongoDB; }
The user authentication format is: Username,dbname,password
Mongodb3.0.5 replica set setup and spring and Java connection replica set configuration