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