EC2手動 Connect Amazon - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

EC2手動 Connect Amazon

下列步驟假設您已完成必要條件主題中的步驟。

步驟 1:創建一個 Amazon EC2 實例

在此步驟中,您將在稍後用來佈建 EC2 Amazon DocumentDB 叢集的同一VPC個區域和 Amazon 執行個體建立一個 Amazon 執行個體。

  1. 在 Amazon 主EC2控台上,選擇啟動執行個體

    Launch instance button with dropdown and Migrate a server option in AWS EC2 interface.
  2. 在「名稱和標籤」區段的「名稱」欄位中輸入名稱或識別碼。

  3. Amazon 機器映像 (AMI) 下拉列表中,找到 Amazon Linux 2 AMI 並選擇它。

    Application and OS Images selection interface with Quick Start options for various operating systems.
  4. 在「執行個體類型」下拉式清單中找到並選擇 t3.micro

    Instance type dropdown showing t3.micro with 2 vCPU, 1 GiB memory, and pricing details.
  5. 在 [key pair (登入)] 區段中,輸入現有金鑰配對的識別碼,或選擇 [建立新金鑰配對]。

    Key pair selection dropdown with option to create a new key pair for secure instance connection.

    您必須提供一個 Amazon EC2 key pair。

    如果你有一個 Amazon EC2 key pair:

    1. 選取 key pair,然後從清單中選擇您的 key pair。

    2. 您必須已經擁有可用的私密金鑰檔案 (.pem 或 .ppk 檔案) 才能登入 Amazon 執行個體。EC2

    如果您沒有 Amazon EC2 key pair:

    1. 選擇建立新 key pair建立 key pair 話方塊便會顯示。

    2. 金鑰配對名稱欄位中輸入名稱

    3. 選擇密鑰對類型私鑰文件格式

    4. 選擇 Create key pair (建立金鑰對)

    Create key pair dialog with options for name, type, and private key file format.
    注意

    為了安全起見,我們強烈建議您同時使用金鑰配對,SSH並將網際網路連線到您EC2的執行個體。

  6. 在 [網路設定] 區段的 [防火牆 (安全性群組)] 下,選擇 [建立安全性群] 或 [選取現有的安全性群組]。

    Network settings interface showing VPC, subnet, IP, and security group options.

    如果您選擇選取現有的安全性群組,請從 [一般安全性群組] 下拉式清單中選取一個群組

    如果您選擇建立新的安全性群組,請執行下列動作:

    1. 檢查所有適用於EC2連線的流量允許規則。

    2. 在 [IP] 欄位中,選擇 [我的 IP] 或選取 [自訂] 以從CIDR封鎖、首碼清單或安全群組清單中選擇。除非您的EC2執行個體位於隔離的網路上,否則我們不建議您選擇 Anywhere,因為它允許任何 IP 位址存取您的EC2執行個體。

    Text field displaying an IP address with a dropdown arrow.
  7. 在 [摘要] 區段中,檢閱您的EC2組態,並選擇 [啟動執行個體 (如果正確 編輯安全性群組

    EC2 instance summary showing configuration details and free tier information.

步驟 2:建立安全群組

現在,您將在默認 Amazon 中創建一個新的安全組VPC。安全群組demoDocDB可讓您從 Amazon 執行個體連接到連接埠 27017 (亞馬遜文件資料庫的預設連接埠) 上的 Amazon DocumentDB 叢集。EC2

  1. Amazon EC2 管理主控台網路和安全性下,選擇安全群組

    Navigation menu showing Network & Security options including Security Groups.
  2. 選擇建立安全群組

    Orange button labeled "Create security group" for initiating the process.
  3. 在「基本詳細資料」區段中:

    1. 針對 Security group name (安全群組名稱),輸入 demoDocDB

    2. 描述中,輸入描述。

    3. 對於 VPC,接受使用您的預設值VPC。

    Security group configuration form with name, description, and VPC selection fields.
  4. Inbound rules (傳入規則) 區段中,選擇 Add rule (新增規則)

    1. 選擇「自訂TCP規則」做為「類型」。

    2. 對於連接埠範圍,輸入27017

    3. 選擇「自訂」做為「目的地」 在旁邊的欄位中,搜尋您剛才呼叫的安全性群組demoEC2。您可能需要重新整理瀏覽器,Amazon 主EC2控台才能自動填入demoEC2來源名稱。

    Inbound rules configuration interface showing custom TCP rule for port 27017.
  5. 接受所有其他預設值並選擇建立安全性群組

    Orange button labeled "Create security group" for creating a new security group.

第 3 步:創建一個 Amazon DocumentDB 集群

在佈建亞馬遜EC2執行個體時,您將建立您的 Amazon DocumentDB 叢集。

  1. 導覽至 Amazon DocumentDB 主控台,然後從導覽窗格中選擇叢集

    Navigation pane showing Dashboard, Clusters, Performance Insights, and Snapshots options.
  2. 選擇 Create (建立)。

    Orange button labeled "Create" for initiating a new item or process.
  3. 將 [叢集類型] 設定保留為 [執行個體型叢集] 的預設值。

    叢集 type selection with Instance Based 叢集 option highlighted.
  4. 針對執行個體數目,選擇 1。這將使成本降至最低。將其他設定保留為預設值。

    Configuration form with 叢集 identifier, engine version, instance class, and number of instances.
  5. 對於 [連],保留 [不要連線至EC2計算資源] 的預設設定。

    Connectivity options for compute resources, with "Don't connect to an EC2 compute resource" selected.
    注意

    連線至EC2計算資源會自動為您與叢集的EC2計算資源連線建立安全群組。由於您在上一個步驟中手動建立這些安全性群組,因此您應該選取 [不連線至EC2計算資源],以免建立第二組安全性群組。

  6. 對於「驗證」,請輸入登入認證。重要:在稍後的步驟中,您將需要登入認證才能驗證叢集。

    Authentication form with username and password fields, including input requirements.
  7. 開啟 [顯示進階設定]

    Estimated hourly cost for DocumentDB instance with toggle for advanced settings and Create 叢集 button.
  8. 在「網路設定」區段中,對於 Amazon VPC 安全群組,請選擇「demoDoc資料庫」。

    Network settings panel showing VPC, subnet group, and security group options for a 叢集.
  9. 選擇建立叢集

    Orange button labeled "Create 叢集" for initiating 叢集 creation process.

步驟 4:Connect 到您的 Amazon EC2 實例

要安裝蒙戈外殼,您必須首先連接到您的 Amazon EC2 實例。安裝蒙戈外殼使您能夠連接和查詢您的 Amazon DocumentDB 集群。請完成下列步驟:

  1. 在 Amazon EC2 主控台上,瀏覽至您的執行個體,並查看您剛建立的執行個體是否正在執行。如果是,請按一下執行個體 ID 來選取執行個體。

    EC2 console showing two instances, one stopped and one running, with details like ID and type.
  2. 選擇連線

    Instance summary showing details like ID, IP addresses, state, and other configuration information.
  3. 您的連線方法有四個索引標籤選項:Amazon EC2 執行個體 Connect、工作階段管理員、SSH用戶端或EC2序列主控台。您必須選擇一個並按照其說明進行操作。完成後,選擇「Connect」。

    EC2 Instance Connect interface showing connection options and instance details.
注意

如果您的 IP 位址在您開始本逐步解說後變更,或是稍後再回到您的環境,您必須更新demoEC2安全性群組輸入規則,以啟用來自新API地址的輸入流量。

第 5 步:安裝蒙戈外殼

您現在可以安裝 mongo 殼層,這是一個命令列公用程式,可用來連接和查詢 Amazon DocumentDB 叢集。請按照以下說明為您的操作系統安裝 mongo 外殼。

On Amazon Linux
在 Amazon Linux 上安裝 mongo 殼層
  1. 建立儲存庫檔案。在執行EC2個體的命令列中,執行下列指令:

    echo -e "[mongodb-org-5.0] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/5.0/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-5.0.repo
  2. 當它完成時,通過執行以下命令安裝 mongo 外殼:

    sudo yum install -y mongodb-org-shell
On Ubuntu 18.04
在 Ubuntu 18.04 上安裝 mongo 殼層
  1. 匯入公開金鑰供套件管理系統使用。

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
  2. 以適用於您 Ubuntu 版本的命令來為 MongoDB 建立清單檔案 /etc/apt/sources.list.d/mongodb-org-3.6.list

    Ubuntu 18.04

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
    注意

    上述命令將安裝 Bionic and Xenial 的 mongo 3.6 殼層。

  3. 使用下列命令重新載入本機套件資料庫:

    sudo apt-get update
  4. 安裝 MongoDB 殼層。

    sudo apt-get install -y mongodb-org-shell

如需在您的 Ubuntu 系統上安裝舊版 MongoDB 的詳細資訊,請參閱在 Ubuntu 上安裝 MongoDB Community 版本

 

On other operating systems

若要在其他作業系統安裝 mongo 殼層,請參閱 MongoDB 文件中的安裝 MongoDB Community 版本

第 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 集群

  1. 在 Amazon DocumentDB 主控台的叢集下,找到您的叢集。按一下叢集識別碼,選擇您建立的叢集

    DocumentDB 叢集 list showing one 叢集 with identifier, role, version, region, and status.
  2. 在 [連線和安全性] 索引標籤中,找到 [Connect 線] 方塊中使用 mongo 殼層 Connect 線到這個叢集

    Command to connect to a MongoDB 叢集 using mongo shell with SSL and authentication details.

    複製提供的連接字符串並將其粘貼到終端中。

    對其進行下列變更:

    1. 請確定您的字串中有正確的使用者名稱。

    2. 省略,<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

  3. 在您的終端中按回車鍵。系統會提示您輸入密碼。輸入您的密碼。

  4. 當您輸入密碼並看到提示時,表rs0:PRIMARY>示您已成功連線到 Amazon DocumentDB 叢集。

連接時遇到問題? 請參閱 Amazon DocumentDB 疑難排解

步驟 8:插入和查詢資料

現在您已連線到叢集,您可以執行一些查詢以熟悉文件資料庫的使用。

  1. 若要插入單一文件,請輸入下列內容:

    db.collection.insert({"hello":"DocumentDB"})
  2. 您會得到以下輸出:

    WriteResult({ "nInserted" : 1 })

  3. 您可以讀取使用findOne()指令撰寫的文件 (因為它只會傳回單一文件)。輸入以下內容:

    db.collection.findOne()
  4. 您會得到以下輸出:

    { "_id" : ObjectId("5e401fe56056fda7321fbd67"), "hello" : "DocumentDB" }

  5. 要執行更多查詢,請考慮遊戲配置文件使用案例。首先,將幾個項目插入到標題為的集合中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} ])
  6. 您會得到以下輸出:

    { "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }

  7. 使用指find()令傳回縱斷面集合中的所有文件。輸入以下內容:

    db.profiles.find()
  8. 您將獲得與您在步驟 5 中輸入的數據匹配的輸出。

  9. 使用篩選器查詢單一文件。輸入以下內容:

    db.profiles.find({name: "Katie"})
  10. 你應該得到這個輸出:

    { "_id" : 4, "name" : "Katie", "status": "active", "level": 3, "score":27}

  11. 現在,讓我們嘗試查找配置文件並使用findAndModify命令對其進行修改。我們將使用以下代碼給用戶 Matt 額外的十分:

    db.profiles.findAndModify({ query: { name: "Matt", status: "active"}, update: { $inc: { score: 10 } } })
  12. 您會得到以下輸出(請注意,他的分數尚未增加):

    { "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 202 }
  13. 您可以通過以下查詢驗證他的分數是否已更改:

    db.profiles.find({name: "Matt"})

  14. 您會得到以下輸出:

    { "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 212 }

步驟 9:探索

恭喜您!您已成功完成 Amazon DocumentDB 快速入門指南。

下一步是什麼? 了解如何充分利用這個功能強大的數據庫以及其一些熱門功能:

注意

若要節省成本,您可以停止 Amazon DocumentDB 叢集以降低成本,也可以刪除叢集。根據預設,在閒置 30 分鐘後,您的 AWS Cloud9 環境將停止基礎 Amazon EC2 執行個體。