如何在 Linux VM 上安装和配置 MongoDBHow to install and configure MongoDB on a Linux VM

MongoDB 是一个流行的开源、高性能 NoSQL 数据库。MongoDB is a popular open-source, high-performance NoSQL database. 本文介绍如何使用 Azure CLI 在 Linux VM 上安装和配置 MongoDB。This article shows you how to install and configure MongoDB on a Linux VM with the Azure CLI. 文中提供了一些示例,详细说明如何执行以下操作:Examples are shown that detail how to:

在 VM 上手动安装和配置 MongoDBManually install and configure MongoDB on a VM

MongoDB 为 CentOS、SUSE、Ubuntu 和 Debian 等 Linux 发行版提供安装说明MongoDB provide installation instructions for Linux distros including CentOS, SUSE, Ubuntu, and Debian. 以下示例创建 CentOS VM。**The following example creates a CentOS VM. 若要创建此环境,需要安装最新的 Azure CLI,并使用 az login 登录到 Azure 帐户。To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

使用 az group create 创建资源组。Create a resource group with az group create. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

使用 az vm create 创建 VM。Create a VM with az vm create. 以下示例通过名为 azureuser 的用户使用 SSH 公钥身份验证创建名为 myVM 的 VMThe following example creates a VM named myVM with a user named azureuser using SSH public key authentication

az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image CentOS \
    --admin-username azureuser \
    --generate-ssh-keys

使用你自己的用户名和上一步骤中的输出中列出的 publicIpAddress 以 SSH 方式登录到 VM:SSH to the VM using your own username and the publicIpAddress listed in the output from the previous step:

ssh azureuser@<publicIpAddress>

若要为 MongoDB 添加安装源,请按如下所示创建一个 yum 存储库文件:****To add the installation sources for MongoDB, create a yum repository file as follows:

sudo touch /etc/yum.repos.d/mongodb-org-3.6.repo

打开该 MongoDB 存储库文件进行编辑,例如使用 vinanoOpen the MongoDB repo file for editing, such as with vi or nano. 添加以下行:Add the following lines:

[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

按如下所示使用 yum 安装 MongoDB:****Install MongoDB using yum as follows:

sudo yum install -y mongodb-org

默认情况下,在阻止访问 MongoDB 的 CentOS 映像中强制实施 SELinux。By default, SELinux is enforced on CentOS images that prevents you from accessing MongoDB. 安装策略管理工具并配置 SELinux,让 MongoDB 在其默认的 TCP 端口 27017 上运行,如下所示:Install policy management tools and configure SELinux to allow MongoDB to operate on its default TCP port 27017 as follows:

sudo yum install -y policycoreutils-python
sudo semanage port -a -t mongod_port_t -p tcp 27017

按如下所示启动 MongoDB 服务:Start the MongoDB service as follows:

sudo service mongod start

通过使用本地 mongo 客户端进行连接来验证 MongoDB 安装:Verify the MongoDB installation by connecting using the local mongo client:

mongo

现在,通过添加一些数据并执行搜索来测试 MongoDB 实例:Now test the MongoDB instance by adding some data and then searching:

> db
test
> db.foo.insert( { a : 1 } )  
> db.foo.find()  
{ "_id" : ObjectId("57ec477cd639891710b90727"), "a" : 1 }
> exit

如果需要,可将 MongoDB 配置为在系统重新引导期间自动启动:If desired, configure MongoDB to start automatically during a system reboot:

sudo chkconfig mongod on

使用模板在 CentOS 上创建基本 MongoDB 实例Create basic MongoDB instance on CentOS using a template

可以使用 GitHub 中的以下 Azure 快速入门模板,在单个 CentOS VM 上创建基本的 MongoDB 实例。You can create a basic MongoDB instance on a single CentOS VM using the following Azure quickstart template from GitHub. 此模板使用适用于 Linux 的自定义脚本扩展将 yum 存储库添加到新建的 CentOS VM,然后安装 MongoDB。****This template uses the Custom Script extension for Linux to add a yum repository to your newly created CentOS VM and then install MongoDB.

若要创建此环境,需要安装最新的 Azure CLI,并使用 az login 登录到 Azure 帐户。To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login. 首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

接下来,使用 az group deployment create 部署 MongoDB 模板。Next, deploy the MongoDB template with az group deployment create. 出现提示时,为 newStorageAccountName**、dnsNameForPublicIP** 以及管理员用户名和密码提供自己的唯一值:When prompted, enter your own unique values for newStorageAccountName, dnsNameForPublicIP, and admin username and password:

az group deployment create --resource-group myResourceGroup \
  --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-on-centos/azuredeploy.json

使用 VM 的公共 DNS 地址登录到 VM。Log on to the VM using the public DNS address of your VM. 可以使用 az vm show 查看公共 DNS 地址:You can view the public DNS address with az vm show:

az vm show -g myResourceGroup -n myLinuxVM -d --query [fqdns] -o tsv

使用自己的用户名和公共 DNS 地址 SSH 连接到 VM:SSH to your VM using your own username and public DNS address:

ssh azureuser@mypublicdns.chinaeast.cloudapp.chinacloudapi.cn

如下所示,通过使用本地 mongo 客户端进行连接来验证 MongoDB 安装:Verify the MongoDB installation by connecting using the local mongo client as follows:

mongo

现在,按如下所示,通过添加一些数据并执行搜索来测试实例:Now test the instance by adding some data and searching as follows:

> db
test
> db.foo.insert( { a : 1 } )  
> db.foo.find()  
{ "_id" : ObjectId("57ec477cd639891710b90727"), "a" : 1 }
> exit

使用模板在 CentOS 上创建复杂的 MongoDB 分片群集Create a complex MongoDB Sharded Cluster on CentOS using a template

可以使用 Github 中的以下 Azure 快速入门模板创建复杂的 MongoDB 分片群集。You can create a complex MongoDB sharded cluster using the following Azure quickstart template from GitHub. 此模板遵循 MongoDB 分片群集最佳实践来提供冗余和高可用性。This template follows the MongoDB sharded cluster best practices to provide redundancy and high availability. 该模板将创建两个分片,其中每个副本集包含三个节点。The template creates two shards, with three nodes in each replica set. 另外,它还会创建一个配置服务器副本集和两个 mongos 路由器服务器,前者包含 3 个节点的,后者用于确保各分片中的应用程序保持一致。****One config server replica set with three nodes is also created, plus two mongos router servers to provide consistency to applications from across the shards.

警告

部署这种复杂 MongoDB 分片群集需要 20 个以上的核心(每个区域中一个订阅的默认核心计数通常为 20 个)。Deploying this complex MongoDB sharded cluster requires more than 20 cores, which is typically the default core count per region for a subscription. 请提出 Azure 支持请求,以增加核心计数。Open an Azure support request to increase your core count.

若要创建此环境,需要安装最新的 Azure CLI,并使用 az login 登录到 Azure 帐户。To create this environment, you need the latest Azure CLI installed and logged in to an Azure account using az login. 首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在“chinaeast” 位置创建名为“myResourceGroup” 的资源组:The following example creates a resource group named myResourceGroup in the chinaeast location:

az group create --name myResourceGroup --location chinaeast

接下来,使用 az group deployment create 部署 MongoDB 模板。Next, deploy the MongoDB template with az group deployment create. 根据需要定义自己的资源名称和大小,例如针对 mongoAdminUsername、sizeOfDataDiskInGB 和 configNodeVmSize:******Define your own resource names and sizes where needed such as for mongoAdminUsername, sizeOfDataDiskInGB, and configNodeVmSize:

az group deployment create --resource-group myResourceGroup \
  --parameters '{"adminUsername": {"value": "azureuser"},
    "adminPassword": {"value": "P@ssw0rd!"},
    "mongoAdminUsername": {"value": "mongoadmin"},
    "mongoAdminPassword": {"value": "P@ssw0rd!"},
    "dnsNamePrefix": {"value": "mypublicdns"},
    "environment": {"value": "AzureChinaCloud"},
    "numDataDisks": {"value": "4"},
    "sizeOfDataDiskInGB": {"value": 20},
    "centOsVersion": {"value": "7.0"},
    "routerNodeVmSize": {"value": "Standard_DS3_v2"},
    "configNodeVmSize": {"value": "Standard_DS3_v2"},
    "replicaNodeVmSize": {"value": "Standard_DS3_v2"},
    "zabbixServerIPAddress": {"value": "Null"}}' \
  --template-uri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/mongodb-sharding-centos/azuredeploy.json \
  --name myMongoDBCluster \
  --no-wait

此部署可能需要一个多小时来部署和配置所有 VM 实例。This deployment can take over an hour to deploy and configure all the VM instances. 上述命令末尾使用了 --no-wait 标志,在 Azure 平台接受模板部署后将控制权返回给命令提示符。The --no-wait flag is used at the end of the preceding command to return control to the command prompt once the template deployment has been accepted by the Azure platform. 然后,可以使用 az group deployment show 查看部署状态。You can then view the deployment status with az group deployment show. 以下示例查看 myResourceGroup 资源组中 myMongoDBCluster 部署的状态:****The following example views the status for the myMongoDBCluster deployment in the myResourceGroup resource group:

az group deployment show \
    --resource-group myResourceGroup \
    --name myMongoDBCluster \
    --query [properties.provisioningState] \
    --output tsv

后续步骤Next steps

在上述示例中,已在本地从 VM 连接到 MongoDB 实例。In these examples, you connect to the MongoDB instance locally from the VM. 如果想要从另一个 VM 或网络连接到 MongoDB 实例,请确保创建相应的网络安全组规则If you want to connect to the MongoDB instance from another VM or network, ensure the appropriate Network Security Group rules are created.

这些示例部署用于开发的核心 MongoDB 环境。These examples deploy the core MongoDB environment for development purposes. 请为你的环境应用必需的安全配置选项。Apply the required security configuration options for your environment. 有关详细信息,请参阅 MongoDB 安全文档For more information, see the MongoDB security docs.

有关使用模板创建这些规则的详细信息,请参阅 Azure 资源管理器概述For more information about creating using templates, see the Azure Resource Manager overview.

Azure 资源管理器模板使用自定义脚本扩展在 VM 上下载并执行脚本。The Azure Resource Manager templates use the Custom Script Extension to download and execute scripts on your VMs. 有关详细信息,请参阅在 Linux 虚拟机上使用 Azure 自定义脚本扩展For more information, see Using the Azure Custom Script Extension with Linux Virtual Machines.