「MongoDB」の版間の差分
Kusanaginoturugi (トーク | 投稿記録) 細 (→使用方法) |
Kusanaginoturugi (トーク | 投稿記録) (→トラブルシューティング: insert == Configuration ==) |
||
28行目: | 28行目: | ||
$ mongo -u ''userName'' |
$ mongo -u ''userName'' |
||
+ | |||
+ | == Configuration == |
||
+ | |||
+ | === 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]: |
||
+ | |||
+ | {{hc|/etc/mongodb.conf|2= |
||
+ | 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 |
||
+ | .. |
||
+ | }} |
||
+ | |||
+ | See https://docs.mongodb.com/manual/reference/configuration-options/ for available configuration options. |
||
+ | |||
+ | === Requiring Authentication === |
||
+ | |||
+ | {{warning|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 [https://docs.mongodb.com/manual/tutorial/enable-authentication/]: |
||
+ | |||
+ | {{hc|$ mongo| |
||
+ | use admin |
||
+ | db.createUser( |
||
+ | { |
||
+ | user: "myUserAdmin", |
||
+ | pwd: "abc123", |
||
+ | roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] |
||
+ | } |
||
+ | )}} |
||
+ | |||
+ | If your {{ic|/etc/mongodb.conf}} uses the new YAML format, [[append]]: |
||
+ | |||
+ | {{hc|/etc/mongodb.conf| |
||
+ | security: |
||
+ | authorization: "enabled" |
||
+ | }} |
||
+ | |||
+ | If it uses the v2.4 format, add: |
||
+ | {{hc|/etc/mongodb.conf|2= |
||
+ | auth = true |
||
+ | }} |
||
+ | |||
+ | [[Restart]] {{ic|mongodb.service}}. |
||
+ | |||
+ | === NUMA === |
||
+ | |||
+ | Running MongoDB with Non-Uniform Access Memory (NUMA) can significantly impact performance. [https://docs.mongodb.com/manual/administration/production-notes/#mongodb-and-numa-hardware] |
||
+ | |||
+ | To see if your system uses 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.) |
||
+ | |||
+ | If your system uses NUMA, to improve performance, you should make MongoDB start through {{ic|numactl}}: |
||
+ | |||
+ | # 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: |
||
+ | |||
+ | ExecStart=/usr/bin/mongod $OPTIONS |
||
+ | |||
+ | To: |
||
+ | |||
+ | ExecStart='''/usr/bin/numactl --interleave=all''' /usr/bin/mongod $OPTIONS |
||
+ | |||
+ | If using {{aur|mongodb-bin}}, 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, {{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.) |
||
+ | |||
+ | === 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. |
||
+ | |||
+ | {{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. |
||
+ | |||
+ | 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] |
||
+ | |||
+ | {{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.: |
||
+ | |||
+ | # 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) |
||
+ | |||
+ | 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 [https://jira.mongodb.org/browse/SERVER-38086], so {{aur|mongodb}} prevents this. If using {{aur|mongodb-bin}}, to make [[systemd]] wait as long as it takes for MongoDB to start, [[append]] to the {{ic|[Service]}} section: |
||
+ | |||
+ | TimeoutStartSec=infinity |
||
== トラブルシューティング == |
== トラブルシューティング == |
2021年3月19日 (金) 22:19時点における版
MongoDB (由来は humongous から) はオープンソースのドキュメント指向データベースです。MongoDB Inc. (旧 10gen) によって開発・サポートされています。MongoDB はデータベースシステムの NoSQL ファミリーの一員です。"古典的"なリレーショナルデータベースでそうあるように、データをテーブルに保存する代わりに、MongoDB は動的なスキーマ (MongoDB では BSON と呼称されるフォーマット) によって JSON ライクなドキュメントとして構造的データを保存して、特定のタイプのアプリケーションでデータを簡単かつ高速に扱えるようにします。
目次
インストール
mongodbAUR あるいは mongodb-binAUR パッケージをインストールしてください。また、mongodb-toolsAUR か mongodb-tools-binAUR をインストールすることで mongoimport
, mongoexport
, mongodump
, mongorestore
などのツールを使うことができます。
mongodb.service
デーモンを起動・有効化します。
mongodb サービスの最初の起動時に、巨大なファイルが作成され、(ジャーナルなどのデータのための) 予約領域が割り当てられます [1]。ファイルの作成には時間がかかり、その間、データベースシェルを使うことはできません。
使用方法
Start/Enable the mongodb.service
daemon.
データベースシェルにアクセスするには、ターミナルに以下を入力:
$ mongo
Or, if authentication is configured:
$ mongo -u userName
Configuration
File Format
MongoDB 2.6 introduced a YAML-based configuration file format. The 2.4 configuration file format remains for backward compatibility [2]:
/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 ..
See https://docs.mongodb.com/manual/reference/configuration-options/ for available configuration options.
Requiring Authentication
To create a MongoDB user account with administrator access [3]:
$ mongo
use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
If your /etc/mongodb.conf
uses the new YAML format, append:
/etc/mongodb.conf
security: authorization: "enabled"
If it uses the v2.4 format, add:
/etc/mongodb.conf
auth = true
Restart mongodb.service
.
NUMA
Running MongoDB with Non-Uniform Access Memory (NUMA) can significantly impact performance. [4]
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
:
# cp /usr/lib/systemd/system/mongodb.service /etc/systemd/system
And change how /etc/systemd/system/mongodb.service
starts MongoDB.
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
. (Merely restarting it will not switch to the /etc
version.)
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's asked to stop, if it hasn't 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. [5] 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. [6]
To prevent systemd from killing MongoDB after 90 seconds, make and then edit your own .service
file, i.e.:
# cp /usr/lib/systemd/system/mongodb.service /etc/systemd/system
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 [8], 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