0
0

More than 1 year has passed since last update.

AWS日記33 (DocumentDB)

Last updated at Posted at 2021-11-30

はじめに

今回は Amazon DocumentDB を試します。

デベロッパーガイドのAmazon EC2 を使用したConnect の手順を参考に、EC2からDocumentDBに接続します。

Amazon EC2 インスタンスを作成

ec2_1.jpg

  • Amazon EC2 コンソールで、「インスタンスを起動」 を選択します。

ec2_2.jpg

  • 今回は「Amazon Linux 2 AMI」 を選択します。

ec2_3.jpg

  • インスタンスタイプは「t2.micro」 を選択します。

ec2_5.jpg

  • SSH接続できる設定になっていることを確認します。

  • 次の画面でAmazon EC2 key pairを作成・ダウンロードし、インスタンスを起動します。

セキュリティグループを作成

security_group_1.jpg

  • Amazon EC2 コンソールから「セキュリティーグループ」を選択します。
  • セキュリティーグループ名を入力し、VPCを選択します。

security_group_2.jpg

  • インバウンドルールから「ルールを追加」を選択します。

security_group_3.jpg

  • ポート範囲は「27017」に設定します。
  • ソースは先ほど作成したEC2のセキュリティーグループを選択します。
  • ページ最下部の「セキュリティーグループを作成」を選択します。

Amazon DocumentDB クラスターを作成

documentdb_0.jpg

  • Amazon DocumentDB コンソールのクラスターから、「作成」を選択します。

documentdb_1.jpg

  • 今回はインスタンス数を「 1 」に設定します。

documentdb_2.jpg

  • マスターユーザー名とマスターパスワードを設定します。
  • ページ最下部の「詳細設定の表示」を選択します。

documentdb_3.jpg

  • Virtual Private Cloud(VPC)はEC2と同じ設定にします。
  • VPCセキュリティーグループは、先ほど作成したセキュリティーグループを選択します。

documentdb_5.jpg

  • ページ最下部の「クラスターの作成」を選択します。

EC2に接続

access_to_ec2.jpg

  • EC2コンソールから、SSHクライアントでの接続方法を確認します。
  • EC2インスタンス作成時にダウンロードしたキーを使い、EC2にSSH接続します。

mongo シェルをインストール

  • リポジトリ・ファイルを作成します。
echo -e "[mongodb-org-3.6] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.6/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc" | sudo tee /etc/yum.repos.d/mongodb-org-3.6.repo 
  • mongo シェルをインストールします
sudo yum install -y mongodb-org-shell

Amazon DocumentDB に接続

access_to_documentdb.jpg

  • Amazon DocumentDBコンソールから、mongoシェルで接続する方法を確認します。
  • 認証に必要な証明書をダウンロードします。
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
  • mongoシェルのコマンドを実行し接続します。

動作確認

rs0:PRIMARY> 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

終わりに

Amazon DocumentDB を試しました。

SSHトンネルを作成してAmazon VPC の外部から Amazon DocumentDB クラスターへの接続する方法も、今後試して行こうと思います。

参考資料
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0