Home > Database > Mysql Tutorial > body text

搭建MongoDB主从复制(Master-Slave)集

WBOY
Release: 2016-06-07 16:47:23
Original
1121 people have browsed it

主从复制是MongoDB数据库一种特性,它通过数据备份来提高数据库的容灾能力。但是由于主从复制并不能自动实现故障转移的特性,Mon

  主从复制是MongoDB数据库一种特性,它通过数据备份来提高数据库的容灾能力。但是由于主从复制并不能自动实现故障转移的特性,MongoDB在1.6版本开发了新的复制模式:Replicate Sets。MongoDB建议不要再使用Master-Slave模式对数据进行备份。但是对于学习来说,我们仍可以了解一下MongoDB的主从复制模式。

  1. 从MongoDB官网下载最新版的MongoDB,解压到某个目录。

  2.在MongoDB文件夹下,创建/data/master和/data/slave目录。

  3.启动主节点(Master)的服务器,运行下面的命令:

mongod --dbpath /data/master --port 10000 --master

搭建MongoDB主从复制(Master-Slave)集

  执行完上面的命令后,mongodb会在data/master下生成数据文件和日志文件。

  4.启动从节点(Slave)的服务器,运行下面的命令:

mongod --dbpath /data/slave --port 10001 --slave --source localhost:10000

搭建MongoDB主从复制(Master-Slave)集

  执行完上面命令后,同样会在data/slave下生成数据文件和日志文件,并在local数据库下的sources表下创建与master的关联信息。

  5.测试主从复制(Master-Slave)是否生效。

  使用“mongo localhost:10000”打开Master数据库,插入一条测试语句:

  db.test.find();

  db.test.insert({“host”:“1000”});

  db.test.find();

  使用“mongo localhost:10001”打开Slave数据库,运行db.test.find()会发现查询出来的数据与从Master数据库中查询的数据相同。

  运行db.test.insert({"girl":"lili"}); 则会显示not master。这是因为主从复制模式只允许从Master数据库更新数据,而不允许从Slave数据库更新数据。正因此,当Master发生故障时,,从节点无法转换为主节点的劣势就暴漏了出来。

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

相关阅读:

MongoDB备份与恢复

CentOS编译安装MongoDB

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

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

Ubuntu 13.04下安装MongoDB2.4.3

如何在MongoDB中建立新数据库和集合

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

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

linux

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!