Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB installation and replica set configuration

Shulou Source: shulou.com Published: 2022-06-01 14:38:11 09月25日 Update

A replica set is a group of N mongo nodes that work together to provide automatic failure cluster transfer. Establishing a replica set requires steps: 1, you need to start each node, 2, and then initialize it. Here, we will configure a set of three nodes, which is the standard setting.

Once the mongod node starts, we will issue a command to initialize the settings correctly. In a few seconds, a master node will be elected, and you can start writing and querying sets.

Environment description:

A.192.168.30.112purl 3306 master node

B.192.168.30.113purl 3306 slave node

C.192.168.30.114purl 3306 slave node

1. Decompress and install mongodb

[root@localhost ~] # tar zxvf mongodb-linux-x86_64-2.0.4.tgz

[root@localhost] # mv mongodb-linux-x86_64-2.0.4 / usr/local/mongodb

[root@localhost ~] # cd / usr/local/mongodb/

[root@localhost mongodb] # mkdir-p mongodb_data/mongodb_db

[root@localhost mongodb] # mkdir mongodb_data/mongodb_logs

For the above three servers, the same installation steps

two。 Set up the startup configuration file

[root@localhost mongodb] # vi / etc/mongodb.conf

Fork = true

Quiet = true

Bind_ip = 192.168.30.112

Port = 3306

Dbpath = / usr/local/mongodb/mongodb_data/mongodb_db

Logpath = / usr/local/mongodb/mongodb_data/mongodb_logs/server1.log

Logappend = true

Journal = true

Rest = true

ReplSet = test

For the remaining 2 servers, modify the corresponding IP.

3. Start mongodb

[root@localhost] # / usr/local/mongodb/bin/mongod-f / etc/mongodb.conf

4. Initialize node

We connect to one of the nodes and initialize it with the replSetInitiate command. This command requires a configuration object that specifies the name of each member set.

> / usr/local/mongodb/bin/mongo 192.168.30.112:3306/admin

> config = {_ id: "test", members: [

... {_ id:0,host:'192.168.30.112:3306',priority:4}

... {_ id:1,host:'192.168.30.113:3306',priority:2}

... {_ id:2,host:'192.168.30.114:3306',priority:1}]

...}

> rs.initiate (config)

{

Info: "Config now saved locally. Should come online in about a minute."

"ok": 1

}

PRIMARY > rs.status ()

{

"set": "test"

Date: ISODate ("2013-12-31T07:49:39Z")

"myState": 1

"members": [

{

"_ id": 0

"name": "192.168.30.112 purl 3306"

"health": 1

"state": 1

"stateStr": "PRIMARY"

"optime": {

"t": 1388474768000

"I": 1

}

OptimeDate: ISODate ("2013-12-31T07:26:08Z")

"self": true

}

{

"_ id": 1

"name": "192.168.30.113purl 3306"

"health": 1

"state": 2

"stateStr": "SECONDARY"

Uptime: 790

"optime": {

"t": 1388474768000

"I": 1

}

OptimeDate: ISODate ("2013-12-31T07:26:08Z")

LastHeartbeat: ISODate ("2013-12-31T07:49:38Z")

"pingMs": 0

}

{

"_ id": 2

"name": "192.168.30.114purl 3306"

"health": 1

"state": 2

"stateStr": "SECONDARY"

"uptime": 786

"optime": {

"t": 1388474768000

"I": 1

}

OptimeDate: ISODate ("2013-12-31T07:26:08Z")

LastHeartbeat: ISODate ("2013-12-31T07:49:38Z")

"pingMs": 0

}

]

"ok": 1

}

Check the node status.

Copy

Now let's test it and write something to the master node.

PRIMARY > db.test.save ({CRAV 3})

PRIMARY > db.test.find ()

{"_ id": ObjectId ("52c2709235dd31988bc3e7c5"), "b": 2}

{"_ id": ObjectId ("52c2719035dd31988bc3e7c6"), "c": 3}

SECONDARY > db.test.find ()

Error: {"$err": "not master and slaveok=false", "code": 13435}

Error solution:

First of all, this is normal, because SECONDARY does not allow reading and writing, and in applications that write more and read less, Replica Sets is used to achieve read-write separation. By specifying the slaveOk at connection time or in the main library, the Secondary shares the read pressure, and the Primary only takes on the write operation. Unreadable by default for secondary nodes in replica set

SECONDARY > db.getMongo () connection to 127.0.0.1:33333SECONDARY > db.getMongo (). SetSlaveOk (); not master and slaveok=false

Set up slaveok=ok on the main library

PRIMARY > db.getMongo (). SetSlaveOk ()

Testing SECONDARY > db.user.find () {"_ id": ObjectId ("4eb68b1540643e10a0000000"), "id": 1, "name": "zhangsan"} {"_ id": ObjectId ("4eb68b1540643e10a0000001"), "id": 2, "name": "; lisi"}

At this point, you go to check the log on the secondary node, and you will see that the content has been copied.

Tags: Node configuration command copy server step this is service test three thing content method pressure name object member fault file log Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Xiaomi Shulou Information Shulou Tech Info