MongoDB builds and configures master-slave and read-write separation

Source: Internet
Author: User
Tags mkdir

1. Environment Preparation


1,Centos7
2,mongodb3.4. 9
3. Three machine IPs are:10.170.1.16, 10.170.1.18, 10.170.1.33

2, the installation of MONGDB database

The following operations are performed on three machines, respectively

1.First download the mongodb installation package mongodb-linux-x86_64-amazon-3.4.9.tgz on three machines respectively.

2 . Unzip the installation package with the TAR command and then modify the extracted directory name

Tar zxvf mongodb-linux-x86_64-amazon-3.4.9.tgz

MV Mongodb-linux-x86_64-amazon-3.4.9.tgz MongoDB

3. Enter The MongoDB directory to create a new three directory conf, logs, DB


Conf Storage profile directory,logs used to store the log directory,db is used to store the data directory

CD MongoDB

mkdir conf Logs db

4. Enter the Conf directory to create a new MongoDB configuration file mongodb.conf

CD conf

Touch mongodb.conf

5. Write the configuration file mongodb.conf, which reads as follows


where dbpath is the database file directory,logpath is the log directory,port is MongoDB The ports that are occupied, Fork is a true the time of the show is started in the background

dbpath=/data/mongodb/db

Logpath=/data/mongodb/log/mongodb.log

port=27017

Fork=true

6. start MongoDB on three machines respectively . The –replset represents the replica cluster parameter, Mongotest is the replica set name, here the name can be arbitrarily taken, the other two machines and this same

/usr/local/mongodb/bin/mongod-f/usr/local/mongodb/conf/mongodb.conf--replset MongoTest

7. Configuring Replica Sets

Enter MongoDB:

MONGO 10.170.1.16:27017/admin-u Test-p

Enter password

config_test={_id: ' Mongotest ', Members: [{_id:0, Host: ' 10.170.1.16:27017 '},{_id:1, Host: ' 10.170.1.18:27017 '},{_id : 2, Host: ' 10.170.1.33:27017 '}]}

Initializing a replica set

Rs.initiate (config_test);

View replica Status

Rs.status ();

health Indicates whether the node is normal in the replica set,0 is unhealthy,1 is normal , State represents the identity of the node, 0 represents a non-primary node, 1 represents the primary node; Statestr used to describe the character of a node, PRIMARY represents the primary node, Secondary represents a secondary node; name is a replica set node. IP and port Information

View Replica synchronization Status

Db.printslavereplicationinfo ();

8, the main library configuration users

Go to the MongoDB Main Library Operations page and add users before adding permissions

Db.createuser ({User: ' admin ', pwd: ' admin ', Roles:[{role: ' Useradminanydatabase

', DB: ' Admin '}]})

The useradminanydatabase role has just been set up to manage users who can create and delete users through this role.

9, increase the security authentication mechanism KeyFile

Mkdir-p/data/mongo_set/usercenter/27017/

Touch Mongodb-keyfile

# Generate key

OpenSSL rand-base64 745 >/data/mongo_set/usercenter/27017/mongodb-keyfile

chmod 600/data/mongo_set/usercenter/27017/mongodb-keyfile

# The key must have a permission of

Note: Place the key on each of the machines in the cluster, remembering that it must be consistent and that the permissions are set to;

Modify mongodb.conf Configuration

Keyfile=/data/mongo_set/usercenter/27017/mongodb-keyfile

Re-enable all servers

Note: Remember that when restarting,keyfile specifies that if it is not configured in the configuration file, it must be started with the parameter keyfile specified, and the close order is noted,MongoDB the cluster has automatic switch to the main library function, if the main library, the main library switch to the other top, here to prevent changes in the main library, from the library closed and then close the main library

turn on keyfile certification on the default auth certification, you can use the previously created account to login

10, finally check the status of Master and slave

Rs.status ();

11. Configure read and write separation of replica sets

Configuring read and write separations for a replica set requires the following two steps:

(1) set the Setslaveok on the replica node (for the slave to become readable);

found by command sudo find/-name. mongorc.js. mongorc.js

add rs.slaveok () in . mongorc.js;

Note: generally this file is empty, directly add to. Save exit. Then exit the MONGO and get in there.

Try to add each server to your computer.

(2) at the code level, set the read data from the replica node during the read operation as follows:

Readpreference preference = Readpreference.secondary ();

Mongotemplate.setreadpreference (preference);

After the configuration is finished, the test read operation is automatically assigned to the slave server, and the write operation is performed on the master server.

This section allows you to view logs from the console for analysis

Attached unit test procedure:

Package com.sinoway.cisp;

Import Org.junit.Test;

Import Org.junit.runner.RunWith;

Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.boot.test.context.SpringBootTest;

Import Org.springframework.test.context.junit4.SpringRunner;

Import Com.sinoway.cisp.constant.CollectionEnum;

Import Com.sinoway.cisp.mongo.collection.BasicReport;

Import Com.sinoway.cisp.mongo.dao.SinowayReportDao;

@RunWith (Springrunner.class)

@SpringBootTest (webenvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class XMLTest {

@Autowired

Private Sinowayreportdao Reportdao;

@Test

public void Testsql () {

System.out.println ("++++++++++++++++++++++++ start reading ");

Basicreport Historyreport = Reportdao.findonebysinocardid ("sw-8x760156-20161122",

Basicreport.class, CollectionEnum.HISTORYREPORT_COLLECTION_NAME.getCollectionName ());

System.out.println (Historyreport);

System.out.println ("++++++++++++++++++++++++ reading end ");

System.out.println ("++++++++++++++++++++++++ begins to write ");

Basicreport report = new Basicreport ();

Report.setsino_cardid ("3333");

Reportdao.insertonereport (report);

System.out.println ("++++++++++++++++++++++++ write End ");

}

}

These can be viewed from the log in the console, for example: Just read the operation,

from the Machine

Here is a write operation, the output is as follows

is the host

can view master-slave status

After testing, the delete operation is also carried out on the host, so it is not part of the read operation

Tested: MongoDB Upsert method belongs to write operation

MongoDB builds and configures master-slave and read-write separation

Related Article

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.