Mongodb3.0.5 replica set setup and spring and Java connection replica set configuration __mongodb

Source: Internet
Author: User
Tags install mongodb mongodb

This is a document written last year, recently found that there is no hair, so now make up, I hope to be helpful to some friends. Because there was no screenshot 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. 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:

/home/admin/mongodb3051/mongodb305/bin/mongod–f  /home/admin/mongo3051/conf/mongodb.conf  --replSet Reptest

Where the reptest is the specified replica set name, and the other two machines are also similar to this one. Such as:

/mongodb3051/mongodb305/bin/mongod–f/mongodb3051/conf/mongodb.conf  --replset RepTest


3. Configure the replica set on any computer, CONFIGURED here on 160:

(1), into the 160 on the MONGO SEHLL (data manipulation Interface):

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

(2), switch to admin database:

Use admin


(3), configure the 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), loading the replica set configuration file:

Rs.initiate (config)


(5), view replica set status:

Rs.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:

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 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), in 160 of the database data directory to establish keyfile files:

Cd/home/admin/mongodb3051/data
OpenSSL  rand  –base64 753  >  keyfile


(12), set 600 permissions on the KeyFile file (600 permissions must be set):

chmod keyfile


(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
scp–r keyfile root@192.168.0.213/mongodb3051/data


(14) Add keyfile to the mongodb.conf file, such as 160:

Keyfile=/home/admin/mongodb3051/data/keyfile


(15), reboot 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 member in priority, set the highest priority to 160, and the priority by default is 1:

Config=rs.conf ()
 config.members[0].priority=2
 rs.reconfig (config)

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 "> <!--Factor Y bean that creates the mongoinstance--> <mongo:mongo-client replica-set= ' 192.168.0.160:57017 ' credentials= ' adm
            In:admin@admin "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>

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.