简体   繁体   English

如何使用MongoDB配置副本集

[英]How to configure a replica set with MongoDB

I've got this problem that I can't solve. 我遇到了我无法解决的问题。 Partly because I can't explain it with the right terms. 部分原因是我无法用正确的术语进行解释。 I'm new to this so sorry for this clumsy question. 我对此很陌生,所以很抱歉这个笨拙的问题。

Below you can see an overview of my goal. 您可以在下面看到我的目标概述。

I want configure Replication Set in MongoDB for that i tried like this 我想在MongoDB中为我这样尝试配置复制集

use local
db.dropDatabase()

config = { _id: "rs0", members:[
{_id: 0, host: 'localhost:27017'}]
}

rs.initiate(config)

i hope every thing is correct only but here its showing the following error message 我希望每件事都是正确的,但是在这里显示以下错误消息

{ "errmsg" : "server is not running with --replSet", "ok" : 0 }

Anything wrong i did here ? 我在这里做错了什么?

Any ideas ? 有任何想法吗 ?

You can actually follow the MongoDB Manual to setup your replica set. 您实际上可以按照MongoDB手册来设置副本集。 It's pretty clear. 很清楚 Here are some critical steps described below: 以下是一些重要步骤,如下所述:

  1. Change your configuration file and add the following line.Don't mess it up with master/slave replication, because replica set is meant to be a replacement of master/slave replication. 更改配置文件并添加以下行。请勿将其与主/从复制搞混,因为副本集旨在替代主/从复制。 So you may want to remove those master/slave related config from your configuration files. 因此,您可能需要从配置文件中删除那些与主/从相关的配置。

     replSet = [set name] 

    EDIT : The replSet does not seem to exist anymore in recent versions of mongoDB, at least it is no longer documented . 编辑replSet在mongoDB的最新版本中似乎不再存在,至少不再记录了 The following seems to have done the trick in my case. 在我看来,以下似乎已经解决了问题。

     replication: replSetName: "smm" 
  2. Restart your mongod instance: 重新启动您的mongod实例:

     systemctl restart mongodb // or service mongod restart 
  3. go to local database and initiate your replica set. 转到本地数据库并启动您的副本集。 Don't pass anything to the initiate function, and mongodb will handle everything just well. 不要将任何东西传递给启动函数,而mongodb会很好地处理所有事情。 Note it will use your hostname as the name of current instance and as I know it's not that easy to change. 请注意,它将使用您的主机名作为当前实例的名称,并且我知道更改起来并不容易。 So you may want to change your host name before doing so. 因此,您可能需要先更改主机名。

     use local rs.initiate() 

That's it. 而已。 Your set is good to go. 你的布景很好。 If you have other member to join the set, you need to do the 1/2 steps, and go to your primary instance and type: 如果您还有其他成员加入集合,则需要执行1/2步,然后转到主实例并键入:

rs.add("hostname:port")

Only when you want to change configs of replica set, do you need to type: 仅当您要更改副本集的配置时,才需要输入:

var conf = rs.conf();
// change your conf here
rs.reconfig(conf);

Note this will lead to server offline a little bit time. 请注意,这将导致服务器离线一段时间。 If you are doing it online, be careful. 如果您在线上进行操作,请当心。

I would wish to discuss the concept of replication and replica set in MongoDB . 我希望讨论MongoDB中复制和副本集的概念。

How do we get availability and fault tolerance? 我们如何获得可用性和容错能力? And by that we mean if that node goes down, we want to still be able to use the system. 这就是说,如果该节点出现故障,我们仍希望能够使用该系统。 And if the primary nodes goes down and we lose it entirely for some reason, let's say there's a lost between backups or a hardware damage making the system unusable. 而且如果主节点出现故障,并且由于某种原因我们完全丢失了它,那么可以说备份之间丢失了,或者硬件损坏了,导致系统无法使用。 And so what we do to solve both those problems is we introduce replication . 因此,为了解决这两个问题,我们要做的就是引入复制

A replica set refers to a set of mongod and mongo nodes that act together and all mirror each other in terms of data. 副本集是指一组mongodmongo节点,它们一起起作用,并且在数据方面都相互镜像。 There is one primary and the other nodes are secondaries. 有一个主节点,其他节点是辅助节点。 But, that selection is dynamic. 但是,这种选择是动态的。 And the data written to the primary will asynchronously replicate to the secondaries. 并且写入主数据库的数据将异步复制到辅助数据库。 The application and drivers stay connected to the primary and will and can only write to the primary. 应用程序和驱动程序保持与主数据库的连接,并且将只能写入主数据库。 Say, if primary goes down, one of the secondaries will perform an election to elect a new primary. 假设,如果小学选修失败,其中一名中学将进行选举以选举新的小学。 To elect a new primary, we have to have a strict majority of the original number of nodes. 要选择一个新的主节点,我们必须拥有原始节点数的严格多数。 So, since the original number of nodes here was 3 , we need 2 nodes to elect a new primary and that's the number we have. 因此,由于此处的原始节点数为3 ,因此我们需要2个节点来选择一个新的主节点,这就是我们拥有的节点数。 So, if one went down, then anyone else can become primary. 因此,如果一个人失败了,那么其他任何人都可以成为主要人。 And in that case, the app will connect to the primary for the rights, through the driver. 在这种情况下,该应用将通过驱动程序连接到主要权限。 All transparently. 全部透明。

Later if the down servers gets up, it will join the replica set as a secondary. 稍后,如果关闭的服务器启动,它将作为辅助服务器加入副本集。 And the minimum number of nodes is 3 - because if we have less than 3, then what would remain would not be a majority of that set of the original set. 最小的节点数是3-因为如果我们少于3,那么剩下的将不是原始集合的大部分。 And so, there would be no way to elect the new primary. 因此,将没有办法选举新的小学。 So, we would go with no primary, which means we could no longer take rights. 因此,我们将没有主要权利,这意味着我们将不再拥有权利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM