本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
EC2手動 Connect Amazon
主題
下列步驟假設您已完成必要條件主題中的步驟。
步驟 1:創建一個 Amazon EC2 實例
在此步驟中,您將在稍後用來佈建 EC2 Amazon DocumentDB 叢集的同一VPC個區域和 Amazon 執行個體建立一個 Amazon 執行個體。
-
在 Amazon 主EC2控台上,選擇啟動執行個體。
-
在「名稱和標籤」區段的「名稱」欄位中輸入名稱或識別碼。
-
在 Amazon 機器映像 (AMI) 下拉列表中,找到 Amazon Linux 2 AMI 並選擇它。
-
在「執行個體類型」下拉式清單中找到並選擇 t3.micro。
-
在 [key pair (登入)] 區段中,輸入現有金鑰配對的識別碼,或選擇 [建立新金鑰配對]。
您必須提供一個 Amazon EC2 key pair。
如果你有一個 Amazon EC2 key pair:
-
選取 key pair,然後從清單中選擇您的 key pair。
-
您必須已經擁有可用的私密金鑰檔案 (.pem 或 .ppk 檔案) 才能登入 Amazon 執行個體。EC2
如果您沒有 Amazon EC2 key pair:
-
選擇建立新 key pair,建立 key pair 話方塊便會顯示。
-
在金鑰配對名稱欄位中輸入名稱。
-
選擇密鑰對類型和私鑰文件格式。
-
選擇 Create key pair (建立金鑰對)。
注意
為了安全起見,我們強烈建議您同時使用金鑰配對,SSH並將網際網路連線到您EC2的執行個體。
-
-
在 [網路設定] 區段的 [防火牆 (安全性群組)] 下,選擇 [建立安全性群組] 或 [選取現有的安全性群組]。
如果您選擇選取現有的安全性群組,請從 [一般安全性群組] 下拉式清單中選取一個群組。
如果您選擇建立新的安全性群組,請執行下列動作:
檢查所有適用於EC2連線的流量允許規則。
在 [IP] 欄位中,選擇 [我的 IP] 或選取 [自訂] 以從CIDR封鎖、首碼清單或安全群組清單中選擇。除非您的EC2執行個體位於隔離的網路上,否則我們不建議您選擇 Anywhere,因為它允許任何 IP 位址存取您的EC2執行個體。
-
在 [摘要] 區段中,檢閱您的EC2組態,並選擇 [啟動執行個體 (如果正確 編輯安全性群組。
步驟 2:建立安全群組
現在,您將在默認 Amazon 中創建一個新的安全組VPC。安全群組demoDocDB
可讓您從 Amazon 執行個體連接到連接埠 27017 (亞馬遜文件資料庫的預設連接埠) 上的 Amazon DocumentDB 叢集。EC2
-
在 Amazon EC2 管理主控台
的網路和安全性下,選擇安全群組。 -
選擇建立安全群組。
-
在「基本詳細資料」區段中:
針對 Security group name (安全群組名稱),輸入
demoDocDB
。在描述中,輸入描述。
對於 VPC,接受使用您的預設值VPC。
-
在 Inbound rules (傳入規則) 區段中,選擇 Add rule (新增規則)。
選擇「自訂TCP規則」做為「類型」。
對於連接埠範圍,輸入
27017
。選擇「自訂」做為「目的地」 在旁邊的欄位中,搜尋您剛才呼叫的安全性群組
demoEC2
。您可能需要重新整理瀏覽器,Amazon 主EC2控台才能自動填入demoEC2
來源名稱。
-
接受所有其他預設值並選擇建立安全性群組。
第 3 步:創建一個 Amazon DocumentDB 集群
在佈建亞馬遜EC2執行個體時,您將建立您的 Amazon DocumentDB 叢集。
-
導覽至 Amazon DocumentDB 主控台,然後從導覽窗格中選擇叢集。
-
選擇 Create (建立)。
-
將 [叢集類型] 設定保留為 [執行個體型叢集] 的預設值。
-
針對執行個體數目,選擇 1。這將使成本降至最低。將其他設定保留為預設值。
-
對於 [連線],保留 [不要連線至EC2計算資源] 的預設設定。
注意
連線至EC2計算資源會自動為您與叢集的EC2計算資源連線建立安全群組。由於您在上一個步驟中手動建立這些安全性群組,因此您應該選取 [不連線至EC2計算資源],以免建立第二組安全性群組。
-
對於「驗證」,請輸入登入認證。重要:在稍後的步驟中,您將需要登入認證才能驗證叢集。
-
開啟 [顯示進階設定]。
-
在「網路設定」區段中,對於 Amazon VPC 安全群組,請選擇「demoDoc資料庫」。
-
選擇建立叢集。
步驟 4:Connect 到您的 Amazon EC2 實例
要安裝蒙戈外殼,您必須首先連接到您的 Amazon EC2 實例。安裝蒙戈外殼使您能夠連接和查詢您的 Amazon DocumentDB 集群。請完成下列步驟:
-
在 Amazon EC2 主控台上,瀏覽至您的執行個體,並查看您剛建立的執行個體是否正在執行。如果是,請按一下執行個體 ID 來選取執行個體。
-
選擇連線。
-
您的連線方法有四個索引標籤選項:Amazon EC2 執行個體 Connect、工作階段管理員、SSH用戶端或EC2序列主控台。您必須選擇一個並按照其說明進行操作。完成後,選擇「Connect」。
注意
如果您的 IP 位址在您開始本逐步解說後變更,或是稍後再回到您的環境,您必須更新demoEC2
安全性群組輸入規則,以啟用來自新API地址的輸入流量。
第 5 步:安裝蒙戈外殼
您現在可以安裝 mongo 殼層,這是一個命令列公用程式,可用來連接和查詢 Amazon DocumentDB 叢集。請按照以下說明為您的操作系統安裝 mongo 外殼。
第 6 步:管理 Amazon DocumentDB TLS
使用以下代碼下載 Amazon DocumentDB 的 CA 證書:wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
注意
傳輸層安全性 (TLS) 預設會為任何新的 Amazon DocumentDB 叢集啟用。如需詳細資訊,請參閱管理 Amazon DocumentDB 叢集TLS設定。
步驟 7:Connect 到您的 Amazon DocumentDB 集群
-
在 Amazon DocumentDB 主控台的叢集下,找到您的叢集。按一下叢集識別碼,選擇您建立的叢集。
-
在 [連線和安全性] 索引標籤中,找到 [Connect 線] 方塊中使用 mongo 殼層 Connect 線到這個叢集:
複製提供的連接字符串並將其粘貼到終端中。
對其進行下列變更:
請確定您的字串中有正確的使用者名稱。
省略,
<insertYourPassword>
以便在連接時由 mongo shell 提示您輸入密碼。
您的連接字串看起來應該類似下列內容:
mongo --ssl host docdb-2020-02-08-14-15-11. cluster.region.docdb.amazonaws.com:27107 --sslCAFile global-bundle.pem --username demoUser --password
-
在您的終端中按回車鍵。系統會提示您輸入密碼。輸入您的密碼。
-
當您輸入密碼並看到提示時,表
rs0:PRIMARY>
示您已成功連線到 Amazon DocumentDB 叢集。
連接時遇到問題? 請參閱 Amazon DocumentDB 疑難排解。
步驟 8:插入和查詢資料
現在您已連線到叢集,您可以執行一些查詢以熟悉文件資料庫的使用。
-
若要插入單一文件,請輸入下列內容:
db.collection.insert({"hello":"DocumentDB"})
-
您會得到以下輸出:
WriteResult({ "nInserted" : 1 })
-
您可以讀取使用
findOne()
指令撰寫的文件 (因為它只會傳回單一文件)。輸入以下內容:db.collection.findOne()
-
您會得到以下輸出:
{ "_id" : ObjectId("5e401fe56056fda7321fbd67"), "hello" : "DocumentDB" }
-
要執行更多查詢,請考慮遊戲配置文件使用案例。首先,將幾個項目插入到標題為的集合中
profiles
。輸入以下內容:db.profiles.insertMany([ { "_id" : 1, "name" : "Matt", "status": "active", "level": 12, "score":202}, { "_id" : 2, "name" : "Frank", "status": "inactive", "level": 2, "score":9}, { "_id" : 3, "name" : "Karen", "status": "active", "level": 7, "score":87}, { "_id" : 4, "name" : "Katie", "status": "active", "level": 3, "score":27} ])
-
您會得到以下輸出:
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }
-
使用指
find()
令傳回縱斷面集合中的所有文件。輸入以下內容:db.profiles.find()
-
您將獲得與您在步驟 5 中輸入的數據匹配的輸出。
-
使用篩選器查詢單一文件。輸入以下內容:
db.profiles.find({name: "Katie"})
-
你應該得到這個輸出:
{ "_id" : 4, "name" : "Katie", "status": "active", "level": 3, "score":27}
-
現在,讓我們嘗試查找配置文件並使用
findAndModify
命令對其進行修改。我們將使用以下代碼給用戶 Matt 額外的十分:db.profiles.findAndModify({ query: { name: "Matt", status: "active"}, update: { $inc: { score: 10 } } })
-
您會得到以下輸出(請注意,他的分數尚未增加):
{ "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 202 }
-
您可以通過以下查詢驗證他的分數是否已更改:
db.profiles.find({name: "Matt"})
-
您會得到以下輸出:
{ "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 212 }
步驟 9:探索
恭喜您!您已成功完成 Amazon DocumentDB 快速入門指南。
下一步是什麼? 了解如何充分利用這個功能強大的數據庫以及其一些熱門功能:
注意
若要節省成本,您可以停止 Amazon DocumentDB 叢集以降低成本,也可以刪除叢集。根據預設,在閒置 30 分鐘後,您的 AWS Cloud9 環境將停止基礎 Amazon EC2 執行個體。