Mongodb 3.6 副本集測試及新增刪除節點等操作
下載tar包並安裝
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.8.tgz
[[email protected] soft]# tar -zxvf mongodb-linux-x86_64-rhel62-3.6.3.tgz -C /usr/local/
[[email protected] soft]# cd /usr/local/
[[email protected] local]# ln -s /usr/local/mongodb-linux-x86_64-rhel62-3.6.3 mongodb
export PATH=/usr/local/mongodb/bin:$PATH
[
[[email protected] local]# groupadd mongodb
[[email protected] local]# useradd -g mongodb mongodb
[[email protected] local]# chown -R mongodb:mongodb /usr/local/mongodb/
[[email protected] local]# mkdir -p /data/mongodb
[[email protected]
[[email protected] ~]# ll /usr/local/mongodb/bin/
[[email protected] data]# mkdir data conf log
[[email protected] data]# mkdir 28001 28002 28003
[[email protected] 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
[[email protected] 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個節點
[[email protected] data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28004.conf
[[email protected] 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節點並檢視
[[email protected] ~]# /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"
[[email protected] ~]# /usr/local/mongodb/bin/mongo localhost:28001/admin
[[email protected] ~]# /usr/local/mongodb/bin/mongo 127.0.0.1:28001/admin
[[email protected] ~]# cat /etc/hosts
[[email protected] ~]# /usr/local/mongodb/bin/mongo mysqlt1:28001/admin
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28001.conf
關閉28001後,28002成為primary節點
[[email protected] ~]# /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個節點加入副本集
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28004.conf
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28001.conf
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28003.conf
MyMongo:SECONDARY> #還剩下2個節點,自動變為secondry
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28002.conf
[[email protected] ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28005.conf
整個副本集全部宕機,把28002作為單節點啟動並使用
[[email protected] conf]# vim 28002.conf
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[[email protected] ~]# /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
[[email protected] conf]# vim 28002.conf#這裡注意,不能修改之前的副本集的名稱,修改了會報錯
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[[email protected] ~]# /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",
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[[email protected] 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})
[[email protected] 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個節點
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[[email protected] conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28003.conf
[[email protected] ~]# /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"