0. 前提
2021/1/4時点
- RHEL7.6のAMI(古いです。8系が最新です。)
- MongoDB4.4.2(この記事を書いている時点での最新)
例によってEC2とかしか使えない環境なので、Amazon DocumentDB使えよというツッコミは無しで。お察しください。
あるいは、マネージドサービスは得てして最新バージョンには対応していないことが多いので、どうしても最新バージョンのMongoDBを使いたいという人には役に立つ記事かも知れません。
1. MongoDBのtgzファイルをダウンロード
MongoDB Enterprise Server Download | MongoDB
https://www.mongodb.com/try/download/community
からMongoDBのアーカイブをダウンロードする。
- Choose which type of deployment is best for you:On-Premisesを選択する
- MongoDB Enterprise Serverのアコーディオンを展開する
- version:4.4.2(current)を選択する(デフォルトがcurrentなのでそのまま)
- Platform:RedHat / CentOS 6.2+を選択する
- package:tgzを選択する
- Downloadボタンをクリックする
mongodb-linux-x86_64-rhel62-4.4.2.tgzがダウンロードできる。
2. EC2インスタンス起動
2.1. AMI
東京リージョンで「RHEL 7.6」でAMIを検索すると、
RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2
ami-00b95502a4d51a07e
Provided by Red Hat, Inc.
が見つかりますので、これを選択します。
2.2. インスタンスタイプ
t2.micro (お試しならこれで十分)
2.3. セキュリティグループ
tcp/27017をオープン (お試しなら不要)
3. MongoDBインストール
3.1. sftp接続
sftp -i private.pem ec2-user@[public ip]
1.でダウンロードしたtgzファイルをサーバにアップロードする。
3.2. ssh接続
ssh -i private.pem ec2-user@[public ip]
3.3. liblzma.so.0のシンボリックリンク作成
tgzを展開してmongodを起動すると、下記のようなエラーで怒られるので、liblzma.so.0のシンボリックを作成する。
[ec2-user@ip-172-30-2-127 bin]$ ./mongod --help
./mongod: error while loading shared libraries: liblzma.so.0: cannot open shared object file: No such file or directory
3.3.1 liblzma.soのバージョン確認
ls /lib64/liblzma*
実行例:
[ec2-user@ip-172-30-2-127 bin]$ ls /lib64/liblzma*
/lib64/liblzma.so.5 /lib64/liblzma.so.5.2.2
3.3.2 シンボリックリンク作成(要sudo)
sudo ln -s /lib64/liblzma.so.5.2.2 /lib64/liblzma.so.0
3.3.3 シンボリックリンク確認
ls -al /lib64/liblzma*
実行例:
[ec2-user@ip-172-30-2-127 bin]$ ls -al /lib64/liblzma*
lrwxrwxrwx. 1 root root 23 Jan 4 01:34 /lib64/liblzma.so.0 -> /lib64/liblzma.so.5.2.2
lrwxrwxrwx. 1 root root 16 Jan 28 2019 /lib64/liblzma.so.5 -> liblzma.so.5.2.2
-rwxr-xr-x. 1 root root 157400 Mar 4 2016 /lib64/liblzma.so.5.2.2
対処法は、下記サイトを参考にした。
mod_securityがRHELまたはOracle Linux 7で有効にされているときにOracle HTTP Serverが起動に失敗する
https://docs.oracle.com/cd/E92951_01/webtier/administer-ohs/GUID-5571D9DB-8F96-4473-9D61-93249DE726AD.htm
3.4. アーカイブの展開
tar zxvf mongodb-linux-x86_64-rhel62-4.4.2.tgz
mongodb-linux-x86_64-rhel62-4.4.2ディレクトリが存在していれば成功。
4. 動作確認
4.1. データベースディレクトリの作成
cd mongodb-linux-x86_64-rhel62-4.4.2
mkdir db
4.2. MongoDB Serverの起動
bin/mongod --dbpath db &
mongodが起動する
4.3. CLIからの接続
bin/mongo
色々やってみた
[ec2-user@ip-172-30-2-127 mongodb-linux-x86_64-rhel62-4.4.2]$ bin/mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2021-01-04T01:35:21.601+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:52938","connectionId":1,"connectionCount":1}}
{"t":{"$date":"2021-01-04T01:35:21.602+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn1","msg":"client metadata","attr":{"remote":"127.0.0.1:52938","client":"conn1","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.2"},"os":{"type":"Linux","name":"Red Hat Enterprise Linux Server release 7.6 (Maipo)","architecture":"x86_64","version":"Kernel 3.10.0-957.5.1.el7.x86_64"}}}}
Implicit session: session { "id" : UUID("fc1bfeb5-986f-4f36-ac7b-a278bb5bef57") }
MongoDB server version: 4.4.2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-01-04T01:35:11.521+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-01-04T01:35:11.521+00:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2021-01-04T01:35:11.521+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-01-04T01:35:11.521+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2021-01-04T01:35:11.521+00:00: Soft rlimits too low
2021-01-04T01:35:11.521+00:00: currentValue: 1024
2021-01-04T01:35:11.521+00:00: recommendedMinimum: 64000
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> show collections
system.version
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.mycoll.find() list objects in collection mycoll
db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> use test;
switched to db test
> db.people.insert({"name":"hoge", "age":30})
{"t":{"$date":"2021-01-04T01:37:26.397+00:00"},"s":"I", "c":"STORAGE", "id":20320, "ctx":"conn1","msg":"createCollection","attr":{"namespace":"test.people","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"7a68d757-0ffe-4a7c-9c1a-a1e15ff56aff"}},"options":{}}}
{"t":{"$date":"2021-01-04T01:37:26.405+00:00"},"s":"I", "c":"INDEX", "id":20345, "ctx":"conn1","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"test.people","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
WriteResult({ "nInserted" : 1 })
> db.people.find()
{ "_id" : ObjectId("5ff27156a04374c0a5db9eb2"), "name" : "hoge", "age" : 30 }
> exit
bye
{"t":{"$date":"2021-01-04T01:37:40.619+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn1","msg":"Connection ended","attr":{"remote":"127.0.0.1:52938","connectionId":1,"connectionCount":0}}
[ec2-user@ip-172-30-2-127 mongodb-linux-x86_64-rhel62-4.4.2]$
4. 余談
普通の人は、Amazon DocumentDB (MongoDB 互換)を使えばよいと思います。
https://aws.amazon.com/jp/documentdb/
公式サイトの表記ゆれが激しい件。MONGODB/mongoDB/MongoDB。ドキュメント内はMongoDBで統一されている模様。
5. 参考リンク
MongoDB公式
https://www.mongodb.com/2