简介:
mongodb有两种类型的复制,第一种是同于MySQL的主从复制模式,第二种是复制集,提供了自动故障转移的主从复制集群。其中复制集没有固定的主节点,当一个主机的故障后从节点会重新“选举”出一个新的主节点,从而提高的系统的可用性
一、实验环境:
(1)各节点信息:
node1: 172.16.2.12
node2: 172.16.2.13
node3: 172.16.2.14
(2)各个节点之间确保时间同步
(3)各个节点确保iptables和selinux以关闭
二、在配置复制集时我们需要了解复制集的影响因素
(1)复制集(副本集)重新选举的影响条件
心跳信息(heartbeat)、优先级(priority)、optime(某成员节点最近一次应用本地oplog的时间戳)、网络连接、网络分区
(2)触发选举的事件:
①新副本集初始化;
②从节点联系不到主节点;
③主节点"下台"时;有以下原因会导致主节点"下台"
主节点收到setupDown()命令时会下台;
某从节点有更高的优先级且已经满足成为主节点的其他所有条件;
主节点无法联系到副本集的"多数方"
三、配置过程
(1)各个节点安装mongodb
[mongodb-org-2.6] \\准备yum源 name=MongoDB 2.6 Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
# yum -y install mongodb-org-server mongodb-org-shell mongodb-org-tools \\每个节点都要安装
(2)创建mongodb数据存放目录
# mkdir -vp /mongodb/data; chown -R mongod.mongod /mongodb \\每个节点都要创建,方法相同
(3)编辑mongodb配置文件:/etc/mongod.conf,修改内容如下:
[root@node1 ~]# cat /etc/mongod.conf # mongod.conf #where to log logpath=/var/log/mongodb/mongod.log \\指定log日志文件路径 logappend=true \\是否自动切割日志 # fork and run in background fork=true \\是否在后端运行 #port=27017 \\指定监听端口;默认即可 dbpath=/mongodb/data \\指定数据存放路径 # location of pidfile pidfilepath=/var/run/mongodb/mongod.pid \\指明pid文件路径 # Listen to local interface only. Comment out to listen on all interfaces. #bind_ip=127.0.0.1 \\指定监听端口,默认为127.0.0.1,最好根据实际需求定义 # Disables write-ahead journaling # nojournal=true # Enables periodic logging of CPU utilization and I/O wait #cpu=true # Turn on/off security. Off is currently the default #noauth=true #auth=true # Verbose logging output. #verbose=true # Inspect all client data for validity on receipt (useful for # developing drivers) #objcheck=true # Enable db quota management #quota=true # Set oplogging level where n is # 0=off (default) # 1=W # 2=R # 3=both # 7=W+some reads #diaglog=0 # Ignore query hints #nohints=true # Enable the HTTP interface (Defaults to port 28017). #httpinterface=true #rest=true # Turns off server-side scripting. This will result in greatly limited # functionality #noscripting=true # Turns off table scans. Any query that would do a table scan fails. #notablescan=true # Disable data file preallocation. #noprealloc=true # Specify .ns file size for new databases. # nssize=<size> # Replication Options # in replicated mongo databases, specify the replica set name here replSet=one \\最重要的一项,设置复制集的名称,这里设置为“one” replIndexPrefetch=_id_only \\这里设置取回的索引,此次设置的是"_id_only" # maximum size in megabytes for replication operation log #oplogSize=1024 # path to a key file storing authentication info for connections # between replica set members #keyFile=/path/to/keyfile
[root@node1 ~]# scp /etc/mongod.conf node2:/etc/ \\复制配置文件给node2节点一份 [root@node1 ~]# scp /etc/mongod.conf node3:/etc/ \\复制配置文件给node3节点一份
[root@node1 ~]# /etc/init.d/mongod start Starting mongod: [ OK ] [root@node2 ~]# /etc/init.d/mongod start Starting mongod: [ OK ] [root@node3 ~]# /etc/init.d/mongod start Starting mongod: [ OK ]
(4)配置主节点
[root@node1 ~]# mongo \\登录到mongodb数据库 MongoDB shell version: 2.6.11 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user
> rs.initiate() \\初始化复制集 { "info2" : "no configuration explicitly specified -- making one", "me" : "node1.linux.com:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } > one:PRIMARY> \\初始化复制集后,提示符变成现在的样子 one:PRIMARY> rs.add("172.16.2.13") \\添加node2从节点 { "ok" : 1 } one:PRIMARY> rs.add("172.16.2.14") \\添加node3从节点 { "ok" : 1 }
(5)配置从节点
[root@node2 ~]# mongo \\链接node2节点的mongodb MongoDB shell version: 2.6.11 connecting to: test one:SECONDARY> rs.slaveOk() \\启动node2从节点 [root@node3 ~]# mongo \\链接node2节点的mongodb MongoDB shell version: 2.6.11 connecting to: test one:SECONDARY> rs.slaveOk() \\启动node3从节点
(6)查看各个节点信息:
one:PRIMARY> rs.status() { "set" : "one", \\“one”复制集的名称 "date" : ISODate("2015-08-29T08:59:14Z"), \\日期类型 "myState" : 1, \\当前状态 "members" : [ \\复制集成员信息 { "_id" : 0, \\主节点ID "name" : "node1.linux.com:27017", \\主节点的主机名 "health" : 1, \\节点健康值 "state" : 1, \\状态 "stateStr" : "PRIMARY", \\当前为主节点 "uptime" : 848, \\运行时长 "optime" : Timestamp(1440838386, 1), \\时间戳 "optimeDate" : ISODate("2015-08-29T08:53:06Z"), "electionTime" : Timestamp(1440838311, 2), "electionDate" : ISODate("2015-08-29T08:51:51Z"), "self" : true \\如果在当前主机则为true,否则为false }, { "_id" : 1, "name" : "172.16.2.13:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 372, "optime" : Timestamp(1440838386, 1), "optimeDate" : ISODate("2015-08-29T08:53:06Z"), "lastHeartbeat" : ISODate("2015-08-29T08:59:12Z"), "lastHeartbeatRecv" : ISODate("2015-08-29T08:59:13Z"), "pingMs" : 1, "syncingTo" : "node1.linux.com:27017" \\从那台主机同步数据 }, { "_id" : 2, "name" : "172.16.2.14:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 368, "optime" : Timestamp(1440838386, 1), "optimeDate" : ISODate("2015-08-29T08:53:06Z"), "lastHeartbeat" : ISODate("2015-08-29T08:59:13Z"), "lastHeartbeatRecv" : ISODate("2015-08-29T08:59:14Z"), "pingMs" : 1, "syncingTo" : "node1.linux.com:27017" } ], "ok" : 1 }
(7)验证复制集节点数据是否同步
one:PRIMARY> use testdb \\切换到新的数据库; switched to db testdb one:PRIMARY> for(i=1;i<=100;i++) db.test.insert({name: "stu"+i, age: "i%100", class: "Net12"}) \\在主节点插入数据
one:SECONDARY> db.test.find() \\在node2从节点查看主节点插入的数据 { "_id" : ObjectId("55e177388aa7580730a33942"), "name" : "stu1", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33943"), "name" : "stu2", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33944"), "name" : "stu3", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33945"), "name" : "stu4", "age" : "i%100", "class" : "Net12" }
one:SECONDARY> db.test.find() \\在node3节点查看主节点插入的数据 { "_id" : ObjectId("55e177388aa7580730a33942"), "name" : "stu1", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33943"), "name" : "stu2", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33944"), "name" : "stu3", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33945"), "name" : "stu4", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33946"), "name" : "stu5", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33947"), "name" : "stu6", "age" : "i%100", "class" : "Net12" } { "_id" : ObjectId("55e177388aa7580730a33948"), "name" : "stu7", "age" : "i%100", "class" : "Net12" }
(8)验证从节点是否可以写入数据
one:SECONDARY> db.test.insert({name: "stu101", age: 20, class: "Net12"})\\在node2测试,从节不能写入 WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
one:SECONDARY> db.test.insert({name: "stu101", age: 20, class: "Net12"})\\在node3测试,从节点不能写入 WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
(9)通过以上测试,可以证明mongodb的复制集已经可以正常工作同时从节点只有读权限,而主节点有读写权限
(10)mongodb额外命令可以通过rs.help()查看帮助信息,下面只介绍常用的几个命令
one:PRIMARY> rs.printReplicationInfo() \\打印出复制集信息 configured oplog size: 1301.37744140625MB log length start to end: 1169secs (0.32hrs) oplog first event time: Sat Aug 29 2015 16:51:51 GMT+0800 (CST) oplog last event time: Sat Aug 29 2015 17:11:20 GMT+0800 (CST) now: Sat Aug 29 2015 17:19:35 GMT+0800 (CST) one:PRIMARY> rs.printSlaveReplicationInfo() \\查看主从复制延迟 source: 172.16.2.13:27017 syncedTo: Sat Aug 29 2015 17:11:20 GMT+0800 (CST) 0 secs (0 hrs) behind the primary source: 172.16.2.14:27017 syncedTo: Sat Aug 29 2015 17:11:20 GMT+0800 (CST) 0 secs (0 hrs) behind the primary one:PRIMARY>
谢谢大家花时间看完我写的博客,若有不足支持请多多提出宝贵的意见。O(∩_∩)O
原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/7764