mongodb 主从复制

MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。

MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。

官方配置说明:https://docs.mongodb.com/manual/reference/configuration-options/#configuration-file

MongoDB的4.x版本已经不再支持主从复制,而是使用副本集(replica-set)。

环境准备:

ip hostname status
192.168.20.3 node2003 primary
192.168.20.4 node2004 secondary

###node2003:
```
~]# cd /usr/local/pkg/
~]# tar xf mongodb-linux-x86_64-rhel70-4.0.5.tgz
~]# mv mongodb-linux-x86_64-rhel70-4.0.5 mongodb
~]# vim /etc/profile.d/mongodb.sh
export PATH=/usr/loca/pkg/mongodb/bin:$PATH
~]# source /etc/profile.d/mongodb.sh

~]# mkdir {conf.d,data,logs}

~]# vim /usr/local/pkg/mongodb/conf.d/mongodb.conf

systemLog:

destination: file

发送所有日志输出的目标。指定file或syslog,如果指定file,则必须指定systemLog.path

logAppend: false

设置为true时,将新条目添加到现有日志文件的末尾。如果设置为false,将备份现有日志并创建新文件。默认为false

logRotate: rename

renme:重命名旧日志文件,创建新日志文件记录

reopen:重新将日志追加进旧日志,需logAppend设置为true

path: /usr/local/pkg/mongodb/logs/mongod.log

日志文件路径

Where and how to store data.

storage:

dbPath: /usr/local/pkg/mongodb/data

数据库存储路径

journal:

enabled: true

#默认值:64位系统上为true,32位系统上为false。启用持久性日志以确保数据文件保持有效和可恢复。不支持使用内存存储引擎的实例。

how the process runs

processManagement:

fork: true # fork and run in background

后台运行

pidFilePath: /var/run/mongodb/mongod.pid

pid文件路径

timeZoneInfo: /usr/share/zoneinfo

加载时区数据库的完整路径。如果未提供此选项,则mongodb将使用其内置时区数据库

network interfaces

net:

port: 27017

监听TCP端口

bindIp: node2003

绑定主机名或IP地址或完整的Unix域套接字路径。要绑定多个地址,用逗号分隔

ssl.mode: disabled

启用或禁用用于所有网络连接的TLS/SSL或混合TLS/SSL。有如下参数

disable:服务器不使用TLS/SSL

allowSSL:服务器之间的连接不使用TLS/SSL。对于连入的连接,服务器接受TLS/SSL和非TLS/SSL

preferSSL:服务器之间的连接使用TLS/SSL。对于连入的连接,服务器接受TLS/SSL和非TLS/SSL

requireSSL:服务器仅使用和接受TLS/SSL加密连接

replication复制集

replication:

oplogSizeMB: 2048

复制操作日志的最大大小(MB为单位)。

主节点的操作记录成为oplog(operation log),oplog存储在特殊的数据local中。oplog只是作为从节点与主节点保持数据同步的机制

replSetName: mymon

4.x版本使用副本集代替主从复制。副本集中的所有主机必具有相同的集名称。


####启动服务

~]# mongod -f conf.d/mongodb.conf

~]# tail -f logs/mongod.log

2018-12-27T16:28:31.184+0800 I CONTROL [initandlisten]

2018-12-27T16:28:31.184+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2018-12-27T16:28:31.184+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'

2018-12-27T16:28:31.184+0800 I CONTROL [initandlisten]

2018-12-27T16:28:31.185+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2018-12-27T16:28:31.185+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'

2018-12-27T16:28:31.185+0800 I CONTROL [initandlisten]

2018-12-27T16:28:31.196+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/usr/local/pkg/mongodb/data/diagnostic.data'

2018-12-27T16:28:31.197+0800 I NETWORK [initandlisten] waiting for connections on port 27017

2018-12-27T16:28:32.023+0800 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK

有两处警告,需要处理


**禁用hugepage:**
自CentOS6版本开始引入了Transparent Huge Pages(THP),从CentOS7版本开始,该特性默认就会启用。尽管THP的本意是为提升内存的性能,不过某些数据库厂商还是建议直接关闭THP(比如说Oracle、MariaDB、MongoDB等),否则可能会导致性能出现下降。 **临时关闭(重启服务生效):**

echo never >> /sys/kernel/mm/transparent_hugepage/enabled

echo never >> /sys/kernel/mm/transparent_hugepage/defrag


**永久关闭(重启机器生效):**

~]# cat /etc/rc.d/rc.local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then

echo never > /sys/kernel/mm/transparent_hugepage/enabled

fi

if test -f /sys/kernel/mm/transparent_hugepage/defrag; then

echo never > /sys/kernel/mm/transparent_hugepage/defrag

fi

<br /><br />

###node2004:

~]# cd /usr/local/pkg/

~]# tar xf mongodb-linux-x86_64-rhel70-4.0.5.tgz

~]# mv mongodb-linux-x86_64-rhel70-4.0.5 mongodb

~]# vim /etc/profile.d/mongodb.sh

export PATH=/usr/loca/pkg/mongodb/bin:$PATH

~]# source /etc/profile.d/mongodb.sh

~]# vim mongodb/conf.d/mongodb.conf

systemLog:

destination: file

logAppend: false

logRotate: rename

path: /usr/local/pkg/mongodb/logs/mongod.log

Where and how to store data.

storage:

dbPath: /usr/local/pkg/mongodb/data

journal:

enabled: true

how the process runs

processManagement:

fork: true

pidFilePath: /var/run/mongodb/mongod.pid

timeZoneInfo: /usr/share/zoneinfo

network interfaces

net:

port: 27017

bindIp: node2004

node2004机器

ssl.mode: disabled

replication:

oplogSizeMB: 2048

replSetName: mymon

关闭hugepage后启动服务

~]# mongod -f mongodb/conf.d/mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 18275

child process started successfully, parent exiting //启动成功

<br /><br />

####node2003:

~]# mongo node2003:27071

MongoDB shell version v4.0.5

connecting to: mongodb://node2003:27017/test?gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("4d6ec81a-957a-4a43-a0b6-0d80d5aa5536") }

MongoDB server version: 4.0.5

Server has startup warnings:

2018-12-27T17:24:04.568+0800 I CONTROL [initandlisten]

2018-12-27T17:24:04.568+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.

2018-12-27T17:24:04.568+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. //读写不受限制

2018-12-27T17:24:04.568+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. //正常还是别用root帐号起服务

2018-12-27T17:24:04.568+0800 I CONTROL [initandlisten]

Enable MongoDB's free cloud-based monitoring service, which will then receive and display

metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you

and anyone you share the URL with. MongoDB may use this information to make product

improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()

To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

//定义一个配置对象

config={_id:"mymon", members:[{_id:0,host:"node2003:27017",priority:1}]}

{

"_id" : "mymon",

	#副本名称
"members" : [
{
"_id" : 0,
"host" : "node2003:27017",
"priority" : 1
#优先级,数字越小,优先级越大。就会被优先选举成primary节点
}
]

}

//调用rs API初始化set

rs.initiate(config)

{

"ok" : 1,

"operationTime" : Timestamp(1545903526, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1545903526, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

mymon:SECONDARY>

//登陆提示符已经显示出来了,但不是primary节点,应该是节点还没选举完成。重新查看一下本机的配置

mymon:SECONDARY> rs.conf()

{

"_id" : "mymon",

"version" : 1,

"protocolVersion" : NumberLong(1),

"writeConcernMajorityJournalDefault" : true,

"members" : [

{

"_id" : 0,

"host" : "node2003:27017",

"arbiterOnly" : false,

"buildIndexes" : true,

"hidden" : false,

"priority" : 1,

"tags" : {

                    },
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : { },
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5c249da69445df4a35b62059")
}

}

mymon:PRIMARY>

//primary节点已经选举成功了

<br />

然后我们将node2004节点添加进来:

mymon:PRIMARY> rs.add("node2004:27017")

{

"ok" : 1,

"operationTime" : Timestamp(1545903657, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1545903657, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

//查看状态

mymon:PRIMARY> rs.status()

{

"set" : "mymon",

"date" : ISODate("2018-12-27T09:41:12.487Z"),

"myState" : 1,

"term" : NumberLong(1),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"readConcernMajorityOpTime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"appliedOpTime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"durableOpTime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

}

},

"lastStableCheckpointTimestamp" : Timestamp(1545903638, 1),

"members" : [

{

"_id" : 0,

"name" : "node2003:27017",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1579,

"optime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2018-12-27T09:41:08Z"),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"infoMessage" : "",

"electionTime" : Timestamp(1545903526, 2),

"electionDate" : ISODate("2018-12-27T09:38:46Z"),

"configVersion" : 2,

"self" : true,

"lastHeartbeatMessage" : ""

},

{

"_id" : 1,

"name" : "node2004:27017",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 15,

"optime" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1545903668, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2018-12-27T09:41:08Z"),

"optimeDurableDate" : ISODate("2018-12-27T09:41:08Z"),

"lastHeartbeat" : ISODate("2018-12-27T09:41:11.029Z"),

"lastHeartbeatRecv" : ISODate("2018-12-27T09:41:11.583Z"),

"pingMs" : NumberLong(0),

"lastHeartbeatMessage" : "",

"syncingTo" : "node2003:27017",

"syncSourceHost" : "node2003:27017",

"syncSourceId" : 0,

"infoMessage" : "",

"configVersion" : 2

}

],

"ok" : 1,

"operationTime" : Timestamp(1545903668, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1545903668, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

可以看到节点已经添加进mymon副本集里了。

<br />

增加数据测试副本是否成功:

mymon:PRIMARY> use test

switched to db test

mymon:PRIMARY> db.test.insert({name:"liyao", age:25})

WriteResult({ "nInserted" : 1 })

mymon:PRIMARY>


####node2004:

rs.initiate(config)

....

mymon:SECONDARY> use test

switched to db test

mymon:SECONDARY> db.test.find()

Error: error: {

"operationTime" : Timestamp(1545903758, 1),

"ok" : 0,

"errmsg" : "not master and slaveOk=false", //不是master是不允许读写的

"code" : 13435,

"codeName" : "NotMasterNoSlaveOk",

"$clusterTime" : {

"clusterTime" : Timestamp(1545903758, 1),

"signature" : {

"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

"keyId" : NumberLong(0)

}

}

}

mymon:SECONDARY> db.setSlaveOk() //允许读写

mymon:SECONDARY> db.test.find() //查看数据

{ "_id" : ObjectId("5c249e73345ffcc8fc7205cc"), "name" : "liyao", "age" : 25 }

mymon:SECONDARY>

验证成功,数据复制OK

mongodb replica-set的更多相关文章

  1. 关于MongoDb Replica Set的故障转移集群——实战篇

    如果你还不了解Replica Set的相关理论,请猛戳传送门阅读笔者的上一篇博文. 因为Replica Set已经属于MongoDb的进阶应用,下文中关于MongoDb的基础知识笔者就不再赘述了,请参 ...

  2. MongoDb Replica Set中使用的地址

    Unable to connect to a member of the replica set matching the read preference Primary 今天尝试使用MongoDB ...

  3. MongoDB 学习笔记(三) MongoDB (replica set) 集群配置

    MongoDB Replica Sets的结构类似于以集群,完全可以把他当成一个集群,因为他确实与集群实现的作用是一样的:如果其中一个节点出现故障,其他的节点会马上将业务接管过来.而无需停机操作 Mo ...

  4. mongodb replica set 和 nodejs中使用mongoose连接replica

    一.mongodb replication 介绍 官网上的第一句话就是Replication is the process of synchronizing data across multiple ...

  5. MongoDB Replica Set搭建集群

    MongoDB做集群,版本3.2官网推荐的集群方式Replica Set 准备服务器3台 两个standard节点(这两个节点直接可以互切primary secondary). 一个arbiter节点 ...

  6. mongodb replica set 配置高性能多服务器详解

    mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:详解mongodb 主从配置.master-slave模式,不能自动实现故障转移和恢复.所以推荐大家使用mon ...

  7. (1)解锁 MongoDB replica set核心姿势

    副本集Replica Set是一个术语,定义具有多节点的数据库集群,这些节点具有主从复制(master-slave replication) 且节点之间实现了自动故障转移. 这样的结构通常需要具有奇数 ...

  8. MongoDB replica set IDs do not match

    在搭建MongoDB(版本 3.2.9)的Replica Set时,使用 rs.status() 查看Replica Set的状态,发现一个成员异常:replica set IDs do not ma ...

  9. MongoDB Replica Set 选举过程

    什么是选举? 选举是副本集选择某个成员成为primary的过程.primary是一个副本集中唯一能够接收写操作的成员. 下面的事件能够引发一次选举: 第一次初始化一个副本集 Primary失效.rep ...

  10. 转 Warning:MongoDB Replica Sets配置注意事项

    我们知道,MongoDB不提供单机的数据安全性,取而代之的是提供了Replica Sets的高可用方案.官方文档中提到的案例是三个节点组成的Replica Sets,这样在其中任何一个节点宕机后都会自 ...

随机推荐

  1. (原)使用ass字幕文件通过ffmpeg给视频添加字幕的一些研究

    使用ass字幕文件通过ffmpeg给视频添加字幕的一些研究 Author:lihaiping1603@aliyun.com Create:2019-09-04 最近对ffmpeg给视频文件添加字幕效果 ...

  2. IDEA中提示配置jdk1.8

    问题描述:运行Java Web项目时,IDEA中提示:Warning:java: 源值1.5已过时, 将在未来所有发行版中删除 解决方法:1. 打开[File]—[Project Structure] ...

  3. WeQuant教程—1.4 实践教学:比特币量化定投

     在wequant.io,为了让读者能直接体验量化系统的魅力,我们用前面的思路,实现了一套完整的量化系统和回测工具,这个系统非常简单,用户只需要把交易意志用策略表达出来,系统就可以自己完成交易效果的回 ...

  4. 【ARM-Linux开发】【CUDA开发】NVIDIA TEGRA X1:LINUX驱动程序包多媒体用户指南

    NVIDIA TEGRA X1:LINUX驱动程序包多媒体用户指南 转载请注明作者和出处:http://blog.csdn.net/u011475210 嵌入式平台:NVIDIA Jetson TX1 ...

  5. 搜索排序的评价指标NDCG

    refer: https://www.cnblogs.com/by-dream/p/9403984.html Out1 = SELECT QueryId, DocId, Rating, ROW_NUM ...

  6. Kubernetes集群

    Kubernetes已经成为当下最火热的一门技术,未来一定也会有更好的发展,围绕着云原生的周边产物也越来越多,使得上云更加便利更加有意义,本文主要讲解一些蔚来汽车从传统应用落地到Kubernetes集 ...

  7. mysql 更新与查询(排序 分组 链接查询)

    UPDATE更新 #每一次数据的更新都需要update UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法: UPDATE table_name SET field1=new-valu ...

  8. 理解 Node.js 的 GC 机制

    <深入浅出Node.js>第五章<内存控制>阅读笔记 随着 Node 的发展,JavaScript 的应用场景早已不再局限在浏览器中.本文不讨论网页应用.命令行工具等短时间执行 ...

  9. zuul重连配置

    #retry #该参数用来开启重试机制 spring.cloud.loadbalancer.retry.enabled=true #断路器的超时时间,断路器的超时时间需要大于ribbon的超时时间,不 ...

  10. kafka java项目测试使用

    引入依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.11 ...