「MongoDB」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
37行目: 37行目:
 
=== File Format ===
 
=== File Format ===
   
MongoDB 2.6 introduced a YAML-based configuration file format. The 2.4 configuration file format remains for backward compatibility [https://docs.mongodb.com/manual/reference/configuration-options/#file-format]:
+
MongoDB uses a YAML-based configuration file format. See https://docs.mongodb.com/manual/reference/configuration-options/ for available configuration options.
   
 
{{hc|/etc/mongodb.conf|2=
 
{{hc|/etc/mongodb.conf|2=
56行目: 56行目:
 
..
 
..
 
}}
 
}}
 
See https://docs.mongodb.com/manual/reference/configuration-options/ for available configuration options.
 
   
 
=== Requiring Authentication ===
 
=== Requiring Authentication ===
65行目: 63行目:
 
To create a MongoDB user account with administrator access [https://docs.mongodb.com/manual/tutorial/enable-authentication/]:
 
To create a MongoDB user account with administrator access [https://docs.mongodb.com/manual/tutorial/enable-authentication/]:
   
{{hc|$ mongo|
+
{{hc|$ mongosh|
 
use admin
 
use admin
 
db.createUser(
 
db.createUser(
75行目: 73行目:
 
)}}
 
)}}
   
If your {{ic|/etc/mongodb.conf}} uses the new YAML format, [[append]]:
+
[[Append]] the following to your {{ic|/etc/mongodb.conf}}.
   
 
{{hc|/etc/mongodb.conf|
 
{{hc|/etc/mongodb.conf|
 
security:
 
security:
 
authorization: "enabled"
 
authorization: "enabled"
}}
 
 
If it uses the v2.4 format, add:
 
{{hc|/etc/mongodb.conf|2=
 
auth = true
 
 
}}
 
}}
   
95行目: 88行目:
 
To see if your system uses NUMA:
 
To see if your system uses NUMA:
   
$ dmesg | grep -i numa
+
# dmesg | grep -i numa
   
 
Also, {{ic|/var/log/mongodb/mongod.log}} will show warnings if NUMA is in use and MongoDB is not started through {{ic|numactl}}. (The {{ic|mongo}} shell will also show this, but only if you do not have authentication enabled.)
 
Also, {{ic|/var/log/mongodb/mongod.log}} will show warnings if NUMA is in use and MongoDB is not started through {{ic|numactl}}. (The {{ic|mongo}} shell will also show this, but only if you do not have authentication enabled.)
   
If your system uses NUMA, to improve performance, you should make MongoDB start through {{ic|numactl}}:
+
If your system uses NUMA, to improve performance, you should make MongoDB start through {{ic|numactl}}.
   
  +
[[Edit]] {{ic|mongodb.service}} according to the package you installed.
# cp /usr/lib/systemd/system/mongodb.service /etc/systemd/system
 
 
And change how {{ic|/etc/systemd/system/mongodb.service}} starts MongoDB.
 
   
 
If using {{aur|mongodb}}, change it from:
 
If using {{aur|mongodb}}, change it from:
118行目: 109行目:
   
 
To:
 
To:
  +
 
ExecStart='''/usr/bin/numactl --interleave=all''' /usr/bin/mongod --quiet --config /etc/mongodb.conf
 
ExecStart='''/usr/bin/numactl --interleave=all''' /usr/bin/mongod --quiet --config /etc/mongodb.conf
   
 
Zone claim also needs to be disabled, but on arch, {{ic|/proc/sys/vm/zone_reclaim_mode}} defaults to {{ic|0}}.
 
Zone claim also needs to be disabled, but on arch, {{ic|/proc/sys/vm/zone_reclaim_mode}} defaults to {{ic|0}}.
   
[[Systemd#Replacement unit files|Reenable]] and [[Restart]] {{ic|mongodb.service}}. (Merely restarting it will not switch to the {{ic|/etc}} version.)
+
[[Reenable]] and [[Restart]] {{ic|mongodb.service}} as needed.
   
 
=== Clean Start and Stop ===
 
=== Clean Start and Stop ===
128行目: 120行目:
 
By default, [[systemd]] immediately kills anything after asking it to start or stop, if it has not finished doing so within 90 seconds.
 
By default, [[systemd]] immediately kills anything after asking it to start or stop, if it has not finished doing so within 90 seconds.
   
{{aur|mongodb}} makes [[systemd]] wait as long as it takes for MongoDB to start, but {{aur|mongodb-bin}} does not. Both packages allow [[systemd]] to kill MongoDB after it's asked to stop, if it hasn't finished within 90 seconds.
+
{{aur|mongodb}} makes [[systemd]] wait as long as it takes for MongoDB to start, but {{aur|mongodb-bin}} does not. Both packages allow [[systemd]] to kill MongoDB after it is asked to stop, if it has not finished within 90 seconds.
   
 
Large MongoDB databases can take a considerable amount of time to cleanly shut down, especially if swap is being used. (An active 450GB database on a top of the line NVMe with 64GB RAM and 16GB swap can take an hour to shut down.)
 
Large MongoDB databases can take a considerable amount of time to cleanly shut down, especially if swap is being used. (An active 450GB database on a top of the line NVMe with 64GB RAM and 16GB swap can take an hour to shut down.)
   
By default, MongoDB uses journaling. [https://docs.mongodb.com/manual/reference/configuration-options/#storage-options] With journaling, an unclean shutdown should not pose a risk of data loss. But, if not shutdown cleanly, large MongoDB databases can take a considerable amount of time to start back up. In this case, choosing whether to require a clean shutdown is a choice of a slower shutdown versus a slower startup. [https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/mongodb-user/KjBU_GcNcmw/gR2UxRIFAgAJ]
+
By default, MongoDB uses journaling. [https://docs.mongodb.com/manual/reference/configuration-options/#storage-options] With journaling, an unclean shutdown should not pose a risk of data loss. But, if not shutdown cleanly, large MongoDB databases can take a considerable amount of time to start back up. In this case, choosing whether to require a clean shutdown is a choice of a slower shutdown versus a slower startup. [https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/mongodb-user/KjBU_GcNcmw/gR2UxRIFAgAJ]
   
 
{{warning|If you disable journaling, failing to require a clean shutdown severely risks data loss, so you really need to require a clean shutdown. [https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/]}}
 
{{warning|If you disable journaling, failing to require a clean shutdown severely risks data loss, so you really need to require a clean shutdown. [https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/]}}
   
To prevent [[systemd]] from killing MongoDB after 90 seconds, make and then [[edit]] your own {{ic|.service}} file, i.e.:
+
To prevent [[systemd]] from killing MongoDB after 90 seconds, [[edit]] {{ic|mongodb.service}}.
 
# cp /usr/lib/systemd/system/mongodb.service /etc/systemd/system
 
   
 
To allow MongoDB to cleanly shutdown, [[append]] to the {{ic|[Service]}} section: (On large databases, this may substantially slow down your system shutdown time, but speeds up your next MongoDB start time)
 
To allow MongoDB to cleanly shutdown, [[append]] to the {{ic|[Service]}} section: (On large databases, this may substantially slow down your system shutdown time, but speeds up your next MongoDB start time)

2022年5月3日 (火) 22:27時点における版

MongoDB (由来は humongous から) はオープンソースのドキュメント指向データベースです。MongoDB Inc. (旧 10gen) によって開発・サポートされています。MongoDB はデータベースシステムの NoSQL ファミリーの一員です。"古典的"なリレーショナルデータベースでそうあるように、データをテーブルに保存する代わりに、MongoDB は動的なスキーマ (MongoDB では BSON と呼称されるフォーマット) によって JSON ライクなドキュメントとして構造的データを保存して、特定のタイプのアプリケーションでデータを簡単かつ高速に扱えるようにします。

インストール

MongoDB は再ライセンス上の問題により公式リポジトリから削除されました [1]

PKGBUILDAUR で提供されています:

  • mongodbAUR - ソースコードからビルドします。180GB 以上のディスク空き領域が必要で、おそらくビルドに数時間かかります。(例: Intel i7 で6.5時間、ハイエンドの NVMe と 32 Xeon cores で1時間。)
  • mongodb-binAUR - ビルド済みの MongoDB バイナリを公式 MongoDB Ubuntu リポジトリから抽出します。ビルドに使用されたコンパイラオプションは不明です。

選んだメインの PKGBUILD に対応する、AUR にある PKGBUILD を使ってツール(mongoimportmongoexportmongodumpmongorestore など) をインストールしてください:

使用方法

mongodb.service デーモンを起動・有効化します。

ノート: mongodb サービスは初回起動時に大きいファイルを作成することにより領域を事前に確保します(ジャーナルや他のデータ用)。この処理には時間が掛かり、その間、MongoDB シェルは使用不能となります。

データベースシェルにアクセスするには、ターミナルに以下を入力:

$ mongo

または、認証設定がなされている場合:

$ mongo -u userName

Configuration

File Format

MongoDB uses a YAML-based configuration file format. See https://docs.mongodb.com/manual/reference/configuration-options/ for available configuration options.

/etc/mongodb.conf
systemLog:
   destination: file
   path: "/var/log/mongodb/mongod.log"
   logAppend: true
storage:
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
..

Requiring Authentication

警告: By default, MongoDB does not require any authentication. Although MongoDB only listens on the localhost interface by default, this still allows any local user to connect without authenticating and may exposes the database(s). It is recommended to enable access control to prevent any unwanted access.

To create a MongoDB user account with administrator access [2]:

$ mongosh
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Append the following to your /etc/mongodb.conf.

/etc/mongodb.conf
security:
  authorization: "enabled"

Restart mongodb.service.

NUMA

Running MongoDB with Non-Uniform Access Memory (NUMA) can significantly impact performance. [3]

To see if your system uses NUMA:

# dmesg | grep -i numa

Also, /var/log/mongodb/mongod.log will show warnings if NUMA is in use and MongoDB is not started through numactl. (The mongo shell will also show this, but only if you do not have authentication enabled.)

If your system uses NUMA, to improve performance, you should make MongoDB start through numactl.

Edit mongodb.service according to the package you installed.

If using mongodbAUR, change it from:

ExecStart=/usr/bin/mongod $OPTIONS

To:

ExecStart=/usr/bin/numactl --interleave=all /usr/bin/mongod $OPTIONS

If using mongodb-binAUR, change it from:

ExecStart=/usr/bin/mongod --quiet --config /etc/mongodb.conf

To:

ExecStart=/usr/bin/numactl --interleave=all /usr/bin/mongod --quiet --config /etc/mongodb.conf

Zone claim also needs to be disabled, but on arch, /proc/sys/vm/zone_reclaim_mode defaults to 0.

Reenable and Restart mongodb.service as needed.

Clean Start and Stop

By default, systemd immediately kills anything after asking it to start or stop, if it has not finished doing so within 90 seconds.

mongodbAUR makes systemd wait as long as it takes for MongoDB to start, but mongodb-binAUR does not. Both packages allow systemd to kill MongoDB after it is asked to stop, if it has not finished within 90 seconds.

Large MongoDB databases can take a considerable amount of time to cleanly shut down, especially if swap is being used. (An active 450GB database on a top of the line NVMe with 64GB RAM and 16GB swap can take an hour to shut down.)

By default, MongoDB uses journaling. [4] With journaling, an unclean shutdown should not pose a risk of data loss. But, if not shutdown cleanly, large MongoDB databases can take a considerable amount of time to start back up. In this case, choosing whether to require a clean shutdown is a choice of a slower shutdown versus a slower startup. [5]

警告: If you disable journaling, failing to require a clean shutdown severely risks data loss, so you really need to require a clean shutdown. [6]

To prevent systemd from killing MongoDB after 90 seconds, edit mongodb.service.

To allow MongoDB to cleanly shutdown, append to the [Service] section: (On large databases, this may substantially slow down your system shutdown time, but speeds up your next MongoDB start time)

TimeoutStopSec=infinity

If MongoDB needs a long time to start back up, it can be very problematic for systemd to keep killing and restarting it every 90 seconds [7], so mongodbAUR prevents this. If using mongodb-binAUR, to make systemd wait as long as it takes for MongoDB to start, append to the [Service] section:

TimeoutStartSec=infinity

トラブルシューティング

MongoDB が起動しない

データベースのパスが正しく設定されているか確認してください:

$ cat /usr/lib/systemd/system/mongodb.service

"ExecStart" 行に "--dbpath /var/lib/mongodb" を追加してください:

ExecStart=/usr/bin/numactl --interleave=all mongod --quiet --config /etc/mongodb.conf --dbpath /var/lib/mongodb

最低でも (ジャーナルファイル用の) 3GB の空き容量があることを確認してください。容量が足りない場合 MongoDB が起動できないことがあります (その場合ユーザーにメッセージは表示されません):

$ df -h /var/lib/mongodb/

ロックファイルが存在しないかどうか確認:

# ls -lisa /var/lib/mongodb

ロックファイルが存在する場合、mongodb.service を停止してください。データベースのパスを指定してデータベースの修復を実行します (Arch Linux では /var/lib/mongodb/ がデフォルトの --dbpath です):

# mongod --dbpath /var/lib/mongodb/ --repair

修復が完了すると、dbpath に修復されたデータファイルと空の mongod.lock ファイルが作られます。

警告: ファイルを削除して、破損したファイルを使ってデータベースを起動して、データベースからデータの復旧を試みることもできます。ただし、データベースの状態を予想することはできません。詳しくは 上流のドキュメント を見てください。

root で修復を実行後、ファイルの所有者は root ユーザーになりますが、Arch Linux は別のユーザーで MongoDB を実行します。chown を使ってファイルの所有者を適切なユーザーに戻す必要があります (詳しくは こちら を参照):

# chown -R mongodb: /var/{log,lib}/mongodb/

mongodb の ドキュメント から設定ファイルをコピーした場合、以下の2行を削除して mongodb.service を再起動してください:

/etc/mongodb.conf
processManagement:
   fork: true

transparent_hugepage のカーネル設定について MongoDB がエラーを吐く

MongoDB の起動後、transparent_hugepage に関する警告が表示される場合、以下のファイルを編集することでシステム設定を永続的に無効化することができます (FreeDesktop tmpfiles.d マニュアル を参照):

/etc/tmpfiles.d/local.conf
w /sys/kernel/mm/transparent_hugepage/enabled - - - - never
w /sys/kernel/mm/transparent_hugepage/defrag - - - - never

sysctl を使ったり以下のように echo コマンドを使うことで一時的に無効化することもできます:

# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# echo never > /sys/kernel/mm/transparent_hugepage/defrag

Warning about Soft rlimits too low

If you are using systemd service, then edit the unit file by systemctl edit mongodb.service:

[Service]
# Other directives omitted
# (file size)
LimitFSIZE=infinity
# (cpu time)
LimitCPU=infinity
# (virtual memory size)
LimitAS=infinity
# (locked-in-memory size)
LimitMEMLOCK=infinity
# (open files)
LimitNOFILE=64000
# (processes/threads)
LimitNPROC=64000

See following link for further details: Further reference