この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、菊池です。
先日、MongoDBの最新バージョンである、MongoDB 3.6がリリースされました。
Release Notes for MongoDB 3.6 | MongoDB Documentation
最新バージョンを使ってシャードクラスタを作成してみましたので手順を紹介します。
シャードクラスタの構成
MongoDBのsハーディングは、以下のような構成になります。
シャーディングで必要なプロセスは以下の通りです。
- mongod:クライアントから書き込まれたデータを保存するDBサーバのプロセス。3つ以上のmongodでレプリカセット構成をとり、複数のレプリカセットを並べることでシャーディングします。
- mongod(config):シャーディングの設定情報を保持するサーバ。バージョン3.2以降ではレプリカセット構成となりました。
- mongos:configサーバから情報を取得し、アプリケーションからのリクエストに対して適切なmongodへルーティングする。
構築
構成
実際に構築する構成です。
mongod、mongod(config)、mongosはそれぞれ独立したプロセスのため、別のサーバでも問題ありません。今回はサーバ台数を増やしたくありませんでしたので、上記のようにPortを分けて同居させ、レプリカセットのメンバー単位でEC2を分けて配置しています。
環境:
- OS:Amazon Linux AMI 2017.09.1
- MongoDB:3.6.1
手順
以下の手順で構築を行います。
- MongoDBのインストール
- mongodの設定・起動
- mongod-configの設定・起動
- mongosの設定・起動
1. MongoDBのインストール
まずはMongoDBのインストールです。yumでパッケージ管理を行うため、/etc/yum.repos.d/mongodb-org-3.6.repo
を作成して以下のように記述します。
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
続いて、インストールです。
$ sudo yum install -y mongodb-org
2. mongodの設定・起動
mongodの設定ファイルを/etc/mongod.conf
に作成します。
net:
port: 27027
bindIp: 172.31.xxx.xxx
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /data/db
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongod/mongod.pid
replication:
replSetName: s0
sharding:
clusterRole: shardsvr
注意点は、MongoDB 3.6から、デフォルトではlocalhostにのみバインドされるようになったことです。bindIp:
にEC2のプライベートIPを記載します。また、dbPath:
に記載したディレクトリを作成し、オーナーをmongodに変更しておきましょう。
3台ともインストールできたら、サービスを起動し、接続します。
$ sudo service mongod start
Starting mongod: [ OK ]
$
$ mongo --host mongo0:27027
MongoDB shell version v3.6.1
connecting to: mongodb://localhost:27027/
MongoDB server version: 3.6.1
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
Server has startup warnings:
2017-12-28T02:31:15.347+0000 I STORAGE [initandlisten]
2017-12-28T02:31:15.347+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-12-28T02:31:15.347+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten]
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten]
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** Start the server with --bind_ip
<address> to specify which IP
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2017-12-28T02:31:15.407+0000 I CONTROL [initandlisten]
2017-12-28T02:36:47.208+0000 E - [main] Error loading history file: FileOpenFailed: Unable to fopen() file /home/ec2-user/.dbnet:
shell: No such file or directory
>
レプリカセットとして認識させます。
> rs.initiate( {
... _id : "s0",
... members: [
... { _id: 0, host: "mongo0:27027" },
... { _id: 1, host: "mongo1:27027" },
... { _id: 2, host: "mongo2:27027" }
... ]
... })
{
"ok" : 1,
"operationTime" : Timestamp(1514429072, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1514429072, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
ここでmembers:
記述したメンバーのホスト名で名前解決可能である必要があります。
s0:PRIMARY>
s0:PRIMARY> rs.status()
Primaryに昇格を確認したら、念のためrs.status()
でメンバーが認識されていることを確認しておきます。
3. mongod-configの設定・起動
configサーバを設定します。/etc/mongod-config.conf
を作成します。
net:
port: 27030
bindIp: 172.31.xxx.xxx
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod-conf.log
storage:
dbPath: /data/config
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /var/run/mongod/mongod-conf.pid
sharding:
clusterRole: configsvr
replication:
replSetName: csReplSet
同様に、データディレクトリの作成、バインドIPの設定を実施します。3台ともインストールできたら、サービスを起動します。mongod-config用の起動スクリプトも作成しておきます。
$ sudo service mongod-config start
Starting mongod: [ OK ]
$
$ mongo --host mongo0:27030
起動・接続できたら、同様にレプリカセットを構成します。
rs.initiate(
{
_id: "csReplSet",
configsvr: true,
members: [
{ _id: 0, host: "mongo0:27030" },
{ _id: 1, host: "mongo1:27030" },
{ _id: 2, host: "mongo2:27030" }
]
}
)
4. mongosの設定・起動
最後に、mongosの設定です。/etc/mongos.conf
に設定を作成します。
net:
port: 27017
bindIp: 172.31.xxx.xxx
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongos.log
processManagement:
fork: true
pidFilePath: /var/run/mongod/mongos.pid
sharding:
configDB: csReplSet/mongo0:27030,mongo1:27030,mongo2:27030
バインドIPを設定し、configDB:
にconfigサーバのホスト/ポートを記述します。
起動スクリプトを作成し、起動します。
$ sudo service mongos start
Starting mongos: [ OK ]
$
$ mongo mongo0:27017
MongoDB shell version v3.6.1
Enter password:
connecting to: mongodb://mongo0:27017/admin
MongoDB server version: 3.6.1
Server has startup warnings:
2017-12-28T03:34:53.182+0000 I CONTROL [main]
2017-12-28T03:34:53.182+0000 I CONTROL [main] ** WARNING: Access control is not enabled for the database.
2017-12-28T03:34:53.182+0000 I CONTROL [main] ** Read and write access to data and configuration is unrestricted.
2017-12-28T03:34:53.182+0000 I CONTROL [main]
mongos>
mongosに接続したら、レプリカセットをシャードメンバーに登録します。
mongos> sh.addShard( "s0/mongo0:27027")
レプリカセット名とホスト1台を指定すればあとは認識します。
これで、1シャード構成のクラスタの構築ができました。
まとめ
MongoDB 3.6のシャードクラスタを構築しました。バインドIPの設定など、気づかないとハマる点もありますので注意しましょう。