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

How to build MongoDB Replica Sets

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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.

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