Home Database Mysql Tutorial 如何配置 MongoDB 副本集

如何配置 MongoDB 副本集

Jun 07, 2016 pm 03:10 PM
number

MongoDB 已经成为市面上最知名的 NoSQL 数据库。MongoDB 是面向文档的,它的无模式设计使得它在各种各样的WEB 应用当中广受欢迎。

MongoDB 已经成为市面上最知名的 NoSQL 数据库。MongoDB 是面向文档的,它的无模式设计使得它在各种各样的WEB 应用当中广受欢迎。最让我喜欢的特性之一是它的副本集(Replica Set),副本集将同一数据的多份拷贝放在一组 mongod 节点上,从而实现数据的冗余以及高可用性。

如何配置 MongoDB 副本集

这篇教程将向你介绍如何配置一个 MongoDB 副本集。

副本集的最常见配置需要一个主节点以及多个副节点。这之后启动的复制行为会从这个主节点到其他副节点。副本集不止可以针对意外的硬件故障和停机事件对数据库提供保护,同时也因为提供了更多的节点从而提高了数据库客户端数据读取的吞吐量。

 

配置环境

这个教程里,我们会配置一个包括一个主节点以及两个副节点的副本集。

如何配置 MongoDB 副本集

为了达到这个目的,我们使用了3个运行在 VirtualBox 上的虚拟机。我会在这些虚拟机上安装 Ubuntu 14.04,并且安装 MongoDB 官方包。

我会在一个虚拟机实例上配置好所需的环境,然后将它克隆到其他的虚拟机实例上。因此,选择一个名为 master 的虚拟机,执行以下安装过程。

首先,,我们需要给 apt 增加一个 MongoDB 密钥:

然后,将官方的 MongoDB 仓库添加到 source.list 中:

  • $ sudo su
  • # echo "deb "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
  • 接下来更新 apt 仓库并且安装 MongoDB。

    现在对 /etc/mongodb.conf 做一些更改

    第一行的作用是表明我们的数据库需要验证才可以使用。keyfile 配置用于 MongoDB 节点间复制行为的密钥文件。replSet 为副本集设置一个名称。

    接下来我们创建一个用于所有实例的密钥文件。

    这将会创建一个含有 MD5 字符串的密钥文件,但是由于其中包含了一些噪音,我们需要对他们清理后才能正式在 MongoDB 中使用。

    grep 命令的作用的是把将空格等我们不想要的内容过滤掉之后的 MD5 字符串打印出来。

    现在我们对密钥文件进行一些操作,让它真正可用。

    接下来,关闭此虚拟机。将其 Ubuntu 系统克隆到其他虚拟机上。

    如何配置 MongoDB 副本集

    这是克隆后的副节点1和副节点2。确认你已经将它们的MAC地址重新初始化,并且克隆整个硬盘。

    如何配置 MongoDB 副本集

    请注意,三个虚拟机示例需要在同一个网络中以便相互通讯。因此,我们需要它们弄到“互联网"上去。

    这里推荐给每个虚拟机设置一个静态 IP 地址,而不是使用 DHCP。这样它们就不至于在 DHCP 分配IP地址给他们的时候失去连接。

    像下面这样编辑每个虚拟机的 /etc/networks/interfaces 文件。

    在主节点上:

    在副节点1上:

    在副节点2上:

    由于我们没有 DNS 服务,所以需要设置设置一下 /etc/hosts 这个文件,手工将主机名称放到此文件中。

    在主节点上:

    在副节点1上:

    在副节点2上:

    使用 ping 命令检查各个节点之间的连接。

     

    配置副本集

    验证各个节点可以正常连通后,我们就可以新建一个管理员用户,用于之后的副本集操作。

    在主节点上,打开 /etc/mongodb.conf 文件,将 auth 和 replSet 两项注释掉。

    在一个新安装的 MongoDB 上配置任何用户或副本集之前,你需要注释掉 auth 行。默认情况下,MongoDB 并没有创建任何用户。而如果在你创建用户前启用了 auth,你就不能够做任何事情。你可以在创建一个用户后再次启用 auth。

    修改 /etc/mongodb.conf 之后,重启 mongod 进程。

  • $ sudo service mongod restart
  • 现在连接到 MongoDB master:

    连接 MongoDB 后,新建管理员用户。

    重启 MongoDB:

  • $ sudo service mongod restart
  • 再次连接到 MongoDB,用以下命令将 副节点1 和副节点2节点添加到我们的副本集中。

    现在副本集到手了,可以开始我们的项目了。参照 官方驱动文档 来了解如何连接到副本集。如果你想要用 Shell 来请求数据,那么你需要连接到主节点上来插入或者请求数据,副节点不行。如果你执意要尝试用副本集操作,那么以下错误信息就蹦出来招呼你了。

    如果你要从 shell 连接到整个副本集,你可以安装如下命令。在副本集中的失败切换是自动的。

    如果你使用其它驱动语言(例如,JavaScript、Ruby 等等),格式也许不同。

    希望这篇教程能对你有所帮助。你可以使用Vagrant来自动完成你的本地环境配置,并且加速你的代码。

    MongoDB 3.0 正式版发布下载 

    CentOS编译安装MongoDB

    CentOS 编译安装 MongoDB与mongoDB的php扩展

    CentOS 6 使用 yum 安装MongoDB及服务器端配置

    Ubuntu 13.04下安装MongoDB2.4.3

    MongoDB入门必读(概念与实战并重)

    Ubunu 14.04下MongoDB的安装指南

    《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

    Nagios监控MongoDB分片集群服务实战

    基于CentOS 6.5操作系统搭建MongoDB服务

    MongoDB 的详细介绍:请点这里
    MongoDB 的下载地址:请点这里

    via: How to set up a Replica Set on MongoDB

    作者:Christopher Valerio 译者:mr-ping 校对:wxy

    本文由 LCTT 原创翻译,Linux中国 荣誉推出

    本文永久更新链接地址

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    C program to find the largest prime factor of a number C program to find the largest prime factor of a number Aug 27, 2023 am 10:09 AM

    PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

    Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Top 10 Global Digital Virtual Currency Trading Platform Ranking (2025 Authoritative Ranking) Mar 06, 2025 pm 04:36 PM

    In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

    Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Top 10 digital currency trading platforms The latest list of top 10 digital currency trading platforms Mar 17, 2025 pm 05:57 PM

    Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

    Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Top 10 exchanges in the currency circle in 2025 latest digital currency app rankings Feb 27, 2025 pm 06:33 PM

    Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

    Top 10 trading platforms for digital currency apps, regular currency speculation platform app recommendations Top 10 trading platforms for digital currency apps, regular currency speculation platform app recommendations Mar 07, 2025 pm 06:51 PM

    This article recommends ten digital currency trading apps: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Poloniex. When choosing a platform, you need to consider factors such as security, liquidity, transaction fees, currency selection, user interface, customer service support and regulatory compliance, and carefully evaluate risks and never blindly follow the trend.

    What are the reliable digital currency platforms? Top 10 formal digital currency trading platforms 2025 What are the reliable digital currency platforms? Top 10 formal digital currency trading platforms 2025 Mar 17, 2025 pm 05:45 PM

    Reliable digital currency platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini. These exchanges have their own characteristics. Users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

    Top 10 recommended top 10 digital currency trading app rankings Top 10 recommended top 10 digital currency trading app rankings Feb 17, 2025 pm 03:18 PM

    As the digital currency market booms, this article lists the top 10 digital currency trading apps to help investors choose reliable and easy-to-use platforms. Ranking lists include Binance, Coinbase, Gemini, Kraken, FTX, Huobi, OKX, Bitfinex, Crypto.com and Gate.io. These apps offer a wide range of trading pairs, a secure and reliable platform, low transaction fees and high liquidity. Investors should choose the app that best meets their needs based on factors such as security, user-friendliness, transaction fees, liquidity, supported currencies and customer service.

    The world's top ten virtual currency trading platform app genuine download and installation tutorial The world's top ten virtual currency trading platform app genuine download and installation tutorial Mar 12, 2025 pm 05:33 PM

    This article provides Android and Apple mobile APP download methods for mainstream digital currency trading platforms such as Binance, OKX, Gate.io, Huobi Global, Coinbase, KuCoin, Kraken and Bitfinex. Whether it is an Android user or an Apple user, you can easily find the official APP download link for the corresponding platform and complete the installation according to the steps. The article provides detailed guidance on searching and downloading on their respective official websites or app stores, and provides instructions on the special steps for installing APK files on Android, so that users can download and use them quickly and easily.

    See all articles