Mongodb-副本集

docker inspect --format '{{ .NetworkSettings.IPAddress }}'容器id


[root@localhost ~]# docker run -it --name master --privileged=true -p 17017:17017 -v /home/Java:/home/test mongodb:1.1 /bin/bash

[root@localhost ~]# docker run -it --name slave1 --privileged=true -p 17018:17018 -v /home/Java:/home/test mongodb:1.1 /bin/bash         

[root@localhost ~]# docker run -it --name slave2 --privileged=true -p 17019:17019 -v /home/Java:/home/test mongodb:1.1 /bin/bash


[root@localhost ~]# docker exec -it master /bin/bash

[root@6657aa3049f0 /]# cd /home/Java/mongodb

[root@6657aa3049f0 mongodb]# bin/mongod --port 17017 --replSet Cloudcc/172.17.0.3:17018



[root@localhost ~]# docker exec -it slave1 /bin/bash

[root@f0fff4d31a82 /]# cd /home/Java/mongodb

[root@f0fff4d31a82 mongodb]# bin/mongod --port 17018 --replSet Cloudcc/172.17.0.2:17017



[root@localhost ~]# docker exec -it master /bin/bash

[root@6657aa3049f0 /]# cd /home/Java/mongodb

[root@6657aa3049f0 mongodb]# bin/mongo 172.17.0.2:17017/admin

> db.runCommand({"replSetInitiate":{"_id":"Cloudcc","members":[{"_id":1,"host":"172.17.0.2:17017"},{"_id":2,"host":"172.17.0.3:17018"}]}})

{ "ok" : 1 }

Cloudcc:SECONDARY>



[root@localhost ~]# docker exec -it slave2 /bin/bash

[root@b95dbf31f6f2 /]# cd /home/Java/mongodb

[root@b95dbf31f6f2 mongodb]# bin/mongod --port 17019 --replSet Cloudcc/172.17.0.2:17017



[root@localhost ~]# docker exec -it master /bin/bash

[root@6657aa3049f0 /]# cd /home/Java/mongodb

[root@6657aa3049f0 mongodb]# bin/mongo 172.17.0.2:17017/admin

Cloudcc:SECONDARY> rs.addArb("172.17.0.4:17019")

{ "ok" : 1 }

Cloudcc:PRIMARY> rs.status

function () {

    return db._adminCommand("replSetGetStatus");

}

Cloudcc:PRIMARY> rs.status()

{

        "set" : "Cloudcc",

        "date" : ISODate("2017-07-19T08:36:31.766Z"),

        "myState" : 1,

        "term" : NumberLong(1),

        "heartbeatIntervalMillis" : NumberLong(2000),

        "optimes" : {

                "lastCommittedOpTime" : {

                        "ts" : Timestamp(1500453389, 1),

                        "t" : NumberLong(1)

                },

                "appliedOpTime" : {

                        "ts" : Timestamp(1500453389, 1),

                        "t" : NumberLong(1)

                },

                "durableOpTime" : {

                        "ts" : Timestamp(1500453389, 1),

                        "t" : NumberLong(1)

                }

        },

        "members" : [

                {

                        "_id" : 1,

                        "name" : "172.17.0.2:17017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 1302,

                        "optime" : {

                                "ts" : Timestamp(1500453389, 1),

                                "t" : NumberLong(1)

                        },

                        "optimeDate" : ISODate("2017-07-19T08:36:29Z"),

                        "electionTime" : Timestamp(1500452748, 1),

                        "electionDate" : ISODate("2017-07-19T08:25:48Z"),

                        "configVersion" : 2,

                        "self" : true

                },

                {

                        "_id" : 2,

                        "name" : "172.17.0.3:17018",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 655,

                        "optime" : {

                                "ts" : Timestamp(1500453389, 1),

                                "t" : NumberLong(1)

                        },

                        "optimeDurable" : {

                                "ts" : Timestamp(1500453389, 1),

                                "t" : NumberLong(1)

                        },

                        "optimeDate" : ISODate("2017-07-19T08:36:29Z"),

                        "optimeDurableDate" : ISODate("2017-07-19T08:36:29Z"),

                        "lastHeartbeat" : ISODate("2017-07-19T08:36:31.467Z"),

                        "lastHeartbeatRecv" : ISODate("2017-07-19T08:36:31.472Z"),

                        "pingMs" : NumberLong(0),

                        "syncingTo" : "172.17.0.2:17017",

                        "configVersion" : 2

                },

                {

                        "_id" : 3,

                        "name" : "172.17.0.4:17019",

                        "health" : 1,

                        "state" : 7,

                        "stateStr" : "ARBITER",

                        "uptime" : 62,

                        "lastHeartbeat" : ISODate("2017-07-19T08:36:31.467Z"),

                        "lastHeartbeatRecv" : ISODate("2017-07-19T08:36:29.958Z"),

                        "pingMs" : NumberLong(0),

                        "configVersion" : 2

                }

        ],

        "ok" : 1

}

Cloudcc:PRIMARY>




[root@localhost ~]# docker exec -it master /bin/bash

[root@6657aa3049f0 /]# cd /home/Java/mongodb

[root@6657aa3049f0 mongodb]# bin/mongo --port 17017

Cloudcc:PRIMARY> use cloudcc

switched to db cloudcc

Cloudcc:PRIMARY> db.test.insert({"username":"hacker", "age":26})

WriteResult({ "nInserted" : 1 })

Cloudcc:PRIMARY>




停掉master

[root@localhost ~]# docker exec -it slave1 /bin/bash

[root@f0fff4d31a82 /]# cd /home/Java/mongodb

[root@f0fff4d31a82 mongodb]# bin/mongo --port 17018

Cloudcc:PRIMARY> rs.status()

{

        "set" : "Cloudcc",

        "date" : ISODate("2017-07-19T08:48:12.350Z"),

        "myState" : 1,

        "term" : NumberLong(2),

        "heartbeatIntervalMillis" : NumberLong(2000),

        "optimes" : {

                "lastCommittedOpTime" : {

                        "ts" : Timestamp(1500453939, 1),

                        "t" : NumberLong(1)

                },

                "appliedOpTime" : {

                        "ts" : Timestamp(1500454089, 1),

                        "t" : NumberLong(2)

                },

                "durableOpTime" : {

                        "ts" : Timestamp(1500454089, 1),

                        "t" : NumberLong(2)

                }

        },

        "members" : [

                {

                        "_id" : 1,

                        "name" : "172.17.0.2:17017",

                        "health" : 0,

                        "state" : 8,

                        "stateStr" : "(not reachable/healthy)",

                        "uptime" : 0,

                        "optime" : {

                                "ts" : Timestamp(0, 0),

                                "t" : NumberLong(-1)

                        },

                        "optimeDurable" : {

                                "ts" : Timestamp(0, 0),

                                "t" : NumberLong(-1)

                        },

                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),

                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),

                        "lastHeartbeat" : ISODate("2017-07-19T08:48:10.391Z"),

                        "lastHeartbeatRecv" : ISODate("2017-07-19T08:45:47.627Z"),

                        "pingMs" : NumberLong(0),

                        "lastHeartbeatMessage" : "Connection refused",

                        "configVersion" : -1

                },

                {

                        "_id" : 2,

                        "name" : "172.17.0.3:17018",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 1863,

                        "optime" : {

                                "ts" : Timestamp(1500454089, 1),

                                "t" : NumberLong(2)

                        },

                        "optimeDate" : ISODate("2017-07-19T08:48:09Z"),

                        "electionTime" : Timestamp(1500453958, 1),

                        "electionDate" : ISODate("2017-07-19T08:45:58Z"),

                        "configVersion" : 2,

                        "self" : true

                },

                {

                        "_id" : 3,

                        "name" : "172.17.0.4:17019",

                        "health" : 1,

                        "state" : 7,

                        "stateStr" : "ARBITER",

                        "uptime" : 762,

                        "lastHeartbeat" : ISODate("2017-07-19T08:48:12.287Z"),

                        "lastHeartbeatRecv" : ISODate("2017-07-19T08:48:10.039Z"),

                        "pingMs" : NumberLong(0),

                        "configVersion" : 2

                }

        ],

        "ok" : 1

}

Cloudcc:PRIMARY>

你可能感兴趣的:(Mongodb-副本集)