标签:初始化 mongodb复制 alt show base comm 行数据 query process
MongoDB 复制集MongoDB复制是将数据同步在多个服务器的过程。
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
mongodb的复制至少需要两个节点。其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据。
mongodb各个节点常见的搭配方式为:一主一从、一主多从。
主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
以上结构图中,客户端从主节点读取数据,在客户端写入数据到主节点时, 主节点与从节点进行数据交互保障数据的一致性。
tar -zxvf mongodb-linux-x86_64-3.2.1.tgz -C /usr/local/
cd /usr/local/
mv mongodb-linux-x86_64-3.2.1/ mongodb #重命名
mkdir -p /data/mongodb/mongodb{1,2,3,4} #创建数据目录
mkdir -p /data/logs
touch /data/logs/mongodb{1,2,3,4}.log #创建日志文件
cd /data/logs/
chmod 777 *.log #赋予权限
cd /usr/local/mongodb/bin
vim mongodb1.conf
port=27017
dbpath=/data/mongodb/mongodb1
logpath=/data/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
replSet=test #复制集名称
------------------------------------------------------------------------------------
#下面是yum安装时修改、添加的
replication: #去注释
replSetName: test #添加复制集名称
ln -s /usr/local/mongodb/bin/mongod /usr/bin/ #把mongodb常用的命令添加到系统命令中
ln -s /usr/local/mongodb/bin/mongo /usr/bin/
[root@localhost bin]# mongo
> cfg={"_id":"test","members":[{"_id":0,"host":"192.168.217.129:27017"},{"_id":1,"host":"192.168.217.129:27018"},{"_id":2,"host":"192.168.217.129:27019"}]}
#配置复制集 ,注意复制集的名称要一致
{
"_id" : "test",
"members" : [
{
"_id" : 0,
"host" : "192.168.217.129:27017"
},
{
"_id" : 1,
"host" : "192.168.217.129:27018"
},
{
"_id" : 2,
"host" : "192.168.217.129:27019"
}
]
}
> rs.initiate(cfg) #初始化配置时保证从节点没有数据
{ "ok" : 1 }
test:PRIMARY> rs.status() #查看复制集的完整状态
{
"set" : "test",
.......
{
"_id" : 0,
"name" : "192.168.217.129:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", #27017 端口作为主节点
"uptime" : 1234,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
......
},
{
"_id" : 1,
"name" : "192.168.217.129:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
......
},
{
"_id" : 2,
"name" : "192.168.217.129:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #从节点
"uptime" : 49,
"optime" : {
"ts" : Timestamp(1531961046, 1),
"t" : NumberLong(1)
},
.......
}
],
"ok" : 1
}
test:PRIMARY> rs.add("192.168.217.129:27020") #添加节点
{ "ok" : 1 }
test:PRIMARY> rs.status()
........
"_id" : 3,
"name" : "192.168.217.129:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
........
test:PRIMARY> rs.remove("192.168.217.129:27020") #删除节点
{ "ok" : 1 }
test:PRIMARY> rs.status()
.........
[root@localhost bin]# mongod -f mongodb1.conf --shutdown #关闭主节点端口
killing process with pid: 3552
[root@localhost bin]# mongo --port 27018
test:SECONDARY> rs.status()
{
"set" : "test",
.......
{
"_id" : 0,
"name" : "192.168.217.129:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
.......
{
"_id" : 1,
"name" : "192.168.217.129:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1811,
.......
{
"_id" : 2,
"name" : "192.168.217.129:27019", #自动切换主节点
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 712,
......
}
],
"ok" : 1
}
[root@localhost bin]# mongo --port 27019
test:PRIMARY> rs.freeze(30) #暂停30s不参与选举
{ "ok" : 1 }
test:PRIMARY> rs.stepDown(60,30)
#交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步
test:SECONDARY> rs.status()
......
test:SECONDARY> show dbs #
2018-07-19T09:04:34.898+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1
shellHelper.show@src/mongo/shell/utils.js:700:19
shellHelper@src/mongo/shell/utils.js:594:15
@(shellhelp2):1:1
test:SECONDARY> rs.slaveOk() #允许默认从节点读取数据
test:SECONDARY> show dbs
local 1.078GB
test:PRIMARY> use local
switched to db local
test:PRIMARY> rs.printReplicationInfo() #查看日志文件能够使用的大小 默认oplog大小会占用64位实例5%的可用磁盘空间
configured oplog size: 95.37109375MB
log length start to end: 1103secs (0.31hrs)
oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)
oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)
now: Thu Jul 19 2018 09:20:08 GMT+0800 (CST)
test:PRIMARY> db.runCommand({"convertToCapped":"oplog.rs","size":10000000000}) #修改 单位:B
{ "ok" : 1 }
test:PRIMARY> rs.printReplicationInfo()
configured oplog size: 9536.746032714844MB
log length start to end: 1103secs (0.31hrs)
oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)
oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)
now: Thu Jul 19 2018 09:20:24 GMT+0800 (CST)
test:PRIMARY>
标签:初始化 mongodb复制 alt show base comm 行数据 query process
原文地址:http://blog.51cto.com/13640803/2147031