设置 Agent for SQL Server

Workload Manager for Microsoft SQL Server 使用 Google Cloud的 Agent for SQL Server 来检测和收集元数据,以评估您的 SQL Server 配置。所有运行 SQL Server 的虚拟机都必须安装 Agent for SQL Server。

本文档介绍了如何安装、配置和验证 Google Cloud的 Agent for SQL Server。

安装代理的前提条件

在安装 Agent for SQL Server 之前,您需要确保满足以下前提条件,并创建 SQL Server 工作负载评估

向服务账号授予 IAM 角色

Google Cloud的 Agent for SQL Server 会使用附加到虚拟机的 Identity and Access Management (IAM) 服务账号,以便向 Google Cloud 进行身份验证并获得访问 Google Cloud 资源的权限。对于 Workload Manager 验证指标收集,请使用包含以下 IAM 角色的新服务账号:

如需向您的服务账号添加所需的角色,请按照以下步骤操作:

  1. 在 Google Cloud 控制台中,转到 IAM 页面。

    转到 IAM

  2. 选择您的 Google Cloud 项目。

  3. 确定要向其添加角色的服务账号。

    • 如果该服务账号不在主账号列表中,则表明还没有为它分配任何角色。点击添加,然后输入该服务账号的电子邮件地址。
    • 如果该服务账号已经在主账号列表中,则表明它已具有角色。点击要修改的服务账号对应的修改按钮。
  4. 从可用角色列表中选择所需角色:

    • Compute Engine > Compute Viewer
    • Secret Manager > Secret Manager Secret Accessor
  5. 点击添加保存,将相应角色应用于该服务账号。

启用对 Google Cloud API 的访问权限

Compute Engine 建议将虚拟机实例配置为允许对所有 Cloud API 的所有访问权限范围,并且仅使用实例服务账号的 IAM 权限来控制对资源的访问。 Google Cloud 如需了解详情,请参阅创建使用用户管理的服务账号的虚拟机

如果您确实限制对 Cloud API 的访问,则 Agent for SQL Server 要求主机虚拟机实例上具有以下最低 Cloud API 访问权限范围:

https://www.googleapis.com/auth/cloud-platform

如需了解详情,请参阅镜重最佳实践

如果您是在没有外部 IP 地址的虚拟机实例上运行 SQL Server 应用,则需要对该虚拟机的子网启用专用 Google 访问通道,以便 Agent for SQL Server 可以访问 Google API 和服务。如需了解如何在子网上启用专用 Google 访问通道,请参阅配置专用 Google 访问通道

SQL Server 上所需的权限

使用以下脚本为在代理中配置的用户账号分配所需权限。

    USE [master]
    GO 
GRANT VIEW SERVER STATE TO [user_name] GRANT VIEW ANY DEFINITION TO [user_name]
-- Adds db_datareader role to the user EXEC sp_MSForEachDB ' USE ? IF NOT EXISTS(SELECT * FROM sys.database_principals WHERE name = ''user_name'') BEGIN CREATE USER [user_name] FOR LOGIN [user_name] END EXEC sp_addrolemember ''db_datareader'', ''user_name'' '

安装代理

Windows

在 Windows 上,您可以使用 GooGet 软件包管理命令 googet 安装 Agent for SQL Server。软件包管理命令可完成以下任务:

  • 下载最新版本的 Agent for SQL Server。
  • 创建一项名为 google-cloud-sql-server-agent 的 Windows 服务以及每分钟运行一次的计划任务,以检查服务是否仍在运行,并在必要时重启服务。

如需在虚拟机上安装代理,请按以下步骤操作:

  1. 使用 RDP 连接到 Windows 虚拟机实例。
  2. 以管理员身份从 PowerShell 运行以下命令:
    googet addrepo google-cloud-sql-server-agent-windows  https://packages.cloud.google.com/yuck/repos/google-cloud-sql-server-agent-windows
    googet install google-cloud-sql-server-agent
    
  3. 查看位于 %ProgramFiles%\Google\google-cloud-sql-server-agent 下的配置文件,并使用配置属性中的详细信息更新 credential_configuration 部分。
  4. 重启代理以使此更改生效。

Linux

在 Linux 上,您可以使用标准操作系统软件包管理命令安装 Agent for SQL Server:

  • 在 RHEL 上,使用 yum 命令
  • 在 SLES 上,使用 zypper 命令

软件包管理命令可完成以下任务:

  • 下载最新版本的 Agent for SQL Server。
  • 将该代理创建为一项名为 google-cloud-sql-server-agent 的 Linux systemd 服务。
  • 启用并启动服务 google-cloud-sql-server-agent

如需在虚拟机上安装代理,请按以下步骤操作:

  1. 与主机虚拟机建立 SSH 连接。
  2. 在您的终端中,通过运行特定于您的操作系统的命令来安装代理:

RHEL

sudo tee /etc/yum.repos.d/google-cloud-sql-server-agent.repo << EOM
[google-cloud-sql-server-agent]
name=Google Cloud Agent for SQL Server
baseurl=https://packages.cloud.google.com/yum/repos/google-cloud-sql-server-agent-el$(cat /etc/redhat-release | cut -d . -f 1 | tr -d -c 0-9)
enabled=1
gpgcheck=0
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM
sudo yum install google-cloud-sql-server-agent

SLES 15

sudo zypper addrepo --refresh https://packages.cloud.google.com/yum/repos/google-cloud-sql-server-agent-sles15 google-cloud-sql-server-agent
sudo zypper install google-cloud-sql-server-agent

SLES 12

sudo zypper addrepo --refresh https://packages.cloud.google.com/yum/repos/google-cloud-sql-server-agent-sles12 google-cloud-sql-server-agent
sudo zypper install google-cloud-sql-server-agent
  1. 查看位于 \etc\google-cloud-sql-server-agent 下的配置文件,并使用配置属性中的详细信息更新 credential_configuration 部分。
  2. 重启代理以使此更改生效。

配置属性

下表介绍了代理的配置文件的属性。

属性
collection_configuration.collect_guest_os_metrics

Boolean

如需启用操作系统指标收集,请将值设置为 true。 默认值为 true
除非您收到 Cloud Customer Care 的指示,否则请勿将 collection_configuration.collect_guest_os_metrics 设置为 false

collection_configuration.guest_os_metrics_collection_interval_in_seconds

Int

Workload Manager 操作系统指标收集频率(以秒为单位)。 默认值为 3600 秒。 您可以更新数据收集频率。不过,我们建议您保留默认值。

collection_configuration.collect_sql_metrics

Boolean

如需启用 SQL Server 指标收集功能,请指定 true。 默认值为 true
除非您收到 Cloud Customer Care 的指示,否则请勿将 collection_configuration.collect_sql_metrics 设置为 false

collection_configuration.collect_sql_metrics_collection_interval_in_seconds

Int

Workload Manager SQL Server 指标收集频率(以秒为单位)。 默认值为 3600 秒。 您可以更新数据收集频率。不过,我们建议您保留默认值。

log_level

String

如需设置代理的日志记录级别,请添加日志级别。 可用的日志级别如下:
  • DEBUG
  • INFO
  • WARNING
  • ERROR
默认值为 INFO。除非有 Cloud Customer Care 的指导,否则请勿更改日志记录级别。
log_to_cloud

Boolean

如需将代理的日志重定向到 Cloud Logging,请指定 true。默认值为 true

disable_log_usage

Boolean

如需停用代理运行状况指标日志记录,请指定 true。 默认值为 false

remote_collection

Boolean

指定 true 以指明代理正在执行远程数据收集。 默认值为 false

credential_configuration[].sql_configurations[].user_name

String

指定用于查询 SQL Server 实例的用户账号。 如需配置账号权限,请在 权限脚本中查看所需的权限,并根据您的内部政策授予这些权限。

注意:如果您使用的是 Windows 身份验证,请务必使用以下格式指定用户名: domain-name\\user-name

credential_configuration[].sql_configurations[].host

String

SQL Server 主机名。

credential_configuration[].sql_configurations[].secret_name

String

为了安全地提供代理用于查询 SQL Server 的数据库用户账号的密码,请在 Secret Manager 中指定包含数据库用户账号的安全凭据的 Secret 的名称。

注意:Secret 和主机虚拟机实例必须位于同一 Google Cloud 项目中。

credential_configuration[].sql_configurations[].port_number

Int

指定 SQL Server 实例接收查询的端口。

credential_configuration[].remote_win.server_name

String

远程虚拟机的 IP 地址或 FQDN

credential_configuration[].remote_win.guest_user_name

String

指定用于远程连接到虚拟机的用户账号。

credential_configuration[].remote_win.guest_secret_name

String

为了安全地提供代理用于远程连接到虚拟机的用户账号的密码,请在 Secret Manager 中指定包含该用户账号的安全凭据的 Secret 的名称。

注意:Secret 和主机虚拟机实例必须位于同一 Google Cloud 项目中。

credential_configuration[].remote_linux.server_name

String

远程虚拟机的 IP 地址或 FQDN

credential_configuration[].remote_linux.guest_user_name

String

指定用于远程连接到虚拟机的用户账号。

credential_configuration[].remote_linux.guest_port_number

Int

指定远程虚拟机的 SSH 端口号。

credential_configuration[].remote_linux.linux_ssh_private_key_path

String

SSH 私钥文件的路径。

credential_configuration[].instance_name

String

指定 Compute Engine 虚拟机实例的名称。

注意:对于本地合集,此属性为可选属性。

credential_configuration[].instance_id

String

指定 Compute Engine 虚拟机实例的 ID。

注意:对于本地合集,此属性为可选属性。

以下示例展示了 Agent for SQL Server 的配置文件:

本地收集

{
"collection_configuration": {
  "collect_guest_os_metrics":true,
  "guest_os_metrics_collection_interval_in_seconds":3600,
  "collect_sql_metrics":true,
  "sql_metrics_collection_interval_in_seconds":3600
},
"credential_configuration": [
  {
    "sql_configurations": [
      {
        "host":".",
        "user_name":"db_user_name",
        "secret_name":"db_pwd_secret_name",
        "port_number":1433
      }
    ],
  }
],
"log_level":"INFO",
"log_to_cloud":true,
"collection_timeout_seconds":10,
"max_retries":5,
"retry_interval_in_seconds":3600
}

远程收集

{
"collection_configuration": {
  "collect_guest_os_metrics":true,
  "guest_os_metrics_collection_interval_in_seconds":3600,
  "collect_sql_metrics":true,
  "sql_metrics_collection_interval_in_seconds":3600
},
"credential_configuration": [
  {
    "sql_configurations": [
      {
        "host":"sql_server_instance",
        "user_name":"db_user_name",
        "secret_name":"db_pwd_secret_name",
        "port_number":1433
      }
    ],
    "remote_win":{
      "server_name":"sql_server_instance",
      "guest_user_name":"user_name",
      "guest_secret_name":"pwd_secret_name"
    },
    "instance_name":"db01",
    "instance_id":"9999999999999999999"
  }
],
"remote_collection":true,
"log_level":"INFO",
"log_to_cloud":true,
"collection_timeout_seconds":10,
"max_retries":5,
"retry_interval_in_seconds":3600
}

验证代理安装

Windows

  1. 使用 RDP 连接到 Windows 虚拟机实例。
  2. 以管理员身份从 PowerShell 运行以下命令:

    $(Get-Service -Name 'google-cloud-sql-server-agent' -ErrorAction Ignore).Status

    如果代理正在运行,则状态会显示 Running

Linux

  1. 与主机虚拟机实例建立 SSH 连接。
  2. 运行以下命令:

    systemctl status google-cloud-sql-server-agent

    如果代理正常运行,则输出包含 active (running)。例如:

    google-cloud-sql-server-agent.service - Google Cloud Agent for SQL Server
    Loaded: loaded (/usr/lib/systemd/system/google-cloud-sql-server-agent.service; enabled; vendor preset: disabled)
    Active: active (running) since Sun 2023-12-31 18:59:12 UTC; 10s ago
    Main PID: 14412 (google_cloud_sq)
      Tasks: 7
    Memory: 12.9M (max: 1.0G limit: 1.0G available: 1011.0M)
    CGroup: /system.slice/google-cloud-sql-server-agent.service
            └─ 14412 /usr/bin/google_cloud_sql_server_agent --action=run
    

检查代理的版本

如需查看代理的版本,请完成以下步骤:

Windows

  1. 使用 RDP 连接到宿主机。
  2. 以管理员身份从 PowerShell 运行以下命令:
    googet installed google-cloud-sql-server-agent

RHEL

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    yum info google-cloud-sql-server-agent

SUSE

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    zypper info google-cloud-sql-server-agent

重启代理

如果 Agent for SQL Server 停止运行或您要更新其配置,请重启该代理。

选择您的操作系统,然后按照以下步骤操作:

Windows

  1. 使用 RDP 连接到宿主机。
  2. 以管理员身份从 PowerShell 运行以下命令:
    Restart-Service -Name 'google-cloud-sql-server-agent' -Force

Linux

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    sudo systemctl restart google-cloud-sql-server-agent

更新代理

为了确保您使用的是最新版本的代理,您需要定期检查更新并更新该代理。

检查更新

选择您的操作系统,然后按照以下步骤操作:

Windows

  1. 使用 RDP 连接到宿主机。
  2. 以管理员身份从 PowerShell 运行以下命令:
    googet latest google-cloud-sql-server-agent

RHEL

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    sudo yum check-update google-cloud-sql-server-agent

SLES

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    sudo zypper list-updates -r google-cloud-sql-server-agent

安装更新

选择您的操作系统,然后按照以下步骤操作:

Windows

  1. 使用 RDP 连接到宿主机。
  2. 以管理员身份从 PowerShell 运行以下命令:
    googet install google-cloud-sql-server-agent

RHEL

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    sudo yum --nogpgcheck update google-cloud-sql-server-agent

SLES

  1. 使用 SSH 连接到宿主机。
  2. 运行以下命令:
    sudo zypper --no-gpg-checks update google-cloud-sql-server-agent

在 Cloud Logging 中查看该代理的日志

默认情况下,Agent for SQL Server 的日志会从虚拟机实例重定向到 Cloud Logging

如需在 Logging 中查看代理的日志,请按以下步骤操作:

  1. 在 Google Cloud 控制台中,转到日志浏览器页面。

    转到日志浏览器

  2. 转到查询窗格。

  3. 资源下拉菜单中选择全局,然后点击应用

  4. 在查询编辑器中,输入 google-cloud-sql-server-agent

  5. 点击运行查询

    您应该会看到在所有虚拟机实例上运行的代理实例生成的日志。如需过滤特定机器的日志,请使用界面中提供的过滤条件。

在 Cloud Logging 中停用代理的日志

如需停用将代理日志默认重定向到 Cloud Logging 的功能,请按以下步骤操作:

  1. 与主机虚拟机实例建立 RDP 或 SSH 连接。

  2. 打开代理的配置文件:

    Windows

    %ProgramFiles%\Google\google-cloud-sql-server-agent\configuration.json

    Linux

    /etc/google-cloud-sql-server-agent/configuration.json

  3. 对于 log_to_cloud 属性,将值更新为 false

  4. 保存配置文件。

  5. 重启代理以使此更改生效。

问题排查

以下部分介绍了与使用 Agent for SQL Server 相关的常见问题及其原因和解决方法。

身份验证范围不足

问题:如果您限制主机虚拟机实例的访问权限范围,则 Agent for SQL Server 日志可能会显示 IAM 权限不足错误。

  googleapi: Error 403: Request had insufficient authentication scopes.
  Details:
  [
    {
      "@type": "type.googleapis.com/google.rpc.ErrorInfo",
      "domain": "googleapis.com",
      "metadata": {
        "method": "google.cloud.workloadmanager.datawarehouse.v1.DataCollectService.WriteInsight",
        "service": "workloadmanager.googleapis.com"
      },
      "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
    }
  ]

More details: Reason: insufficientPermissions, Message: Insufficient Permission

原因:Agent for SQL Server 要求主机虚拟机实例具有最小 Cloud API 访问权限范围。

解决方法:如需解决此问题,请启用必需的访问权限范围

未能加载配置文件

问题:如果配置文件包含无效值,您会看到以下错误。

"Failed to load configuration","pid":3524,"error":"proto: (line 19:42): unknown
field "{field_name}"

解决方法:如需解决此问题,请使用配置属性中的详细信息更新配置文件

未能初始化数据收集

问题:安装代理后,如果未更新配置文件,您会看到以下错误:

"Failed to initialize guest collection","pid":2112,"error":"invalid value for "user_name" "secret_name"

解决方法:如需解决此问题,请使用配置属性初始化凭据配置

后续步骤