dockermongodb怎么用(Percona版MongoDB副本集部署流程)(1)

※ MongoDB PRIMARY,SECONDARY库配置

dockermongodb怎么用(Percona版MongoDB副本集部署流程)(2)

1、进入容器后,默认为/etc/mongod.conf;找到/data/db,创建mongod.conf,添加配置內容:

①、PRIMARY库:

storage:

dbPath: /data/db/mongodb-data #根据自己的实际目录设定。

journal:

enabled: true

systemLog:

destination: file

logAppend: true

path: /data/db/mongodb-log/mongodb.log

processManagement:

fork: true

pidFilePath: /var/run/mongod.pid

net:

port: 27021 #容器需要连接mongo的port

bindIp: 0.0.0.0

replication:

replSetName: "rs-smy" #副本集名称,需要在启动mongo容器的时候指定。

security:

keyFile:/data/db/mongodb/mongodb-keyfile/mongodbkey #增加权限认证

②、SECONDARY库:

storage:

dbPath: /data/db/mongodb-data #根据自己的实际目录设定。

journal:

enabled: true

systemLog:

destination: file

logAppend: true

path: /data/db/mongodb-log/mongodb.log

processManagement:

fork: true

pidFilePath: /var/run/mongod.pid

net:

port: 27022 #容器需要连接mongo的port

bindIp: 0.0.0.0

replication:

replSetName: "rs-smy" #副本集名称,需要在启动mongo容器的时候指定

security:

keyFile:/data/db/mongodb/mongodb-keyfile/mongodbkey #增加权限认证

③、mongod_Arbiter.conf:

systemLog:

destination: file

logAppend: true

path: /data/db/arbiter/log/arbiter.log

storage:

dbPath:/data/db/arbiter/data

journal:true

enabled:true

processManagement:

fork: true

pidFilePath: /var/run/arbiter.pid

net:

port: 27023 #欲启动的端口

bindIp: 0.0.0.0

replication:

replSetName: "rs-smy"

security:

keyFile:/data/db/mongodb/mongodb-keyfile/mongodbkey

2、重启docker:

sudo docker restart<container _id>

3、启动两个mongodb进程:

A、sudo docker run --name pmdb45 -m 4g --cpus=2 -p 27021:27017 -v $PWD/db:/data/db -d hub.docker.com/percona-server-mongodb:latest --replSet rs-smy

B、 sudo docker run --name pmdb46 -m 4g --cpus=2 -p 27022:27017 -v $PWD/db:/data/db -d hub.docker.com/percona-server-mongodb:latest --replSet rs-smy

如果需要再加一个secondary,则在primary节点上用:rs.add(“host:ip”)

4、进入mongodb docker:

docker exec –it <container_id> bash

5、进入要作为master数据库的mongodb shell:

mongo IP1:27021/admin

6、添加并初始化副本集信息:

rs.initiate( {_id: "rs-smy",members: [{ _id: 0, host: "IP1:27021" },{ _id: 1, host: "IP2:27022" },{_id: 2, host: "IP3:27023}] }),成功后部分信息显示如下:

>rs.initiate( {_id: "rs-smy",members: [{ _id: 0, host: "IP1:27021" },{ _id: 1, host: "IP2:27022" },{_id: 2, host: "IP3:27023}] })

{

"ok" : 1,

"operationTime" : Timestamp(1573810405, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1573810405, 1),

"signature" : {

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

"keyId" : NumberLong(0)

}

}

}

7、查看状态信息:

rs-smy:PRIMARY> rs.status();

{

"set" : "rs-smy",

"date" : ISODate("2019-11-15T10:01:51.969Z"),

"myState" : 1,

"term" : NumberLong(1),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"heartbeatIntervalMillis" : NumberLong(2000),

"optimes" : {

"lastCommittedOpTime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"readConcernMajorityOpTime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"appliedOpTime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"durableOpTime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

}

},

"lastStableCheckpointTimestamp" : Timestamp(1573812099, 1),

"members" : [

{

"_id" : 0,

"name" : "IP1:27021",

"health" : 1,

"state" : 1,

"stateStr" : "PRIMARY",

"uptime" : 1975,

"optime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2019-11-15T10:01:49Z"),

"syncingTo" : "",

"syncSourceHost" : "",

"syncSourceId" : -1,

"infoMessage" : "",

"electionTime" : Timestamp(1573810417, 1),

"electionDate" : ISODate("2019-11-15T09:33:37Z"),

"configVersion" : 1,

"self" : true,

"lastHeartbeatMessage" : ""

},

{

"_id" : 1,

"name" : "IP2:27022",

"health" : 1,

"state" : 2,

"stateStr" : "SECONDARY",

"uptime" : 1338,

"optime" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"optimeDurable" : {

"ts" : Timestamp(1573812109, 1),

"t" : NumberLong(1)

},

"optimeDate" : ISODate("2019-11-15T10:01:49Z"),

"optimeDurableDate" : ISODate("2019-11-15T10:01:49Z"),

"lastHeartbeat" : ISODate("2019-11-15T10:01:50.652Z"),

"lastHeartbeatRecv" : ISODate("2019-11-15T10:01:50.650Z"),

"pingMs" : NumberLong(1),

"lastHeartbeatMessage" : "",

"syncingTo" : "IP1:27021",

"syncSourceHost" : "IP1:27021",

"syncSourceId" : 0,

"infoMessage" : "",

"configVersion" : 1

}

],

"ok" : 1,

"operationTime" : Timestamp(1573812109, 1),

"$clusterTime" : {

"clusterTime" : Timestamp(1573812109, 1),

"signature" : {

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

"keyId" : NumberLong(0)

}

}

}

8、添加权限认证:

dockermongodb怎么用(Percona版MongoDB副本集部署流程)(3)

rs-smy:PRIMARY> db.createUser({user:"mongo",pwd:"mongo",roles:["root"]})

执行结果为:

Successfully added user: { "user" : "mongo", "roles" : [ "root" ] }

※ 测试同步复制:1、连接到PRIMARY节点并创建例子:rs-smy:PRIMARY> db

percona

rs-smy:PRIMARY> db.percona.insert({id: 200})

WriteResult({ "nInserted" : 1 })

rs-smy:PRIMARY> db.percona.find()

{ "_id" : ObjectId("5dcfa4b9103a69d6d277dba4"), "id" : 200 }

2、在另外的CVM登陆到SECONDARY节点:

mongo IP3:27022

连接到SECONDARY节点并查找相同的文档:

rs-smy:SECONDARY> use percona

switched to db percona

rs-smy:SECONDARY> db.percona.find()

{ "_id" : ObjectId("5dcfa4b9103a69d6d277dba4"), "id" : 200 },可以看到SECONDARY已经同步了PRIMARY相同文档的数据了。

注:在SECONDARY节点上,需要rs.slaveOk(),读取SECONDARY节点上的数据,默认无法读取。

,