在 Azure 虛擬機器上快速搭建 MongoDB 叢集

來源:互聯網
上載者:User

標籤:開始   china   關於   mod   mirrors   總計   過程   模式   ict   

MongoDB 是目前在 NoSQL 市場上非常受歡迎的一個資料庫,本文介紹如何使用 Azure PowerShell 和 Azure CLI 在 Azure 虛擬機器上搭建單節點 MongoDB(測試使用)和包含主從複製以及分區叢集的多節點 MongoDB(生產環境使用)。

 

準備步驟
  • 如果你選擇 Azure PowerShell 方式搭建 MongoDB,那麼請按如何安裝和配置 Azure PowerShell中的說明在本機電腦上安裝 Azure PowerShell。然後開啟 Azure PowerShell 命令提示字元,通過運行以下命令並遵循提示進行 Azure 帳戶的互動式登入體驗,來使用工作或學校 ID 登入:

    Login-AzureRmAccount -EnvironmentName AzureChinaCloud

    然後你需要建立一個 Azure 資源群組( Resource Group ),建立 Azure 虛擬機器和搭建 MongoDB 都在該資源群組中進行,運行以下命令建立 Azure 資源群組:

    New-AzureRmResourceGroup -Name "YOUR-RESOURCE-GROUP-NAME" -Location "China East"

     

  • 如果你選擇 Azure CLI 方式搭建 MongoDB,那麼請安裝 Azure CLI。然後請確保你是處於資源管理員模式下,可通過運行以下命令來驗證:

    azure config mode arm

    現在,通過運行以下命令並遵循提示進行 Azure 帳戶的互動式登入體驗,來使用工作或學校 ID 登入:

    azure login -e AzureChinaCloud -u <your account>

    然後你需要建立一個 Azure 資源群組( Resource Group ),建立 Azure 虛擬機器和搭建 MongoDB 都在該資源群組中進行,運行以下命令建立 Azure 資源群組:

    azure group create "YOUR-RESOURCE-GROUP-NAME" "China East"

 

開始動手

Warning

需要以管理員權限運行 PowerShell,使用之前需運行如下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

 

1.在Azure虛擬機器上搭建單節點 MongoDB

預設情況下,以下指令碼將建立1台 CentOS 虛機,構成一個單節點 MongoDB。 以下將主要介紹 Azure PowerShell 方式
你需要下載 PowerShell 指令碼 mongodb-singlenode-deploy.ps1,按照以下樣本運行 mongodb-install-centos.ps1 指令碼,即可在資源群組 rg1 中產生一台 CentOS 虛擬機器,接著會在該虛機上安裝單節點 MongoDB。

PS C:\mongodb> .\mongodb-singlenode-deploy.ps1 -ResourceGroupName rg1 -AdminUserName azureuser -AdminPassword “YOUR-PASSWORD”  -CentosVersion 6.5 -VmName mongodbserver

建立過程大概需要6分鐘,運行成功後會出現如下提示:

Deploy MongoDB on VM mongodb11 (CentOS 6.5) in China North successfully.    To connect using the mongo shell:    % mongo mongodbserver.chinanorth.cloudapp.chinacloudapi.cn:27017/test

 

2. 在Azure虛擬機器上搭建包含主從複製節點的 MongoDB

預設情況下,以下指令碼將建立3台 CentOS 虛機,組成一個 MongoDB 複製集,該複製集將包含一個 Pirmary 節點和2個 Secondary 節點,架構如所示:

 

Azure PowerShell 方式

你需要下載 PowerShell 指令碼 mongodb-replica-set-deploy.ps1,按照以下樣本運行 mongodb-replica-set-deploy.ps1 指令碼,即可在資源群組 rg1 中產生多台 CentOS 虛擬機器,接著會在該虛機上搭建具備主從複製節點的 MongoDB。

PS C:\mongodb>.\mongodb-replica-set-deploy.ps1 -ResourceGroupName rg1 -CentosVersion 7.2 -AdminUsername azureuser -AdminPassword “YOUR-PASSWORD” -MongoUsername mongoadmin -MongoPassword “YOUR-PASSWORD” -DNSNamePrefix mongoreplicaset

建立過程大概需要10分鐘,運行成功後會出現如下提示:

Deploy MongoDB Replica Set successfully.To connect primary node using the mongo shell:% mongo mongoreplicaset.chinanorth.cloudapp.chinacloudapi.cn:27017To connect secondary node0 using the mongo shell:% mongo mongoreplicasetsecondary0.chinanorth.cloudapp.chinacloudapi.cn:27017To connect secondary node1 using the mongo shell:% mongo mongoreplicasetsecondary1.chinanorth.cloudapp.chinacloudapi.cn:27017

 

3. 在 Azure 虛擬機器上搭建 MongoDB 分區叢集

預設情況下,以下指令碼將建立11台 CentOS 虛機,包含:2台 router 伺服器, 3台 config 伺服器,2個分區,每個分區帶有1個 Priamry 節點和2個 Secondary 節點,總計11個節點,架構如所示:

Azure PowerShell 方式

你需要下載PowerShell指令碼 mongodb-sharding-deploy.ps1,按照以下樣本運行 mongodb-sharding-deploy.ps1 指令碼,即可在資源群組rg1中產生多台 CentOS 虛擬機器,接著會在該虛機上搭建具備主從複製節點及分區叢集的 MongoDB,建立過程大概需要1小時15分鐘。

PS C:\mongodb>.\mongodb-sharding-deploy.ps1 -ResourceGroupName rg1 -CentosVersion 7.2 -AdminUsername azureuser -AdminPassword “YOUR-PASSWORD” -MongoUsername mongoadmin -MongoPassword “YOUR-PASSWORD” -DNSNamePrefix mongoshard

 

關於另一種:Azure CLI 方式,感興趣的朋友可以點擊此處進行查看。

 

在 Azure 虛擬機器上快速搭建 MongoDB 叢集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.