😎

ひとりMongoDB University 12/21 - レプリカセットのデプロイ

13 min read

この記録は、アドベントカレンダー形式の、MongoDB Universityの学習コースの記録、21日目になります!
ただいまコース: M103[1]を進めています。Chapter2を1日3つぐらいずつ進めたい。
25日までに15stepが目標です。

Chapter 2: Lab: Deploy a Replica Set (演習問題)

Problem

Launch a replica set with three members:
レプリカセットを3つ起動しましょう!

  1. There are three configuration files in your IDE workspace, but they are incomplete.

Update these configuration files so that all three mongod processes:

authenticate internally using the keyfile /var/mongodb/pki/m103-keyfile
belong to the replica set m103-repl

設定の参考情報:

  1. Once your configuration files are complete, start a mongod process using one of them.
  • 1つだけ起動
  1. When this node is running, use the mongo shell to connect and initiate your replica set with rs.initiate(). The shell prompt will read PRIMARY when the election is complete.
  • port 27001 で接続すること!
  • rs.initiate() ってなに?
    • https://docs.mongodb.com/manual/reference/method/rs.initiate/
    • Initiates a replica set. レプリカセットを初期化します。
    • Optionally, the method can take an argument in the form of a document that holds the configuration of a replica set.
      - また、引数をとることでレプリカセットを調整できます
# 接続してみる!
bash-4.4# mongo --port 27001
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27001/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5930a10f-7f68-4282-869e-dd14001f66da") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

レプリカセットの初期化。

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "localhost:27001",
        "ok" : 1
}

キャプチャです。プロンプトに "PRIMARY" が表示されました!

  1. Because the replica set uses a keyfile for internal authentication, clients must authenticate before performing any actions.
db.createUser({
  user: "m103-admin",
  pwd: "m103-pass",
  roles: [
    {role: "root", db: "admin"}
  ]
})

実際の処理。

# mongo shellのプロンプトが変わりました!
m103-repl:OTHER>


# まずはユーザ作成 (さらにmongo shellのプロンプトが変わりました!)
m103-repl:PRIMARY> use admin
switched to db admin
m103-repl:PRIMARY> db.createUser({
   user: "m103-admin",
   pwd: "m103-pass",
   roles: [
     {role: "root", db: "admin"}
   ]
 })
Successfully added user: {
        "user" : "m103-admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
  1. Once m103-admin is created, exit the mongo shell and start the other two mongod processes with their respective configuration files.
# 残り2つ起動
bash-4.4# mongod -f mongod_2.conf
bash-4.4# mongod -f mongod_3.conf

bash-4.4# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:27001         0.0.0.0:*               LISTEN
tcp        0      0 localhost:27002         0.0.0.0:*               LISTEN
tcp        0      0 localhost:27003         0.0.0.0:*               LISTEN
tcp        0      0 localhost:53672         localhost:27001         TIME_WAIT
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  3      [ ]         DGRAM                    1150502782 /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     1150659984 /tmp/mongodb-27002.sock
unix  2      [ ACC ]     STREAM     LISTENING     1150573201 /tmp/mongodb-27001.sock
unix  2      [ ACC ]     STREAM     LISTENING     1150658340 /tmp/mongodb-27003.sock
unix  2      [ ]         DGRAM                    1150502814
bash-4.4#
  1. Reconnect to your primary node as m103-admin and add the other two nodes to your replica set using rs.add().
  • プライマリノードに再接続し、レプリカセットに追加をする
  • rs.add()
bash-4.4# mongo --port 27001 -u m103-admin -p m103-pass

---

# primaryに接続
m103-repl:PRIMARY>
m103-repl:PRIMARY> rs.add( { host: "localhost:27002" } )
{
        "ok" : 1,
        "operationTime" : Timestamp(1608562007, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1608562007, 1),
                "signature" : {
                        "hash" : BinData(0,"CjAYCI3CxKlruG818lnvta3FkdU="),
                        "keyId" : NumberLong("6908718490643857410")
                }
        }
}

m103-repl:PRIMARY>
m103-repl:PRIMARY> rs.add( { host: "localhost:27003" } )
{
        "ok" : 1,
        "operationTime" : Timestamp(1608562027, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1608562027, 1),
                "signature" : {
                        "hash" : BinData(0,"ih1GVxuhUrGc4nL6Zz0K5uCtGs8="),
                        "keyId" : NumberLong("6908718490643857410")
                }
        }
}

キャプチャはこんな感じ。

  1. Once your other two members have been successfully added, run rs.status() to check that the members array has three nodes - one labeled PRIMARY and two labeled SECONDARY.

プライマリに接続した状態で、結果を確認!
"stateStr" : "SECONDARY" 2つ、"stateStr" : "PRIMARY" 1つを確認!

m103-repl:PRIMARY> rs.status()
{
        "set" : "m103-repl",
        "date" : ISODate("2020-12-21T14:48:05.453Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1608562083, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1608562083, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1608562083, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1608562083, 1),
                        "t" : NumberLong(1)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1608562027, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 986,
                        "optime" : {
                                "ts" : Timestamp(1608562083, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-21T14:48:03Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1608561372, 2),
                        "electionDate" : ISODate("2020-12-21T14:36:12Z"),
                        "configVersion" : 3,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 77,
                        "optime" : {
                                "ts" : Timestamp(1608562083, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1608562083, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-21T14:48:03Z"),
                        "optimeDurableDate" : ISODate("2020-12-21T14:48:03Z"),
                        "lastHeartbeat" : ISODate("2020-12-21T14:48:05.065Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-21T14:48:04.089Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27001",
                        "syncSourceHost" : "localhost:27001",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 58,
                        "optime" : {
                                "ts" : Timestamp(1608562083, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1608562083, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2020-12-21T14:48:03Z"),
                        "optimeDurableDate" : ISODate("2020-12-21T14:48:03Z"),
                        "lastHeartbeat" : ISODate("2020-12-21T14:48:05.066Z"),
                        "lastHeartbeatRecv" : ISODate("2020-12-21T14:48:04.338Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "localhost:27001",
                        "syncSourceHost" : "localhost:27001",
                        "syncSourceId" : 0,
                        "infoMessage" : "",
                        "configVersion" : 3
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1608562083, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1608562083, 1),
                "signature" : {
                        "hash" : BinData(0,"rXS8Wjw1L/KN/HVCUX4TCVdDm9Y="),
                        "keyId" : NumberLong("6908718490643857410")
                }
        }
}
  1. Click "Run Tests" to run a suite of tests that will check the configuration of your replica set. The results of these tests will let you know which steps you've yet to complete.

テスト実行。
やったあ!!!!通りました!!!!!

おさらいが必要そうですが、とにかく通りました!

今日の進捗

1個だけ、演習問題だけ完了。
初めてのレプリカセットの起動でした!

きょうのzenn

ひきつづき同じ方法で進めています :)
アップデートの通知が来ていました。ありがとうございます!!

脚注
  1. M103: Basic Cluster Administration のコースになります。コースを開始すると、完了までの期限は2ヶ月以内です。 ↩︎

Discussion

ログインするとコメントできます