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

1. Environmental preparation 


1 , Centos7 
2 , mongodb3.4.9 3 , the IPs
of the three machines are: 10.170.1.16, 10.170.1.18, 10.170.1.33

 

2. 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 the three machines respectively

2. Use the tar command to decompress the installation package and then modify the decompressed 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 and create three new directories: conf, logs, and db 


conf stores the configuration file directory, logs is used to store the log directory, and db is used to store the data directory

cd mongodb

mkdir conf logs db

 

4.  Enter the conf directory and create a new mongodb configuration file mongodb.conf

cd conf

touch mongodb.conf

 

5.  Write the configuration file mongodb.conf, the content is as follows 


Where dbpath is the database file directory, logpath is the log directory, port is the port occupied by mongodb , and when fork is true , it means that it will be 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 where –replSet represents the replica cluster parameter, mongoTest is the name of the replica set, the name here can be chosen arbitrarily, and the other two machines should be the same as this

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf --replSet mongoTest

 

 

 

7.  Configure the replica set

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'}]}

Initialize the replica set

rs.initiate(config_test);

View replica status

rs.status();

 

 

health indicates whether the node in the replica set is normal, 0 indicates abnormal, 1 indicates normal; state indicates the identity of the node, 0 indicates non-primary node, 1 indicates primary node; stateStr is used to characterize the node identity, PRIMARY indicates the primary node, SECONDARY represents the secondary node; name is the ip and port information of the replica set node

 

View replica synchronization status

db.printSlaveReplicationInfo();

 

 

 

8. Main library configuration user

Go to the mongodb main library operation page and add users before adding permissions

db.createUser({user:'admin',pwd:'admin',roles:[{role:'userAdminAnyDatabase

',db:'admin'}]})

 The userAdminAnyDatabase  role has just been created to manage users, and users can be created and deleted 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  permission of the key must be 600

 

 

Note: Put the key on each machine in the cluster, remember that it must be consistent, and the permission is set to 600 ;

 

Modify mongodb.conf configuration

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

 

 

 

Re-enable all servers

 

Note: Remember when restarting, if the keyfile specification is not configured in the configuration file, you must use the parameter keyfile specification at startup . Pay attention to the shutdown sequence. The mongodb cluster has the function of automatically switching the main database. If the main database is closed first, the main database Just switch to the other ones, here to prevent the main library from changing, and then close the main library after the slave library is closed.

 

When keyfile authentication is turned on, auth authentication is turned on by default , and you can log in with the previously created account.

 

 

 

 

 

 

10. Finally check the status of the master and slave

rs.status();

 

 

 

11. Configure read-write separation of replica sets

Configuring read-write separation for a replica set requires the following two steps:

(1) Set setSlaveOk on the replica node (in order to make the slave machine readable) ;

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

Add rs.slaveOk(); in .mongorc.js ;

Note: Generally, this file is empty, add it directly. Save and exit. After that, you can log out of mongo and go in.

Try to add it to every server.

(2) At the code level, set the read data from the replica node during the read operation, as shown below:

 

ReadPreference preference = ReadPreference.secondary();

mongoTemplate.setReadPreference(preference);

 

After the configuration is completed, the test read operation will be automatically assigned to the slave server, and the write operation will be performed on the master server

 This part can view the log from the console for analysis

Attached unit test program:

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("++++++++++++++++++++++++++ end of reading ");

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

BasicReport report = new BasicReport();

report.setSino_cardid("3333");

reportDao.insertOneReport(report);

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

}

}

 

These can be viewed from the console log, for example: a read operation was just performed,

 

 

16 is the slave

A write operation is performed below, and the output is as follows

 

 

18 is the host

You can view the master-slave status

After testing, the delete operation is also performed on the host, so it is not a read operation

 

Tested: the upsert method in mongodb is a write operation

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324511812&siteId=291194637