Get the App
SLTechnology News&Howtos  ›  Database  › 

How to build MongoDB Replica Sets

Shulou Source: shulou.com Published: 2022-05-31 17:28:42 10月06日 Update

This article is to share with you about how to build MongoDB Replica Sets, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

No matter what language we learn, being able to type a HelloWorld becomes our first step. It is equivalent to the HelloWorld built by MongoDBReplicaSets.

How to build MongoDBReplicaSets

The following is to call getlasterror after doing an operation on ReplicaSets to synchronize the write operation to at least 3 machines before returning.

Db.runCommand ({getlasterror:1,w:3})

Start building and start the three mongodserver:

Mongod--replSetprod--port27017--dbpath/data/node1

Mongod--replSetprod--port27027--dbpath/data/node2

Mongod--replSetprod--port27037--dbpath/data/node3

Perform one of the mongod to complete the relevant configuration

Replica_config= {_ id:'prod',members: [

{_ id:0,host:'localhost:27017'}

{_ id:1,host:'localhost:27027'}

{_ id:2,host:'localhost:27037'}]}

# Nowinitiatethereplica_config

Rs.initiate (replica_config)

Ruby client code:

#! / usr/bin/envruby

Require'mongo'

Begin

How to build MongoDBReplicaSets

@ connection=Mongo::Connection.multi ([

['localhost',27017]

['localhost',27027]

['localhost',27037]])

@ collection=@connection.db ("sales") .collection ("products")

Product= {"name" = > "Refactoring"

"code" = > "023XX3"

"type" = > "book"

"in_stock" = > 100}

@ collection.insert (product)

100.timesdo

Sleep0.5

Begin

Product=@collection.find_one "code" = > "023XX3"

Puts "FoundBook:" + product ["name"]

RescueException= > e

Putse.message

Next

End

The above is how to build MongoDB Replica Sets, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: More knowledge article learning practical three code client client that is work meeting article is in machine look knowledge point industry details language information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Microsoft OPPO Reno Huawei