搭建MongoDB主从复制(Master-Slave)
2014-03-28 14:44
549 查看
主从复制是MongoDB数据库一种特性,它通过数据备份来提高数据库的容灾能力。但是由于主从复制并不能自动实现故障转移的特性,MongoDB在1.6版本开发了新的复制模式:Replicate Sets。MongoDB建议不要再使用Master-Slave模式对数据进行备份。但是对于学习来说,我们仍可以了解一下MongoDB的主从复制模式。
1. 从MongoDB官网下载最新版的MongoDB,解压到某个目录。
2.在MongoDB文件夹下,创建/data/master和/data/slave目录。
3.启动主节点(Master)的服务器,运行下面的命令:
执行完上面的命令后,mongodb会在data/master下生成数据文件和日志文件。
4.启动从节点(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发生故障时,从节点无法转换为主节点的劣势就暴漏了出来。
本文出自 “这个人的IT世界” 博客,请务必保留此出处http://favccxx.blog.51cto.com/2890523/1386026
1. 从MongoDB官网下载最新版的MongoDB,解压到某个目录。
2.在MongoDB文件夹下,创建/data/master和/data/slave目录。
3.启动主节点(Master)的服务器,运行下面的命令:
mongod --dbpath /data/master --port 10000 --master |
4.启动从节点(Slave)的服务器,运行下面的命令:
mongod --dbpath /data/slave --port 10001 --slave --source localhost:10000 |
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发生故障时,从节点无法转换为主节点的劣势就暴漏了出来。
本文出自 “这个人的IT世界” 博客,请务必保留此出处http://favccxx.blog.51cto.com/2890523/1386026
相关文章推荐
- MongoDB主从复制实验 master/slave
- MongoDB的Master-Slave主从模式配置及主从复制要点解析
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- MongoDB的Master-Slave主从模式配置及主从复制要点解析
- 转】MongoDB主从复制实验 master/slave
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- mongodb (master/slave) 主从复制
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……
- MongoDB 主从复制(Master-Slaver)实验
- 详解mongoDB主从复制搭建详细过程
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
- MySQL主从复制(Master-Slave)实践(centos)
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
- Redis主从(Master-Slave)复制(Replication)设置
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
- 保存一篇关于MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)...