高可用mongodb集群的学习记录(二mongodb主从配置)
二mongodb主从配置
在之前我们使用mysql数据库或者redis时大家广泛用到,采用双机备份后主节点挂掉了后从节点可以接替主机继续服务。所以这种模式比单节点的高可用性要好很多。
1、环境准备
实际应用中,需要两台机器一主一从。我这里因资源问题,使用一台机器解决。
192.168.221.161:27021当做master
192.168.221.161:27022当做slave
2、分别建立两个文件夹/data/db_master,/data/db_slave
3、分别配置两个配置文件mongodb_master.conf,mongodb_salve.conf
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@MidApp mongodb] # cat mongodb_master.conf
dbpath= /data/db_master
logpath= /usr/local/mongodb/logs/mongodb_master .log
logappend= true
port=27021
fork= true
auth= false
nohttpinterface= false
bind_ip=192.168.221.161
journal= true
quiet= true
master= true
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@MidApp mongodb] # cat mongodb_salve.conf
dbpath= /data/db_slave
logpath= /usr/local/mongodb/logs/mongodb_slave .log
logappend= true
port=27022
fork= true
auth= false
nohttpinterface= false
bind_ip=192.168.221.161
journal= true
quiet= true
slave= true
source =192.168.221.161:27021
|
4. 启动master节点
1
2
3
4
|
[root@MidApp mongodb] # mongod -f mongodb_master.conf
about to fork child process, waiting until server is ready for connections.
forked process: 55489
child process started successfully, parent exiting
|
Master上输出日志内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
2017-11-14T09:02:39.999-0800 I CONTROL [initandlisten] options: { config: "mongodb_master.conf" , master: true , net: { bindIp: "192.168.221.161" , http: { enabled: true }, port: 27021 }, processManagement: { fork: true }, security: { authorization: "disabled" }, storage: { dbPath: "/data/db_master" , journal: { enabled: true } }, systemLog: { destination: "file" , logAppend: true , path: "/usr/local/mongodb/logs/mongodb_master.log" , quiet: true } }
2017-11-14T09:02:40.014-0800 I INDEX [initandlisten] allocating new ns file /data/db_master/local .ns, filling with zeroes...
2017-11-14T09:02:40.018-0800 I NETWORK [websvr] admin web console waiting for connections on port 28021
2017-11-14T09:02:40.254-0800 I STORAGE [FileAllocator] allocating new datafile /data/db_master/local .0, filling with zeroes...
2017-11-14T09:02:40.254-0800 I STORAGE [FileAllocator] creating directory /data/db_master/_tmp
2017-11-14T09:02:40.263-0800 I STORAGE [FileAllocator] done allocating datafile /data/db_master/local .0, size: 64MB, took 0.001 secs
2017-11-14T09:02:40.318-0800 I REPL [initandlisten] ******
2017-11-14T09:02:40.318-0800 I REPL [initandlisten] creating replication oplog of size: 990MB...
2017-11-14T09:02:40.319-0800 I STORAGE [FileAllocator] allocating new datafile /data/db_master/local .1, filling with zeroes...
2017-11-14T09:02:40.349-0800 I STORAGE [FileAllocator] done allocating datafile /data/db_master/local .1, size: 1024MB, took 0.029 secs
2017-11-14T09:02:40.354-0800 I REPL [initandlisten] ******
2017-11-14T09:02:40.358-0800 I NETWORK [initandlisten] waiting for connections on port 27021
|
5. 启动从节点
1
2
3
4
|
[root@MidApp mongodb] # mongod -f mongodb_salve.conf
about to fork child process, waiting until server is ready for connections.
forked process: 55577
child process started successfully, parent exiting
|
Slave上输出日志如下:
1
2
3
4
5
6
7
8
|
2017-11-14T09:05:10.757-0800 I CONTROL [initandlisten] allocator: tcmalloc
2017-11-14T09:05:10.757-0800 I CONTROL [initandlisten] options: { config: "mongodb_salve.conf" , net: { bindIp: "192.168.221.161" , http: { enabled: true }, port: 27022 }, processManagement: { fork: true }, security: { authorization: "disabled" }, slave: true , source : "192.168.221.161:27021" , storage: { dbPath: "/data/db_slave" , journal: { enabled: true } }, systemLog: { destination: "file" , logAppend: true , path: "/usr/local/mongodb/logs/mongodb_slave.log" , quiet: true } }
2017-11-14T09:05:10.759-0800 I INDEX [initandlisten] allocating new ns file /data/db_slave/local .ns, filling with zeroes...
2017-11-14T09:05:10.763-0800 I NETWORK [websvr] admin web console waiting for connections on port 28022
2017-11-14T09:05:11.039-0800 I STORAGE [FileAllocator] allocating new datafile /data/db_slave/local .0, filling with zeroes...
2017-11-14T09:05:11.039-0800 I STORAGE [FileAllocator] creating directory /data/db_slave/_tmp
2017-11-14T09:05:11.045-0800 I STORAGE [FileAllocator] done allocating datafile /data/db_slave/local .0, size: 64MB, took 0.001 secs
2017-11-14T09:05:11.066-0800 I NETWORK [initandlisten] waiting for connections on port 27022
|
6. 验证一下主从复制
登录master插入数据:
1
2
3
4
5
6
7
8
9
10
11
|
[root@MidApp mongodb] # mongo 192.168.221.161:27021
MongoDB shell version: 3.0.6
connecting to: 192.168.221.161:27021 /test
> show dbs
local 1.078GB
> use test
switched to db test
> db.testdb.insert({ "test1" : "item1" })
WriteResult({ "nInserted" : 1 })
> db.testdb. find ()
{ "_id" : ObjectId( "5a0b23702c969986fca777ed" ), "test1" : "item1" }
|
登录slave节点,验证数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@MidApp mongodb] # mongo 192.168.221.161:27022
MongoDB shell version: 3.0.6
connecting to: 192.168.221.161:27022 /test
> show dbs
2017-11-14T09:07:45.745-0800 E QUERY Error: listDatabases failed:{ "note" : "from execCommand" , "ok" : 0, "errmsg" : "not master" }
at Error (<anonymous>)
at Mongo.getDBs (src /mongo/shell/mongo .js:47:15)
at shellHelper.show (src /mongo/shell/utils .js:630:33)
at shellHelper (src /mongo/shell/utils .js:524:36)
at (shellhelp2):1:1 at src /mongo/shell/mongo .js:47
> db.getMongo().setSlaveOk() #由于slave节点默认是不可读的,需要先执行这句命令才能查询数据
> show dbs
local 0.078GB
test 0.078GB
> use test
> db.testdb. find ()
{ "_id" : ObjectId( "5a0b23702c969986fca777ed" ), "test1" : "item1" }
|
从节点上可以通过命令db.printReplicationInfo()查看服务状态:
1
2
3
4
5
|
> db.printReplicationInfo()
this is a slave, printing slave replication info.
source : 192.168.221.161:27021
syncedTo: Tue Nov 14 2017 09:12:04 GMT-0800 (PST)
7 secs (0 hrs) behind the freshest member (no primary available at the moment)
|
至此,mongodb的主从架构已经完成了。为防止机器负载过大,也可以配置一主多从服务,master节点只负责写操作,slave节点只提供读操作。
不过,存在几个疑问:
a. 从服务器可以当做主服务器吗,也就是从服务器可写吗?
试一下:
1
2
|
> db.testdb.insert({ "test3" : "item3" })
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
|
可以看到,slave节点是不可写的
b.如果master挂掉,从服务器会自动接管主服务,变为可写吗?
把master进程杀掉:
1
|
kill -9 ` ps -ef| grep mongod| grep - v grep | awk '{print $2}' `
|
测试slave节点:
1
2
|
> db.testdb.insert({ "test3" : "item3" })
WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
|
发现,还是不可写!
这种情况只能手动干预了。。。
继续思考一个问题:
我们怎么实现主节点挂了之后能够自动切换?下一篇接着学习mongodb的集群搭建
高可用mongodb集群的学习记录(二mongodb主从配置)相关推荐
- Kubernetes学习-K8S安装篇-Kubeadm安装高可用K8S集群
Kubernetes学习-K8S安装篇-Kubeadm高可用安装K8S集群 1. Kubernetes 高可用安装 1.1 kubeadm高可用安装k8s集群1.23.1 1.1.1 基本环境配置 1 ...
- 二、《云原生 | Kubernetes篇》Kubernetes学习-K8S安装篇-Kubeadm安装高可用K8S集群--生产环境
目录 1. Kubernetes 高可用安装 1.1.1实验环境规划 高可用Kubernetes集群规划
- 【15W字长文】主从复制高可用Redis集群,完整包含Redis所有知识点
往期文章一览 分布式会话与单点登录SSO系统CAS,包含完整示例代码实现 [7W字长文]使用LVS+Keepalived实现Nginx高可用,一文搞懂Nginx 主从复制高可用Redis集群 分布式架 ...
- 高可用 Canal集群( 秒懂 + 史上最全)
文章很长,而且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录 博客园版 为您奉上珍贵的学习资源 : 免费赠送 :<尼恩Java面试宝典> 持续更新+ 史上最全 + 面试必备 2000页+ ...
- 高可用 Elasticsearch 集群 21 讲
课程内容 开篇词 | 如何构建一个高可用.低延迟的 Elasticsearch 集群? 我们从 1.x 开始使用 Elasticsearch ,发展到现在大大小小的集群有 5000+,最大的集群物理主 ...
- zookeeper安装包_构建高可用ZooKeeper集群
ZooKeeper 是 Apache 的一个顶级项目,为分布式应用提供高效.高可用的分布式协调服务,提供了诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知和分布式锁等分布式基础服务.由于 Zo ...
- gitee如何搭建mysql_MySQL高可用架构集群环境搭建手册.md
# MySQL高可用架构集群环境搭建手册 ## 环境准备 ### 机器规划 | 节点 | IP | 配置 | 角色 | | -------- | -------------- | ---- | --- ...
- Redis进阶-高可用:集群
前言 前面几篇文章中,已经介绍了Redis的几种高可用技术:持久化.主从复制和哨兵,但这些方案仍有不足,其中最主要的问题是存储能力受单机限制,以及无法实现写操作的负载均衡. Redis集群解决了上述 ...
- Redis(3)-高可用与集群
Redis 高可用与集群 虽然 Redis 可以实现单机的数据持久化,但无论是 RDB 也好或者 AOF 也好,都解决 不了单点宕机问题,即一旦单台 redis 服务器本身出现系统故障.硬件故障等问题 ...
最新文章
- Hadoop自学笔记(二)HDFS简单介绍
- 漫步者煲耳机软件_它是苹果AirPods的“真香替身”——漫步者lolliPods开箱体验...
- 八年级上册计算机知识点总结,八年级数学上册知识要点归纳
- 产品问答| 如何选择行业?行业经验会变成阻碍吗?
- 自定义字符串函数:strlen,strcat,strcpy,strcmp
- C# 列表中查找大小比较
- PE知识复习之PE的导入表
- python自学教程-3D图示Python标准自学教程入门篇
- 四格漫画《MUXing》——请客记
- SNMP Trap 报文
- 二元分类(Binary Classfication)与logistic回归
- win10装debian 双系统_如何安装win10和linux [ubuntu14]双系统
- hdu 1864 最大报销额
- CS5212替代RTD2166|低BOM成本替代RTD2166
- opencv获取灰度图像的像素值
- 【软件工程】 软件工程及开发模型
- 一个人工作之余6元上线一款APP的血泪史
- 小猿圈python视频_小猿圈分享学习Python的最佳方式
- excel文件保护工作表无法编辑,忘记密码时解决办法
- iOS 真机调试出现”A signed resource has been added modified or deleted“错误的解决办法