Mongodb 3.6 副本集测试及添加删除节点等操作

下载tar包并安装
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.8.tgz

[root@mysqlt1 soft]# tar -zxvf mongodb-linux-x86_64-rhel62-3.6.3.tgz -C /usr/local/
[root@mysqlt1 soft]# cd /usr/local/
[root@mysqlt1 local]# ln -s /usr/local/mongodb-linux-x86_64-rhel62-3.6.3 mongodb
export PATH=/usr/local/mongodb/bin:$PATH
[root@mysqlt1 local]# export PATH=/usr/local/mongodb/bin:$PATH
[root@mysqlt1 local]# groupadd mongodb
[root@mysqlt1 local]# useradd -g mongodb mongodb
[root@mysqlt1 local]# chown -R mongodb:mongodb /usr/local/mongodb/
[root@mysqlt1 local]# mkdir -p /data/mongodb
[root@mysqlt1 local]# chown -R mongodb:mongodb /data/mongodb/
[root@mysqlt1 ~]# ll /usr/local/mongodb/bin/

[root@hongquan1 data]# mkdir data conf log
[root@hongquan1 data]# mkdir 28001 28002 28003
[root@hongquan1 data]# chown -R mongodb:mongodb data/ conf/ log/ 2800*
1 配置文件
port=28001
bind_ip=192.168.20.118
logpath=/data/log/28001.log
dbpath=/data/28001/
logappend=true
pidfilepath=/data/28001/28001.pid
fork=true
oplogSize=1024
replSet=MyMongo

2 启动实例
/usr/local/mongodb/bin/mongod -f /data/conf/28001.conf
[mongodb@hongquan1 bin]$ ps -ef|grep mongodb
/usr/local/mongodb/bin/mongod -f /data/conf/28002.conf
/usr/local/mongodb/bin/mongod -f /data/conf/28003.conf

登录
/usr/local/mongodb/bin/mongo 192.168.20.118:28001/admin
> db
admin
> use admin
switched to db admin
>config = {
"_id":"MyMongo",
members:[
{"_id":0,host:"192.168.20.118:28001"},
{"_id":1,host:"192.168.20.118:28002"},
{"_id":2,host:"192.168.20.118:28003"}]
}
> config.members
> config.members[2] ={"_id":2,"host":"192.168.20.118:28003",arbiterOnly:true}
> rs.initiate(config)
> rs.status();
MyMongo:PRIMARY> db.isMaster()

/usr/local/mongodb/bin/mongo 192.168.20.118:28002/admin
MyMongo:SECONDARY> show dbs
MyMongo:SECONDARY> rs.slaveOk(true)
/usr/local/mongodb/bin/mongo 192.168.20.118:28003/admin
MyMongo:ARBITER> rs.slaveOk(true)
MyMongo:ARBITER> show dbs
local 0.000GB
--
MyMongo:PRIMARY> show users;
db.createUser(
{
user: "dba",
pwd: "dba",
roles: [ { role: "root", db: "admin" } ]
}
)
MyMongo:PRIMARY>db.auth('dba','dba')
MyMongo:SECONDARY> show users;
{
"_id" : "admin.dba",
"user" : "dba",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
MyMongo:ARBITER> show users
--
MyMongo:PRIMARY> use test;
switched to db test
MyMongo:PRIMARY> for(var i=1;i<=1000;i++) db.table1.save({id:i,"test1":"testval1"});
WriteResult({ "nInserted" : 1 })
MyMongo:PRIMARY> db.table1.find().count()
1000
MyMongo:PRIMARY> show collections
table1

MyMongo:SECONDARY> use test;
switched to db test
MyMongo:SECONDARY> db.table1.find().count()
1000
--
MyMongo:PRIMARY> show collections
system.keys
system.users
system.version
MyMongo:PRIMARY> db.system.users.find().pretty()
{
"_id" : "admin.dba",
"user" : "dba",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "ZbsCePqGIpsp492RvVB3cg==",
"storedKey" : "GczfCAfP0yNUUrMOjVatzZnWisw=",
"serverKey" : "i0CDYveRNXcQ8F3HFthQGSyqbFM="
}
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
MyMongo:PRIMARY> db.system.indexes.find();
MyMongo:PRIMARY> db.system.version.find();
{ "_id" : "featureCompatibilityVersion", "version" : "3.6" }
{ "_id" : "authSchema", "currentVersion" : 5 }

==3个节点,添加2个节点
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28004.conf
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28005.conf
此时在primary上插入数据
MyMongo:PRIMARY> db.books.find()
MyMongo:SECONDARY> db.books.find() //28002上查看是否存在
MyMongo:PRIMARY> rs.add( { host: "10.15.7.114:28004"} )
登录28004节点并查看
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 10.15.7.114:28004/admin
MyMongo:SECONDARY> db.getMongo().setSlaveOk()
MyMongo:SECONDARY> db.books.find()//数据已经存在
再添加一个仲裁节点28005
MyMongo:PRIMARY> rs.add( { host: "10.15.7.114:28005", arbiterOnly: true } )
MyMongo:PRIMARY> rs.status()
MyMongo:PRIMARY> db.isMaster()
{
"hosts" : [
"10.15.7.114:28001",
"10.15.7.114:28002",
"10.15.7.114:28004"
],
"arbiters" : [
"10.15.7.114:28003",
"10.15.7.114:28005"
],
现在5个节点,1个primary,2个secondary,2个arbiterOnly
现在测试宕机掉28001primary节点
MyMongo:PRIMARY> use admin
MyMongo:PRIMARY> db.shutdownServer();
//"errmsg" : "shutdown must run from localhost when running db without auth"
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo localhost:28001/admin
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:28001/admin
[root@mysqlt1 ~]# cat /etc/hosts
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo mysqlt1:28001/admin
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28001.conf
关闭28001后,28002成为primary节点
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 10.15.7.114:28002/admin
MyMongo:PRIMARY> rs.status()
MyMongo:PRIMARY> use test;//加入测试数据
switched to db test
MyMongo:PRIMARY> for(var i=1;i<=1000;i++) db.table1.save({id:i,"test1":"testval1"});
WriteResult({ "nInserted" : 1 })
MyMongo:PRIMARY> db.table1.find().count()
1000
现在宕机掉28002节点,28003成为primary节点,现在剩下1个primary和2个仲裁节点
MyMongo:PRIMARY> use test;
switched to db test
MyMongo:PRIMARY> db.table1.find().count()
1000
MyMongo:PRIMARY> for(var i=1;i<=1000;i++) db.table1.save({id:i,"test22":"testval22"}); //插入数据
WriteResult({ "nInserted" : 1 })
MyMongo:PRIMARY> db.table1.count()
2000
现在启动28001,28002节点,自动加入副本集,并成为secondry节点
MyMongo:SECONDARY> db.getMongo().setSlaveOk()
MyMongo:SECONDARY> db.table1.find().count() //数据已经同步
2000
这里测试,5个节点,剩余3个节点,不管是secondry还是仲裁节点,即使最后剩下primary+2个仲裁,primary节点还是可以支持读写

==添加备份节点
MyMongo:PRIMARY> rs.remove("10.15.7.114:28005"); #删除原来的第二个仲裁节点
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true}) #设置为备份节点
"_id" : 4,
"name" : "10.15.7.114:28005",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
MyMongo:STARTUP2> rs.slaveOk()
MyMongo:STARTUP2> db.table1.find().count() #不能进行读写操作
2018-10-15T01:42:38.593+0800 E QUERY [thread1] Error: count failed: {
"ok" : 0,
"errmsg" : "node is not in primary or recovering state",
"code" : 13436,
"codeName" : "NotMasterOrSecondary"
} :

添加延迟节点
Delayed(成员用于支持专用功能):可以指定一个时间延迟从primary节点同步数据。主要用于处理误删除数据马上同步到从节点导致的不一致问题
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true,"slaveDelay":60})

Secondary-Only:不能成为primary节点,只能作为secondary副本节点,防止一些性能不高的节点成为主节点。
Non-Voting:没有选举权的secondary节点,纯粹的备份数据节点

连续写入数据,测试选举
== 选举期间会丢失数据(严格意义上不叫丢失,因为这个时候本来mongo就不可用,在等待切换,程序可以把这部分数据存入消息队列,等待选举成功在写入)
======
import time
from pymongo import MongoClient
conn= MongoClient('mongodb://10.15.7.114:28001,10.15.7.114:28002,10.15.7.114:28004/?replicaSet=MyMongo')

for i in xrange(100):
try:
conn.test.tt1.insert({"name":"test" + str(i)})
time.sleep(1)
print conn.primary
print conn.secondaries
except:
pass
======
测试5个节点,宕机掉3个,然后剩余2个节点,将一个可用节点以单节点启动并使用,使用一段时间后,在以副本集启动,让其他4个节点加入副本集
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28004.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28001.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28003.conf
MyMongo:SECONDARY> #还剩下2个节点,自动变为secondry
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28002.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28005.conf
整个副本集全部宕机,把28002作为单节点启动并使用
[root@mysqlt1 conf]# vim 28002.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 10.15.7.114:28002/admin
> show dbs;
admin 0.000GB
bookstore 0.000GB
config 0.000GB
local 0.002GB
test 0.000GB
> rs.status()
{
"ok" : 0,
"errmsg" : "not running with --replSet",
"code" : 76,
"codeName" : "NoReplicationEnabled"
}
> use test;
switched to db test
> db.tt2.find().count()
10
再插入10条数据
> db.tt2.find().count()
20
[root@mysqlt1 conf]# vim 28002.conf#这里注意,不能修改之前的副本集的名称,修改了会报错
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 10.15.7.114:28002/admin
MyMongo:OTHER> rs.add( { host: "10.15.7.114:28001"} )
{
"ok" : 0,
"errmsg" : "replSetReconfig should only be run on PRIMARY, but my state is REMOVED; use the \"force\" argument to override",
"code" : 10107,
"codeName" : "NotMaster",
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28003.conf

config = {
"_id":"MyMongo1",
members:[
{"_id":0,host:"10.15.7.114:28001"},
{"_id":1,host:"10.15.7.114:28002"},
{"_id":2,host:"10.15.7.114:28003"}]
}
config.members
config.members[1].priority = 2;
rs.reconfig(config);
#MyMongo:OTHER> rs.initiate(config);
==不修改副本集名称启动3个节点,自动加入副本集
MyMongo:PRIMARY> use test;
switched to db test
MyMongo:PRIMARY> db.tt2.find().count()
20
==登录其他secondry节点查看数据,只有副本集之前的数据,作为单节点的数据没有同步,应为写入oplog的问题?
==修正,这里作为单节点启动,要显示指定oplog参数(根本没有oplog这个参数)所以,意思测试不成立
分析,因为oplog的存在的时间和空间都有限制,是循环写,所有如果点节点的存在,就不能保证所有的写入都会在oplog

MyMongo:SECONDARY> db.getMongo().setSlaveOk()
MyMongo:SECONDARY> db.tt2.find().count()
10
MyMongo:SECONDARY> db.tt2.find().sort({"name":1})

[root@mysqlt1 conf]# cat 28002.conf
port=28002
bind_ip=10.15.7.114
logpath=/data/mongodb/log/28002.log
dbpath=/data/mongodb/data/28002/
logappend=true
pidfilepath=/data/mongodb/28002.pid
fork=true
oplogSize=1024
replSet=MyMongo

MyMongo:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1024MB #oplog文件大小
log length start to end: 430037secs (119.45hrs)
oplog first event time: Thu Oct 11 2018 03:45:19 GMT+0800 (CST)
oplog last event time: Tue Oct 16 2018 03:12:36 GMT+0800 (CST)
now: Tue Oct 16 2018 03:12:40 GMT+0800 (CST)
MyMongo:PRIMARY> db.getReplicationInfo() #用来查看oplog的状态、大小、存储的时间范围
{
"logSizeMB" : 1024,
"usedMB" : 4.97,
"timeDiff" : 430086,
"timeDiffHours" : 119.47,
===在secondry节点,重新配置副本集,加入几个仲裁节点
MyMongo:SECONDARY> use admin
MyMongo:SECONDARY> config = {
"_id":"MyMongo",
members:[
{"_id":0,host:"10.15.7.114:28001"},
{"_id":1,host:"10.15.7.114:28002"},
{"_id":2,host:"10.15.7.114:28003", arbiterOnly: true},
{"_id":3,host:"10.15.7.114:28004"},
{"_id":4,host:"10.15.7.114:28005", arbiterOnly: true},
{"_id":5,host:"10.15.7.114:28006", arbiterOnly: true},
{"_id":6,host:"10.15.7.114:28007", arbiterOnly: true}]
}
MyMongo:SECONDARY> rs.reconfig(config,{force:true});
测试插入数据
MyMongo:PRIMARY> db.tt3.find().count()
100
启动另外3个节点
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28003.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo 10.15.7.114:28002/admin
MyMongo:SECONDARY> rs.slaveOk(true)
MyMongo:SECONDARY> use test;
switched to db test
MyMongo:SECONDARY> db.tt3.find().count() #数据同步成功
100
之前的5个节点,现在变成了7个节点,删除新加的2个仲裁节点
rs.remove("10.15.7.114:28007");
rs.remove("10.15.7.114:28006");
MyMongo:PRIMARY> db.isMaster() #变回之前的5个节点,1主,2secondry,2仲裁
{
"hosts" : [
"10.15.7.114:28001",
"10.15.7.114:28002",
"10.15.7.114:28004"
],
"arbiters" : [
"10.15.7.114:28003",
"10.15.7.114:28005"

posted @ 2018-11-22 15:12  春困秋乏夏打盹  阅读(5400)  评论(0编辑  收藏  举报