准备工作:
host zy_mongo01 副本集主节点
hostname 121.41.101.208 10.168.218.46
host zy_mongo02 副本集副本节点
hostname 121.41.118.184 10.168.217.186
host zy_mongo03 副本集副本节点
hostname 121.40.61.78 10.252.130.121
mongodb 目录:
mkdir -p /data/mongodb/replset/data 存放mongo数据目录
mkdir -p /data/mongodb/replset 存放mongo文件目录
安装操作:
tar xf mongodb-linux-x86_64-2.6.8.tgz
mv mongodb-linux-x86_64-2.6.8 /data/mongodb/mongodb
启动:
在三台机器上做如下操作:
/data/mongodb/mongodb/bin/mongod --dbpath /data/mongodb/replset/data/ --replSet repset
提示日志:
2015-04-21T11:17:23.238+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2015-04-21T11:17:24.238+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2015-04-21T11:17:25.238+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
配置mongodb,在其中一个mongo机器上登录:
登录方式: /data/mongodb/mongodb/bin/mongo
做如下操作:
> use admin
switched to db admin
> config = { _id:"repset", members:[
... {_id:0,host:"10.168.218.46:27017"},
... {_id:1,host:"10.168.217.186:27017"},
... {_id:2,host:"10.252.130.121:27017"}]
... }
{
"_id" : "repset",
"members" : [
{
"_id" : 0,
"host" : "10.168.218.46:27017"
},
{
"_id" : 1,
"host" : "10.168.217.186:27017"
},
{
"_id" : 2,
"host" : "10.252.130.121:27017"
}
]
}
>
化配置文件:
> rs.initiate(config);
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
查看日志 看到mongoDB状态
2015-04-21T11:54:35.579+0800 [conn1] replSet info saving a newer config version to local.system.replset: { _id: "repset", version: 1, members: [ { _id: 0, host: "10.168.218.46:27017" }, { _id: 1, host: "10.168.217.186:27017" }, { _id: 2, host: "10.252.130.121:27017" } ] }
2015-04-21T11:54:35.580+0800 [conn1] build index on: local.system.replset properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.system.replset" }
2015-04-21T11:54:35.580+0800 [conn1] added index to empty collection
2015-04-21T11:54:35.580+0800 [conn1] replSet saveConfigLocally done
2015-04-21T11:54:35.580+0800 [conn1] replSet replSetInitiate config now saved locally. Should come online in about a minute.
2015-04-21T11:54:36.528+0800 [rsStart] replSet I am 10.168.218.46:27017
2015-04-21T11:54:36.529+0800 [rsHealthPoll] replSet member 10.168.217.186:27017 is up
2015-04-21T11:54:36.529+0800 [rsHealthPoll] replSet member 10.252.130.121:27017 is up
2015-04-21T11:54:36.531+0800 [rsStart] build index on: local.me properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.me" }
2015-04-21T11:54:36.531+0800 [rsStart] added index to empty collection
2015-04-21T11:54:36.531+0800 [rsStart] replSet STARTUP2
2015-04-21T11:54:36.531+0800 [rsSync] replSet SECONDARY
2015-04-21T11:54:36.532+0800 [rsMgr] replSet not electing self, not all members up and we have been up less than 5 minutes
2015-04-21T11:54:36.532+0800 [rsMgr] replSet not electing self, not all members up and we have been up less than 5 minutes
2015-04-21T11:54:36.533+0800 [rsMgr] replSet not electing self, not all members up and we have been up less than 5 minutes
2015-04-21T11:54:36.921+0800 [initandlisten] connection accepted from 10.252.130.121:58941 #2 (2 connections now open)
2015-04-21T11:54:36.922+0800 [initandlisten] connection accepted from 10.252.130.121:58942 #3 (3 connections now open)
2015-04-21T11:54:36.954+0800 [conn2] end connection 10.252.130.121:58941 (2 connections now open)
2015-04-21T11:54:37.113+0800 [initandlisten] connection accepted from 10.168.217.186:40487 #4 (3 connections now open)
2015-04-21T11:54:37.113+0800 [conn4] end connection 10.168.217.186:40487 (2 connections now open)
2015-04-21T11:54:37.113+0800 [initandlisten] connection accepted from 10.168.217.186:40488 #5 (3 connections now open)
2015-04-21T11:54:38.530+0800 [rsHealthPoll] replset info 10.168.217.186:27017 thinks that we are down
2015-04-21T11:54:38.530+0800 [rsHealthPoll] replSet member 10.168.217.186:27017 is now in state STARTUP2
2015-04-21T11:54:38.530+0800 [rsHealthPoll] replset info 10.252.130.121:27017 thinks that we are down
2015-04-21T11:54:38.530+0800 [rsHealthPoll] replSet member 10.252.130.121:27017 is now in state STARTUP2
2015-04-21T11:54:38.530+0800 [rsMgr] not electing self, 10.252.130.121:27017 would veto with 'I don't think 10.168.218.46:27017 is electable'
2015-04-21T11:54:38.531+0800 [rsMgr] not electing self, 10.252.130.121:27017 would veto with 'I don't think 10.168.218.46:27017 is electable'
2015-04-21T11:54:44.548+0800 [rsMgr] replSet info electSelf 0
2015-04-21T11:54:45.532+0800 [rsMgr] replSet PRIMARY
查看副本集状态 详细的列出了 mongo主从状态信息
[root@zy_mongo01 ~]# /data/mongodb/mongodb/bin/mongo
MongoDB shell version: 2.6.8
connecting to: test
repset:PRIMARY> rs.status();
{
"set" : "repset",
"date" : ISODate("2015-04-21T03:59:09Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.168.218.46:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 543,
"optime" : Timestamp(1429588475, 1),
"optimeDate" : ISODate("2015-04-21T03:54:35Z"),
"electionTime" : Timestamp(1429588484, 1),
"electionDate" : ISODate("2015-04-21T03:54:44Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.168.217.186:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 273,
"optime" : Timestamp(1429588475, 1),
"optimeDate" : ISODate("2015-04-21T03:54:35Z"),
"lastHeartbeat" : ISODate("2015-04-21T03:59:08Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T03:59:09Z"),
"pingMs" : 0,
"syncingTo" : "10.168.218.46:27017"
},
{
"_id" : 2,
"name" : "10.252.130.121:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 273,
"optime" : Timestamp(1429588475, 1),
"optimeDate" : ISODate("2015-04-21T03:54:35Z"),
"lastHeartbeat" : ISODate("2015-04-21T03:59:08Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T03:59:09Z"),
"pingMs" : 1,
"syncingTo" : "10.168.218.46:27017"
}
],
"ok" : 1
}
repset:PRIMARY>
测试副本集,在主的创建数据库插入,然后看从的是否能同步
repset:PRIMARY> use test;
switched to db test
repset:PRIMARY> db.testdb.insert({"test1":"testval1"})
WriteResult({ "nInserted" : 1 })
repset:PRIMARY>
在从服务器上查看
[root@zy_mongo02 ~]# /data/mongodb/mongodb/bin/mongo
MongoDB shell version: 2.6.8
connecting to: test
repset:SECONDARY> show dbs;
admin (empty)
local 1.078GB
test 0.078GB
repset:SECONDARY> use test
switched to db test
repset:SECONDARY> show tables;
2015-04-21T12:04:58.540+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
repset:SECONDARY>
[root@zy_mongo03 ~]# /data/mongodb/mongodb/bin/mongo
MongoDB shell version: 2.6.8
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
repset:SECONDARY> show dbs;
admin (empty)
local 1.078GB
test 0.078GB
repset:SECONDARY> use test;
switched to db test
repset:SECONDARY> show tables;
2015-04-21T12:05:29.160+0800 error: { "$err" : "not master and slaveOk=false", "code" : 13435 } at src/mongo/shell/query.js:131
repset:SECONDARY>
mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读。
repset:SECONDARY> db.getMongo().setSlaveOk();
可以看到数据已经复制到了副本集。以及输出:
repset:SECONDARY> db.testdb.find();
{ "_id" : ObjectId("5535cc2ed18a01e86aec9738"), "test1" : "testval1" }
repset:SECONDARY>
测试副本集故障转移集群
#在mongo1上杀死进程
[root@zy_mongo01 ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 920/sshd
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1127/mongod
[root@zy_mongo01 ~]# kill -9 1127
[root@zy_mongo01 ~]#
在从节点查看状态,其中杀掉的直接没有数据发送
[root@zy_mongo02 ~]# /data/mongodb/mongodb/bin/mongo
MongoDB shell version: 2.6.8
connecting to: test
repset:SECONDARY> rs.status();
{
"set" : "repset",
"date" : ISODate("2015-04-21T05:03:54Z"),
"myState" : 2,
"syncingTo" : "10.252.130.121:27017",
"members" : [
{
"_id" : 0,
"name" : "10.168.218.46:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(1429589038, 1),
"optimeDate" : ISODate("2015-04-21T04:03:58Z"),
"lastHeartbeat" : ISODate("2015-04-21T05:03:52Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T04:58:29Z"),
"pingMs" : 0
},
{
"_id" : 1,
"name" : "10.168.217.186:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4421,
"optime" : Timestamp(1429589038, 1),
"optimeDate" : ISODate("2015-04-21T04:03:58Z"),
"self" : true
},
{
"_id" : 2,
"name" : "10.252.130.121:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4154,
"optime" : Timestamp(1429589038, 1),
"optimeDate" : ISODate("2015-04-21T04:03:58Z"),
"lastHeartbeat" : ISODate("2015-04-21T05:03:54Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T05:03:54Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1429592314, 1),
"electionDate" : ISODate("2015-04-21T04:58:34Z")
}
],
"ok" : 1
}
repset:SECONDARY>
然后再插入数据测试 看是否数据能同步过去
#插入数据
repset:PRIMARY> use test;
switched to db test
repset:PRIMARY> db.testdb.insert({"test1":"testval1"})
WriteResult({ "nInserted" : 1 })
repset:PRIMARY> db.testdb.insert({"test2":"testval2"})
WriteResult({ "nInserted" : 1 })
repset:PRIMARY> show tables;
system.indexes
testdb
repset:PRIMARY> db.testdb.find();
{ "_id" : ObjectId("5535cc2ed18a01e86aec9738"), "test1" : "testval1" }
{ "_id" : ObjectId("5535dc9b4ae39b27c85c8c99"), "test1" : "testval1" }
{ "_id" : ObjectId("5535dca24ae39b27c85c8c9a"), "test2" : "testval2" }
repset:PRIMARY>
启动刚我们停止掉的mongo1,然后在查看我们这台机器启动之后是不是又变成主服务器了
#查看日志 我们刚启动起来之后,节点会变成从节点
2015-04-21T13:19:01.676+0800 [initandlisten] waiting for connections on port 27017
2015-04-21T13:19:01.737+0800 [rsStart] replSet I am 10.168.218.46:27017
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replset info 10.168.217.186:27017 thinks that we are down
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replSet member 10.168.217.186:27017 is up
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replSet member 10.168.217.186:27017 is now in state SECONDARY
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replset info 10.252.130.121:27017 thinks that we are down
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replSet member 10.252.130.121:27017 is up
2015-04-21T13:19:01.738+0800 [rsHealthPoll] replSet member 10.252.130.121:27017 is now in state PRIMARY
2015-04-21T13:19:01.748+0800 [rsStart] replSet STARTUP2
2015-04-21T13:19:01.749+0800 [rsSync] replSet SECONDARY
2015-04-21T13:19:03.015+0800 [initandlisten] connection accepted from 10.168.217.186:44350 #1 (1 connection now open)
2015-04-21T13:19:03.079+0800 [initandlisten] connection accepted from 10.168.217.186:44351 #2 (2 connections now open)
2015-04-21T13:19:03.158+0800 [conn1] end connection 10.168.217.186:44350 (1 connection now open)
2015-04-21T13:19:03.173+0800 [initandlisten] connection accepted from 10.252.130.121:34574 #3 (2 connections now open)
2015-04-21T13:19:03.429+0800 [conn3] end connection 10.252.130.121:34574 (1 connection now open)
2015-04-21T13:19:03.429+0800 [initandlisten] connection accepted from 10.252.130.121:34575 #4 (2 connections now open)
2015-04-21T13:19:05.749+0800 [rsBackgroundSync] replSet syncing to: 10.168.217.186:27017
查看状态:
repset:SECONDARY> re.status();
2015-04-21T13:20:18.781+0800 ReferenceError: re is not defined
repset:SECONDARY> rs.status();
{
"set" : "repset",
"date" : ISODate("2015-04-21T05:20:22Z"),
"myState" : 2,
"syncingTo" : "10.252.130.121:27017",
"members" : [
{
"_id" : 0,
"name" : "10.168.218.46:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 79,
"optime" : Timestamp(1429593250, 1),
"optimeDate" : ISODate("2015-04-21T05:14:10Z"),
"lastHeartbeat" : ISODate("2015-04-21T05:20:21Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T05:20:21Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: 10.168.217.186:27017",
"syncingTo" : "10.168.217.186:27017"
},
{
"_id" : 1,
"name" : "10.168.217.186:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5409,
"optime" : Timestamp(1429593250, 1),
"optimeDate" : ISODate("2015-04-21T05:14:10Z"),
"self" : true
},
{
"_id" : 2,
"name" : "10.252.130.121:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 5142,
"optime" : Timestamp(1429593250, 1),
"optimeDate" : ISODate("2015-04-21T05:14:10Z"),
"lastHeartbeat" : ISODate("2015-04-21T05:20:20Z"),
"lastHeartbeatRecv" : ISODate("2015-04-21T05:20:22Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1429592314, 1),
"electionDate" : ISODate("2015-04-21T04:58:34Z")
}
],
"ok" : 1
}
repset:SECONDARY>
至此安装部署完成,解决两个问题
主节点挂掉之后能自动切换过去
主节点压力过大问题解决