首页 > 数据库 >docker部署mongodb副本集

docker部署mongodb副本集

时间:2023-06-30 12:02:04浏览次数:44  
标签:副本 mongo mongodb 27017 ff50 ecs docker root SECONDARY

1.规格

ip地址

端口

192.168.1.101

27017

192.168.1.102

27017

192.168.1.103

27017


2.部署操作

2.1 以下操作分别在三台不同的ECS服务器上操作

[root@ecs-ff50-73e8-0002 ~]# mkdir /path/to -p

[root@ecs-ff50-73e8-0002 ~]# cd /path/to/

[root@ecs-ff50-73e8-0002 to]# ls

[root@ecs-ff50-73e8-0002 to]# mkdir  logs  data  config

[root@ecs-ff50-73e8-0002 to]# cd ..

[root@ecs-ff50-73e8-0002 path]# chown -R 999 to

[root@ecs-ff50-73e8-0002 path]# cd to/

[root@ecs-ff50-73e8-0002 to]# ll

total 12

drwxr-xr-x 2 polkitd root 4096 Jun 22 09:59 config

drwxr-xr-x 4 polkitd root 4096 Jun 22 10:10 data

drwxr-xr-x 2 polkitd root 4096 Jun 22 09:59 logs


2.2 创建docker网络:

docker network create mongo-cluster


docker run -d --name mongo \

-p 27017:27017 \

-v /path/to/data:/data/db \

-v /path/to/config:/data/configdb \

-v /path/to/logs:/var/log/mongodb \

--network mongo-cluster \

mongo:4.0 \

mongod --replSet rs0 --bind_ip_all --logpath /var/log/mongodb/mongod.log


等待三台ECS的容器的状态都是up,接着往下操作


[root@ecs-ff50-73e8-0001 to]# docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                           NAMES

c74428d6f17e   mongo:latest   "docker-entrypoint.s…"   24 minutes ago   Up 24 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongo


#进入容器的命令 docker exec -it 容器id  /bin/bash

Docker exec -it c74428d6f17e  /bin/bash

[root@ecs-ff50-73e8-0001 path]# docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                           NAMES

64e6de21a8bf   mongo:latest   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongo

[root@ecs-ff50-73e8-0001 path]# docker exec -it  64e6de21a8bf /bin/bash

root@64e6de21a8bf:/# mongo

MongoDB shell version v5.0.5

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("0f6aeeba-af39-4010-a551-c9f1e52e791f") }

MongoDB server version: 5.0.5

==========================省悦,附带截图如下


执行如下操作

rs0:SECONDARY>  use admin

switched to db admin

rs0:SECONDARY> config = { _id:"repset", members:[{_id:0,host:"192.168.1.101:27017"},{_id:1,host:"192.168.1.100:27017"},{_id:2,host:"192.169.0.102:27017"}]}

返回信息如下:


初始化副本集配置,执行命令如下:

rs0:PRIMARY> rs.initiate(config);

返回信息如下:



查看集群节点的状态,执行如下命令

repset:SECONDARY> rs.status();

返回信息如下:




如上信息表明:

副本集配置成功后,192.168.101为主节点PRIMARY,192.168.1.100/102为副本节点SECONDARY。

health:1   1表明状态是正常,0表明异常

state:1     值小的是primary节点、值大的是secondary节点



3.测试Mongodb副本集数据复制功能

在第一台服务器上写入数据:


rs0:PRIMARY>  use test;    #切换到test库

switched to db test

rs0:PRIMARY> db.testdb.insert({"test1":"testval1"})   写入数据

WriteResult({ "nInserted" : 1 })


在第二台服务器上进行查看数据:

[root@ecs-ff50 ~]# docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                           NAMES

b0a0b2f823f8   mongo:latest   "docker-entrypoint.s…"   28 minutes ago   Up 28 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongo

[root@ecs-ff50 ~]# docker exec -it b0a0b2f823f8 /bin/bash

root@b0a0b2f823f8:/# mongo

MongoDB shell version v5.0.5

connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("a77cd5e2-fc1e-4aa5-932a-451ecdc93ce5") }

MongoDB server version: 5.0.5

执行如下操作。发现报错


rs0:SECONDARY> use  test  

switched to db test

rs0:SECONDARY>  show tables;

uncaught exception: Error: listCollections failed: {

        "topologyVersion" : {

                "processId" : ObjectId("6493b000675804e2db3d22b9"),

                "counter" : NumberLong(4)

        },

        "ok" : 0,

        "errmsg" : "not master and slaveOk=false",

        "code" : 13435,

        "codeName" : "NotPrimaryNoSecondaryOk",

        "$clusterTime" : {

                "clusterTime" : Timestamp(1687402264, 1),

                "signature" : {

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

                        "keyId" : NumberLong(0)

                }

        },

        "operationTime" : Timestamp(1687402264, 1)

} :

_getErrorWithCode@src/mongo/shell/utils.js:25:13

DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:718:15

DB.prototype.getCollectionInfos@src/mongo/shell/db.js:766:16

shellHelper.show@src/mongo/shell/utils.js:943:9

shellHelper@src/mongo/shell/utils.js:838:15

@(shellhelp2):1:1

这是因为mongodb默认是从主节点读写数据的,副本节点上不允许读,需要设置副本节点可以读

rs0:SECONDARY> db.getMongo().setSlaveOk();

WARNING: setSlaveOk() is deprecated and may be removed in the next major release. Please use setSecondaryOk() instead.

rs0:SECONDARY>

rs0:SECONDARY>

rs0:SECONDARY>  db.testdb.find();

{ "_id" : ObjectId("6493b5858e3154da611dc177"), "test1" : "testval1" }

rs0:SECONDARY>

rs0:SECONDARY>

rs0:SECONDARY>  show tables;

testdb

rs0:SECONDARY>

rs0:SECONDARY>

发现数据已经同步过来了


4.测试副本集故障切换功能

停掉第一台服务器执行如下操作

[root@ecs-ff50-73e8-0001 path]# docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                           NAMES

64e6de21a8bf   mongo:latest   "docker-entrypoint.s…"   38 minutes ago   Up 38 minutes   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   mongo

[root@ecs-ff50-73e8-0001 path]# docker stop  64e6de21a8bf

64e6de21a8bf



在其他节点,查看副本集的状态

repset:PRIMARY> rs.status();


返回信息如下:






从节点的SECONDARY  切换成PRIMARY表面,故障切换成功。

标签:副本,mongo,mongodb,27017,ff50,ecs,docker,root,SECONDARY
From: https://blog.51cto.com/u_14529165/6589561

相关文章

  • Docker部署redis集群
    1.规划ip地址端口数据存储位置192.168.103.587000/var/lib/redis/70007003/var/lib/redis/7003192.168.103.417001/var/lib/redis/70017004/var/lib/redis/7004192.168.103.1247002/var/lib/redis/70027005/var/lib/redis/70052.创建数据存储目录,及配置文件,导入镜像文件2.1创建好用......
  • Dockerfile构建镜像
    https://blog.csdn.net/qq_69278945/article/details/127461465先编写镜像构建文件DockerfilevimDockerfileFROMnginx:1.20RUNecho'开心活泼的果冻'>/usr/share/nginx/html/index.html构建命令dockerbuild-tmy_nginx.build:构建命令-t:指定镜像名my_nginx:镜......
  • Docker-环境安装
    安装yum-utils:yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2  为yum源添加docker仓库位置: yum-config-manager--add-repohttps://download.docker.com/linux/centos/docker-ce.repo   安装docker: yuminstalldocker-ce......
  • mongodb数据库操作备忘
    db.SMU_REALDATA_PARAMETERS.update({},{$rename:{"interfaceID":"INTERFACEID"}},false,true); 修改field,字段名,不是修改值哟。 网上搜索mongodb语言和CheatSheet手册。https://www.mongodb.com/developer/products/mongodb/cheat-sheet/navcate工具和下面工具都可以。 ......
  • docker
    https://blog.csdn.net/weixin_42249893/article/details/122598512?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-5-122598512-blog-108204378.235^v38^pc_relevant_sort_base1&spm=1001.2101.3001.4242.4&utm_rel......
  • gitlab-docker部署最终版
    下载gitlab镜像dockerpullgitlab/gitlab-ce:latest 运行mkdirgitlabgitlab/etcgitlab/loggitlab/optdockerrun-id-p3000:80-p9922:22-v/root/gitlab/etc:/etc/gitlab-v/root/gitlab/log:/var/log/gitlab-v/root/gitlab/opt:/var/opt/gitlab--restart......
  • MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(8)-Ant Design Blazor前端
    前言前面的章节我们介绍了一些值得推荐的BlazorUI组件库,通过该篇文章的组件库介绍最终我选用AntDesignBlazor这个UI框架作为ToDoList系统的前端框架。因为在之前的工作中有使用过AntDesignVue、AntDesignAngular习惯并且喜欢AntDesign设计规范和风格,废话不多说今天我......
  • MongoDB索引Index
    MongoDB索引Index索引概述索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对应用的性能是非常致命的创......
  • MongoDB聚合操作之排序、分页
    聚合操作之排序、分页管道命令之$sort$sort用于将输入的文档排序后输出使用示例如下:查询人物,按照年龄升序db.person.aggregate([{$sort:{age:1}}])查询每个国家的人数,并排序db.person.aggregate([{$group:{_id:"$country",counter:{$sum:1}}},{$sort:{count......
  • docker部署Zookeeper+Kafka+Storm
    docker部署Zookeeper+Storm+Kafka安装docker信任Docker的GPG公钥:curl-fsSLhttps://repo.huaweicloud.com/docker-ce/linux/ubuntu/gpg|sudoapt-keyadd-`添加软件仓库:sudoadd-apt-repository"deb[arch=amd64]https://repo.huaweicloud.com/docker-ce/linux/ubun......