Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Build a mongodb replica (one master and two backups)

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Build a mongodb replica (one master and two backups)

Master: 192.168.0.237

Standby 192.168.0.122

Prepare 2Rank 192.168.0.123

Three machines operate in the same way

Tar zxf mongodb-linux-i686-1.8.1.tgz

Mv mongodb-linux-i686-1.8.1 / usr/local/mongodb

Cd / usr/local/mongodb/

Mkdir etc

Mkdir logs

Cd logs/

Touch mongodb.log

Cd..

Cd etc/

Vi mongodb.cnf

Join

Dbpath=/data/cc

Mkdir / data/cc

/ usr/local/mongodb/bin/mongod-dbpath=/data/cc-logpath=/usr/local/mongodb/logs/mongodb.log-f / usr/local/mongodb/etc/mongodb.cnf-replSet repset-fork

All installed and started successfully

Log in to mongodb on any of the three machines

/ usr/local/mongodb/bin/mongo

# using admin database

Use admin

# define the replica set configuration variable, where the _ id: "repset" is the same as the above command parameter "- replSet repset".

Config = {_ id: "repset", members: [

... {_ id:0,host: "192.168.0.122purl 27017"}

... {_ id:1,host: "192.168.0.123purl 27017"}

... {_ id:2,host: "192.168.0.237purl 27017"}]

...}

# output

{

"_ id": "repset"

"members": [

{

"_ id": 0

"host": "192.168.0.122 purl 27017"

}

{

"_ id": 1

"host": "192.168.0.123 purl 27017"

}

{

"_ id": 2

"host": "192.168.0.237purl 27017"

}

]

}

# initialize replica set configuration

Rs.initiate (config)

# output successful

one

{

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

"ok": 1

}

# View the status of cluster nodes

Rs.status ()

# output

{

"set": "repset"

Date: ISODate ("2014-05-05T10:51:50Z")

"myState": 2

"members": [

{

"_ id": 0

"name": "192.168.0.122 purl 27017"

"health": 1

"state": 2

"stateStr": "SECONDARY"

"optime": {

"t": 1399286403000

"I": 1

}

OptimeDate: ISODate ("2014-05-05T10:40:03Z")

"self": true

}

{

"_ id": 1

"name": "192.168.0.123 purl 27017"

"health": 1

"state": 2

"stateStr": "SECONDARY"

"uptime": 920

"optime": {

"t": 1399286403000

"I": 1

}

OptimeDate: ISODate ("2014-05-05T10:40:03Z")

LastHeartbeat: ISODate ("2014-05-05T10:51:48Z")

}

{

"_ id": 2

"name": "192.168.0.237purl 27017"

"health": 1

"state": 1

"stateStr": "PRIMARY"

"uptime": 920

"optime": {

"t": 1399286403000

"I": 1

}

OptimeDate: ISODate ("2014-05-05T10:40:03Z")

LastHeartbeat: ISODate ("2014-05-05T10:51:48Z")

}

]

"ok": 1

}

The entire replica set has been built successfully.

6. Test the data replication function of replica set

# Connect to the terminal on the primary node 192.168.0.237:

Mongo 127.0.0.1

# establish a test database.

Use test

Insert data into the testdb table.

> db.testdb.insert ({"test1": "testval1"})

# Connect to mongodb on replica nodes 192.168.0.122 and 192.168.0.123 to see if the data has been copied.

/ usr/local/mongodb/bin/mongo 192.168.0.122:27017

# use test database.

Repset:SECONDARY > use test

Repset:SECONDARY > show tables

# output

Sun Dec 29 21 not master and slaveOk=false 50 not master and slaveOk=false 48.590 error: {"$err": "not master and slaveOk=false", "code": 13435} at src/mongo/shell/query.js:128

This is normal because SECONDARY does not allow reading and writing, and if you have to solve it, the method is as follows:

Repset:SECONDARY > rs.slaveOk (); or

Repset:SECONDARY > db.getMongo (). SetSlaveOk ()

You can see that the data has been replicated to the replica set.

Repset:SECONDARY > db.testdb.find ()

# output

{"_ id": ObjectId ("52c028460c7505626a93944f"), "test1": "testval1"}

If there is a failure in this method, you need to specify mongodb ip manually

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report