简体   繁体   English

无法从 Azure 管道连接到 Azure 数据库

[英]Unable to connect to Azure Database from an Azure pipeline

I've setup and Azure Database for MySQL. I am able to connect fine through the Azure Cloud Shell using我已经为 MySQL 设置了 Azure 数据库。我可以使用 Azure Cloud Shell 正常连接

mysql -h...mysql.database.azure.com --ssl -u web -p

Now what I want to do is to be able to connect to this database from an Azure pipeline.现在我想要做的是能够从 Azure 管道连接到这个数据库。 The purpose is to use the database to track the version numbers for our artifacts.目的是使用数据库来跟踪我们的工件的版本号。

My initial test pipeline yaml looks like我的初始测试管道 yaml 看起来像

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- checkout: none
- bash: |
    mysql -h ...mysql.database.azure.com -u web -p -D applications -N -e "select version, snapshot from releases where name='$1'" | while IFS= read -r value
    do
    echo $value
    done

However, the bash script never completes.但是,bash 脚本永远不会完成。 The indicator simply spins and I have yet to receive a timeout or any type of error.指标只是旋转,我还没有收到超时或任何类型的错误。

Since the database is setup to use SSL, my next iteration of the pipeline looked like由于数据库设置为使用 SSL,我的管道的下一次迭代看起来像

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- checkout: none
- bash: |
    mysql -h ...mysql.database.azure.com --ssl -u web -p -D applications -N -e "select version, snapshot from releases where name='$1'" | while IFS= read -r value
    do
    echo $value
    done

which mirrors the same connection I use in Cloud Shell. However this time I receive the following output这反映了我在 Cloud Shell 中使用的相同连接。但是这次我收到以下 output

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.182.0
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/b5b259c5-bee8-4e95-b1c6-50a98baaf7dc.sh
mysql: [ERROR] unknown option '--ssl'.
Finishing: Bash

Finally I tried downloading the recommended certificate and adding it to the connection最后我尝试下载推荐的证书并将其添加到连接中

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- checkout: none
- task: DownloadSecureFile@1
  inputs:
      secureFile: DigiCertGlobalRootG2.crt.pem
- bash: |
    mysql -h ...mysql.database.azure.com --ssl_cert $(Agent.TempDirectory)/DigiCertGlobalRootG2.crt.pem -u web -p -D applications -N -e "select version, snapshot from releases where name='$1'" | while IFS= read -r value
    do
    echo $value
    done

which again resulted in the task simply sitting there with no output. I'm not sure if this is a firewall issue(meaning I need to add a rule to the database firewall settings)?这再次导致任务只是坐在那里,没有 output。我不确定这是否是防火墙问题(意味着我需要向数据库防火墙设置添加规则)? If this is the case, then how would I determine what range of IP addresses to allow?如果是这种情况,那么我将如何确定允许的 IP 地址范围?

By the way, I already have the following rule set顺便说一句,我已经有以下规则集

  {
    "endIpAddress": "55.226.19.255",
    "id": "/subscriptions/1fa96361-56c7-4090-8823-26ae03e4c38d/resourceGroups/VS-americas-manufacturing-Group/providers/Microsoft.DBforMySQL/servers/mfg-mysql-eus-1/firewallRules/cloudshell",
    "name": "cloudshell",
    "resourceGroup": "VS-americas-manufacturing-Group",
    "startIpAddress": "52.226.19.0",
    "type": "Microsoft.DBforMySQL/servers/firewallRules"
  }

but this doesn't seem to work for the pipelines.但这似乎不适用于管道。

So what setting changes do I need to make, either on the server or in my pipeline, to get this to work?那么我需要在服务器上或管道中进行哪些设置更改才能使其正常工作?

In case someone has the same issue as me and finds this post...如果有人遇到与我相同的问题并找到这篇文章......

Reading the comments on the proposed task I had the same error when using the AzureMysqlDeployment@1 to deploy an ef migrations script:阅读关于建议任务的评论,我在使用 AzureMysqlDeployment@1 部署 ef 迁移脚本时遇到了同样的错误:

"Error: The MySQL server list is empty. The MySQL server host name is invalid." “错误:MySQL 服务器列表为空。MySQL 服务器主机名无效。”

According to the microsoft documentation for the pipeline task it seems that the task is only works for mysql single server database on Azure. Azure recommends creating a flexible server mysql database for all new development, which was the reason for it not to work in my case.根据管道任务的微软文档,该任务似乎仅适用于 Azure 上的 mysql 单服务器数据库。Azure 建议为所有新开发创建一个灵活的服务器 mysql 数据库,这就是它在我的情况下不起作用的原因.

Instead I got it to work by using a codlin task and running a mysql command相反,我通过使用 codlin 任务并运行 mysql 命令来让它工作

- task: CmdLine@2       
  displayName: Run SQL Scripts in MySQL database
  inputs:
  script: 'mysql --host=$(mysql-server) --user=$(mysql-user) //
      --password=$(mysql-password) -D $(mysql-database-name) //
      --execute="source path/to/script.sql"'

If you want to run your scripts and make changes to your database in Azure Database for MySQL.如果要运行脚本并更改 Azure 数据库中的数据库 MySQL。 You could use Azure Database for MySQL Deployment task :您可以将Azure 数据库用于 MySQL 部署任务

# Azure Database for MySQL deployment
# Run your scripts and make changes to your Azure Database for MySQL
- task: AzureMysqlDeployment@1
  inputs:
    ConnectedServiceName: # Or alias azureSubscription
    ServerName:
    #DatabaseName: # Optional
    SqlUsername:
    SqlPassword:
    #TaskNameSelector: 'SqlTaskFile' # Optional. Options: SqlTaskFile, InlineSqlTask
    #SqlFile: # Required when taskNameSelector == SqlTaskFile
    #SqlInline: # Required when taskNameSelector == InlineSqlTask
    #SqlAdditionalArguments: # Optional
    #IpDetectionMethod: 'AutoDetect' # Options: AutoDetect, IPAddressRange
    #StartIpAddress: # Required when ipDetectionMethod == IPAddressRange
    #EndIpAddress: # Required when ipDetectionMethod == IPAddressRange
    #DeleteFirewallRule: true # Optional

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM