如何使用MongoDB配置副本集 [英] How to configure a replica set with MongoDB

查看:84
本文介绍了如何使用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.

我想为此在MongoDB中配置复制集

I want configure Replication Set in MongoDB for that i tried like this

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 }

我在这里做错了什么?

有什么想法吗?

推荐答案

您实际上可以遵循 MongoDB手动来设置您的副本集.很清楚 以下是一些关键步骤,如下所述:

You can actually follow the MongoDB Manual to setup your replica set. It's pretty clear. Here are some critical steps described below:

  1. 更改配置文件并添加以下行.请勿将其与主/从复制搞混,因为副本集是对主/从复制的替换.因此,您可能需要从配置文件中删除那些与主/从相关的配置.

  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]

编辑:replSet在mongoDB的最新版本中似乎不再存在,至少它是

EDIT: The replSet does not seem to exist anymore in recent versions of mongoDB, at least it is no longer documented. The following seems to have done the trick in my case.

replication: 
  replSetName: "smm"

  • 重新启动mongod实例:

  • Restart your mongod instance:

    systemctl restart mongodb
    // or
    service mongod restart
    

  • 转到本地数据库并启动您的副本集.不要将任何东西传递给启动函数,而mongodb会很好地处理所有事情.请注意,它将使用您的主机名作为当前实例的名称,并且我知道更改起来并不容易.因此,您可能需要先更改主机名.

  • go to local database and initiate your replica set. Don't pass anything to the initiate function, and mongodb will handle everything just well. 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()
    

  • 就是这样.你的布景很好.如果您还有其他成员加入集合,则需要执行1/2步,然后转到主实例并键入:

    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:

    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.

    这篇关于如何使用MongoDB配置副本集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆