下载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"

Mongodb 3.6 副本集测试及添加删除节点等操作的更多相关文章

  1. Mongodb主从复制 及 副本集+分片集群梳理

    转载努力哥原文,原文连接https://www.cnblogs.com/nulige/p/7613721.html 介绍了Mongodb的安装使用,在 MongoDB 中,有两种数据冗余方式,一种 是 ...

  2. 【六】MongoDB管理之副本集

    一.复制介绍 所谓的复制就是在多个主机之间同步数据的过程. 1.数据冗余及可用性 复制技术提供数据冗余及可用性,在不同的数据库服务器上使用多个数据副本,复制技术防止单个数据库服务器出现数据故障而出现数 ...

  3. MongoDB 复制(副本集)学习

    MongoDB 复制(副本集)学习 replication set复制集,复制集,多台服务器维护相同的数据副本,提高服务器的可用性.MongoDB复制是将数据同步在多个服务器的过程.复制提供了数据的冗 ...

  4. MongoDB添加删除节点

    副本集添加删除节点 sharding添加删除节点 先将节点设置为hidden,再remove

  5. mongodb replica set 添加/删除节点方法--http://www.ii123.com/jc/bc/bczh/258948.html

    replica set多服务器主从,添加,删除节点,肯定会经常遇到的.下面详细说明一下,添加,删除节点的2种方法. 一,利用rs.reconfig,来添加,删除节点 1,添加节点  代码如下   re ...

  6. redis cluster 添加/删除节点操作

    RedisCluster 添加/删除节点 添加节点新配置两个测试节点8008和9009 [root@--- ~]# /usr/local/redis-/bin/redis-server /u02/re ...

  7. C#通过Ado.net对连接数据库并进行添加删除等常规操作的代码

    如下资料是关于C#通过Ado.net对连接数据库并进行添加删除等常规操作的内容. static string sqlcon = "server=.;database=;Integrated ...

  8. centos的用户、组权限、添加删除用户等操作的详细操作命令

    1.Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还 有组账户所谓组账户就是用户账户的集合,centos组中有两种类型, ...

  9. java程序连接MongoDB副本集测试

    三个节点有一个节点挂掉也不会影响应用程序客户端对整个副本集的读写! public class TestMongoDBReplSet { public static void main(String[] ...

随机推荐

  1. Eclipse Task的使用

    参考链接:http://blog.csdn.net/limb99/article/details/8881891; http://hi.baidu.com/jinxv1987/item/64496f6 ...

  2. javax.servlet.jsp.JspException cannot be resolved to a type 和 javax.servlet.jsp.PageContext cannot be resolved to a type 解决办法

    今天我从码云上拉一个项目下来,是个maven项目,闲来无事自己研究研究,发现刚拉下来,项目就有报错,我一看是httpServletRequest cannot be resolved to a typ ...

  3. ZooKeeper-安装和运行

    ZooKeeper安装和运行 1. 下载安装包 zookeeper-3.4.9.tar.gz 2. 解压 tar -zxvf zookeeper-3.4.9.tar.gz ZooKeeper提供了几个 ...

  4. HDFS存档

    Hadoop存档 每个文件均按块方式存储,每个块的元数据存储在namenode的内存中,因此Hadoop存储小文件会非常低效.因为大量的小文件会耗尽namenode中的大部分内存.存储小文件所需的磁盘 ...

  5. shell学习之常用技巧

    cut 切割,-d分隔符,-f指定段号,-c指定第几个字符.d,f用的多些. sort排序,-n按照数字排序,-r反排序. wc -l 查看文件行数. 大道至简,掌握常用的命令就可以了. uniq去重 ...

  6. Thinkpad E440个性化设置:如何/禁用关闭触摸板?

    #如何禁用/关闭触摸版 默认情况下,ThinkPad E440是不支持触摸板的关闭功能,如果要关闭的话,需要去官方下载相应的鼠标驱动 UltraNav. 下载地址:http://think.lenov ...

  7. 5.3 Razor语法基础

    以往开发ASP.NET Web Form时,在ASPX页面上都会出现许多夹杂C#/VB.NET与HTML的情况,而先前使用<%...%>这种传统圆角括号的表示法会让HTML标签与ASP.N ...

  8. 解决:Python爬取https站点时SNIMissingWarning和InsecurePlatformWarning

    今天想利用Requests库爬取糗事百科站点,写了一个请求,却报错了: 后来参考kinsomy的博客,在cmd中pip install pyopenssl ndg-httpsclient pyasn1 ...

  9. Linux脚本基础

    1.文本编辑 命令模式快捷键命令 命令 作用 dd 删除(剪切)光标所在整行. 5dd 删除(剪切)从光标处开始的5行. yy 复制光标所在整行. 5yy 复制从光标处开始的5行. n 显示搜索命令定 ...

  10. 【VS2013生成DirectX Tutorials时遇到的错误】无法解析的外部符号 _D3D10CreateDeviceAndSwapChain@32

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/3696472.html 今天尝试编译DirectX10中的一个Turorials时, ...