Setting up a MongoDB replica set on Centos7

Source: Internet
Author: User
Tags locale mkdir mongodb version

1. Installation Replica Set Introduction

A replica set (Replica set) is a cluster of MongoDB instances consisting of a primary (Primary) server and multiple backup (secondary) servers. With replication, updates to the data are pushed from primary to other instances, and after a certain delay, each MongoDB instance maintains the same copy of the DataSet. By maintaining redundant database replicas, you can achieve offsite backup of data, read-write separation, and automatic failover.

This means that if the primary server crashes, the backup server automatically upgrades one of the members to the new primary server. When you use the replication feature, you can still access data from other servers on the replica set if one server goes down. If the data on the server is corrupt or inaccessible, you can create a new copy of the data from a member of the replica set.

Early MongoDB version uses Master-slave, one master one from and MySQL similar, but slave in this architecture is read-only, when the main library down, from the library can not automatically switch to primary. The Master-slave mode has now been phased out, changed to a replica set, this mode has a master (primary), and multiple Slave (secondary), read-only. Support to set weights for them, when the main outage, the most weighted from the switch-based. In this architecture it is also possible to establish a quorum (arbiter) role, which is only responsible for adjudication, without storing data. The read-write data in this architecture is the Lord, and for the purpose of load balancing, it is necessary to manually specify the target server for reading the library.

Official Document Address: https://docs.mongodb.com/manual/replication/

Replica set schema diagram:

2.mongodb Replica Set Setup

Due to the limited machine in the local virtual machine, the pseudo-cluster simulation is built

First install boot three mongod process ports are: 27017,27108,27019

The steps are as follows:

2.1 Start MongoDB service in turn

① Decompression

Tar zxvf mongodb-linux-x86_64-rhel62-3.6.3.tgz

② Modify Name

MV mongodb-linux-x86_64-rhel62-3.6.3 MONGODB1

③ Creating the appropriate folder

CD MONGODB1

mkdir Data//directory

mkdir logs//log directory

mkdir conf//configuration file directory

CD conf

Touch mongo.conf//new configuration file

Port=27017dbpath=/home/lcc/mongodb1/datalogpath=/home/lcc/mongodb1/logs/mongod.logfork = True    //background start bind_ip= 0.0.0.0   replset = replset  //Replica set name

④ Start Mongod Service

CD MONGODB1

./bin/mongod-f conf/mongo.conf

At this point MONGODB1 started up, starting MONGODB2,MONGODB3 (note modify the exposed port)

2.2 Replica Set Configuration

After the server is started, go to the command line of any one node and associate three instances with each other.

To enter the MONGODB1 client:

CD MONGODB1

./bin/mongo

> config = {... _id: "Replset",... Members: [... {_id:0, Host: ' 192.168.73.29:27017 '},... {_id:1, Host: ' 192.168.73.29:27018 '},... {_id:2, Host: ' 192.168.73.29:27019 '}]} {        "_id": "Replset",        "members": [                {                        "_id": 0,                        "host": "192.168.73.29:27017"                },                {                        "_ ID ": 1,                        " host ":" 192.168.73.29:27018 "                },                {                        " _id ": 2,                        " host ":" 192.168.73.29:27019 "                }< c15/>]}

Initializing the configuration of a replica set

Rs.initiate (config) {        "OK": 1,        "Operationtime": Timestamp (1520260635, 1),        "$clusterTime": {                " Clustertime ": Timestamp (1520260635, 1),                " signature ": {                        " hash ": Bindata (0," aaaaaaaaaaaaaaaaaaaaaaaaaaa= "),                        "KeyId": Numberlong (0)                }        

When the configuration information is initialized, you can clearly see that the command line of MongoDB has changed, showing the replica set name and node type that the current node belongs to.

Go in again. MONGODB1 Client:

发现mongodb1作为了主节点
至此,mongodb的副本集配置已经完成了,接下来是测试副本集是否可用
3. Test 3.1 view replica set status
Replset:secondary> Rs.config () {"_id": "Replset", "Version": 1, "ProtocolVersion": Numberlong (1), "Membe            RS ": [{" _id ": 0," host ":" 192.168.73.129:27017 "," arbiteronly ": false,                            "Buildindexes": True, "hidden": false, "priority": 1, "tags": {            }, "Slavedelay": Numberlong (0), "votes": 1}, {"_id": 1, "Host": "192.168.73.129:27018", "arbiteronly": false, "buildindexes": true, " Hidden ": false," priority ": 1," tags ": {}," Slavedelay ":             Numberlong (0), "votes": 1}, {"_id": 2, "host": "192.168.73.129:27019", "Arbiteronly": false, "buildindexes": True, "hidden": false, "priority": 1, "tags": {                            }, "Slavedelay": Numberlong (0), "votes": 1}], "Settings        ": {" chainingallowed ": True," Heartbeatintervalmillis ": $," heartbeattimeoutsecs ": 10,        "Electiontimeoutmillis": 10000, "Catchuptimeoutmillis":-1, "Catchuptakeoverdelaymillis": 30000, "Getlasterrormodes": {}, "Getlasterrordefaults": {"W": 1, "Wtimeout"  : 0}, "Replicasetid": ObjectId ("5b3c7d50a5952cfcd32c859b")}}replset:primary>
3.2 Viewing master node information
Replset:primary> Rs.ismaster () {"Hosts": ["192.168.73.129:27017", "192.168.73.129:27018", "19    2.168.73.129:27019 "]," setName ":" Replset "," setversion ": 1," IsMaster ": True," secondary ": false, "PRIMARY": "192.168.73.129:27017", "Me": "192.168.73.129:27017", "Electionid": ObjectId ("7fffffff0000000000000001        ")," Lastwrite ": {" OpTime ": {" ts ": Timestamp (1530690959, 1)," T ": Numberlong (1) }, "Lastwritedate": Isodate ("2018-07-04t07:55:59z"), "Majorityoptime": {"ts": Timestamp (15    30690959, 1), "T": Numberlong (1)}, "Majoritywritedate": Isodate ("2018-07-04t07:55:59z")}, "Maxbsonobjectsize": 16777216, "maxmessagesizebytes": 48000000, "maxwritebatchsize": 100000, "LocalTime": Isodate ("2018-07-04t07:56:08.530z"), "logicalsessiontimeoutminutes": +, "minwireversion": 0, "Maxwireversion" : 6, "readOnly": FALSE, "OK": 1, "Operationtime": Timestamp (1530690959, 1), "$clusterTime": {"Clustertime": Timestamp (153 0690959, 1), "signature": {"hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "keyId": Nu  Mberlong (0)}}}replset:primary>
3.3 Data Testing

Insert 100 Piece of data

Replset:primary> show Dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBtest    0.000gbreplset:primary > Use testswitched to DB Testreplset:primary> for (var i = 0; i < i++) {... db.test.insert ({order:i, Name: " Test "+ i}"}writeresult ({"ninserted": 1}) replset:primary> Db.test.count () 100replset:primary>

Go to the replica node to see if the data is synchronized

[[email protected] mongodb3]$./bin/mongo 192.168.73.129:27018mongodb Shell version v3.6.3connecting To:mongodb ://192.168.73.129:27018/testmongodb server Version:3.6.3server has startup warnings:2018-07-04t00:46:51.797-0700 I  control [Initandlisten] 2018-07-04t00:46:51.797-0700 I control [Initandlisten] * * warning:access CONTROL is not enabled For the database.2018-07-04t00:46:51.797-0700 I CONTROL [initandlisten] * * Read and Write access to data and Co Nfiguration is unrestricted.2018-07-04t00:46:51.797-0700 i CONTROL [Initandlisten] 2018-07-04t00:46:51.797-0700 I control [Initandlisten] 2018-07-04t00:46:51.797-0700 I CONTROL [initandlisten] * * WARNING:/sys/kernel/mm/transparent_ Hugepage/enabled is ' always '. 2018-07-04t00:46:51.797-0700 I CONTROL [initandlisten] * * We suggest setting it to ' n Ever ' 2018-07-04t00:46:51.797-0700 I control [Initandlisten] 2018-07-04t00:46:51.797-0700 i control [initandlisten] * * W Arning:/sys/kernel/mm/transparent_hugepage/Defrag is ' always '. 2018-07-04t00:46:51.797-0700 I CONTROL [initandlisten] * * We suggest setting it to ' Never ' 2018- 07-04t00:46:51.797-0700 I CONTROL [Initandlisten] replset:secondary> show dbs2018-07-04t01:23:47.373-0700 E QUERY [ THREAD1] error:listdatabases failed:{"operationtime": Timestamp (1530692619, 1), "OK": 0, "errmsg": "Not mast  ER and Slaveok=false "," code ": 13435," codename ":" Notmasternoslaveok "," $clusterTime ": {" Clustertime "            : Timestamp (1530692619, 1), "signature": {"hash": Bindata (0, "aaaaaaaaaaaaaaaaaaaaaaaaaaa="), "KeyId": Numberlong (0)}}: [email protected]/mongo/shell/utils.js:25:13[email protected]/mongo/ shell/mongo.js:65:1[email protected]/mongo/shell/utils.js:816:19[email protected]/mongo/shell/  utils.js:706:15@ (SHELLHELP2):1:1replset:secondary>

When we want to view the data from the node, we find an error, because the slave node is denied read by default, so we need to turn on the Read function

Replset:secondary> Rs.slaveok ()

Then look at the data and find that it's synchronized.

Replset:secondary> use testswitched to DB testreplset:secondary> db.test.count () 100

Setting up a MongoDB replica set on Centos7

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.