Azure Database for MySQL のデータベースモニタリングの設定

データベースモニタリングは、InnoDB ストレージエンジンのクエリメトリクス、クエリサンプル、説明プラン、接続データ、システムメトリクス、テレメトリを公開することにより、MySQL データベースの詳細な可視性を提供します。

Agent は、読み取り専用のユーザーとしてログインすることでデータベースから直接テレメトリーを収集します。MySQL データベースでデータベースモニタリングを有効にするには、以下の手順を行ってください。

  1. データベースのパラメーターを構成する
  2. Agent にデータベースへのアクセスを付与する
  3. Agent のインストールと構成
  4. Azure MySQL インテグレーションをインストールする

はじめに

サポート対象の MySQL バージョン
5.7 または 8.0+
サポートされる Azure MySQL デプロイメントタイプ
MySQL on Azure VMs、Single Server、Flexible Server (Flexible Server では、Query Activity と Wait Event の収集はサポートされていません)
サポート対象の Agent バージョン
7.36.1+
パフォーマンスへの影響
データベースモニタリングのデフォルトの Agent コンフィギュレーションは保守的ですが、収集間隔やクエリのサンプリングレートなどの設定を調整することで、よりニーズに合ったものにすることができます。ワークロードの大半において、Agent はデータベース上のクエリ実行時間の 1 % 未満、および CPU の 1 % 未満を占めています。

データベースモニタリングは、ベースとなる Agent 上のインテグレーションとして動作します (ベンチマークを参照してください)。
プロキシ、ロードバランサー、コネクションプーラー
Datadog Agent は、監視対象のホストにできればインスタンスエンドポイントを通じて直接接続する必要があります。Agent は、プロキシ、ロードバランサー、またはコネクションプーラーを介してデータベースに接続すべきではありません。Agent が実行中に異なるホストに接続すると (フェイルオーバーやロードバランシングなどの場合)、Agent は 2 つのホスト間で統計情報の差を計算し、不正確なメトリクスを生成します。
データセキュリティへの配慮
Agent がお客様のデータベースからどのようなデータを収集するか、またそのデータの安全性をどのように確保しているかについては、機密情報を参照してください。

MySQL 設定を構成する

サーバーパラメーターで以下を構成し、サーバーを再起動することで設定が有効になります。

パラメーター説明
performance_schemaON必須。パフォーマンススキーマを有効にします。

Agent は、現在実行中のクエリを収集するために performance_schema.events_statements_* コンシューマが ON に設定されていることも要求します。デフォルトでは、Azure MySQL Database はパフォーマンススキーマコンシューマを有効にするため、追加の構成は必要ありません。

Agent にアクセスを付与する

Datadog Agent が統計やクエリを収集するためには、データベースへの読み取り専用のアクセスが必要となります。

datadog ユーザーを作成し、基本的なアクセス許可を付与します。

CREATE USER datadog@'%' IDENTIFIED by '<UNIQUEPASSWORD>';
ALTER USER datadog@'%' WITH MAX_USER_CONNECTIONS 5;
GRANT REPLICATION CLIENT ON *.* TO datadog@'%';
GRANT PROCESS ON *.* TO datadog@'%';
GRANT SELECT ON performance_schema.* TO datadog@'%';

次のスキーマを作成します。

CREATE SCHEMA IF NOT EXISTS datadog;
GRANT EXECUTE ON datadog.* to datadog@'%';
GRANT CREATE TEMPORARY TABLES ON datadog.* TO datadog@'%';

Agent が説明プランを収集できるようにするには、explain_statement プロシージャを作成します。

DELIMITER $$
CREATE PROCEDURE datadog.explain_statement(IN query TEXT)
    SQL SECURITY DEFINER
BEGIN
    SET @explain := CONCAT('EXPLAIN FORMAT=json ', query);
    PREPARE stmt FROM @explain;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;

さらに、説明プランを収集するすべてのスキーマでこのプロシージャを作成します。<YOUR_SCHEMA> をデータベーススキーマに置き換えます。

DELIMITER $$
CREATE PROCEDURE <YOUR_SCHEMA>.explain_statement(IN query TEXT)
    SQL SECURITY DEFINER
BEGIN
    SET @explain := CONCAT('EXPLAIN FORMAT=json ', query);
    PREPARE stmt FROM @explain;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
GRANT EXECUTE ON PROCEDURE <YOUR_SCHEMA>.explain_statement TO datadog@'%';

Securely store your password

Store your password using secret management software such as Vault. You can then reference this password as ENC[<SECRET_NAME>] in your Agent configuration files: for example, ENC[datadog_user_database_password]. See Secrets Management for more information.

The examples on this page use datadog_user_database_password to refer to the name of the secret where your password is stored. It is possible to reference your password in plain text, but this is not recommended.

Agent のインストールと構成

Azure ホストを監視するには、インフラストラクチャーに Datadog Agent をインストールし、各インスタンスのエンドポイントにリモートで接続するよう構成します。Agent はデータベース上で動作する必要はなく、データベースに接続するだけで問題ありません。ここに記載されていないその他の Agent のインストール方法については、Agent のインストール手順を参照してください。

ホスト上で実行されている Agent のこのチェックを構成するには、次の手順に従ってください。(Agent でデータベースからメトリクスを収集するために小規模な仮想マシンをプロビジョニングする場合など)

Agent のコンフィギュレーションディレクトリのルートにある conf.d/ フォルダーの mysql.d/conf.yaml ファイルを編集して、MySQL メトリクスの収集を開始します。カスタムメトリクスに対するものを含む、使用可能な全コンフィギュレーションオプションの詳細については、サンプル mysql.d/conf.yaml を参照してください。

MySQL メトリクスを収集するには、mysql.d/conf.yaml に次のコンフィギュレーションブロックを追加します。

init_config:

instances:
  - dbm: true
    host: '<AZURE_INSTANCE_ENDPOINT>'
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]' # from the CREATE USER step earlier, stored as a secret

    # After adding your project and instance, configure the Datadog Azure integration to pull additional cloud data such as CPU and Memory.
    azure:
      deployment_type: '<DEPLOYMENT_TYPE>'
      fully_qualified_domain_name: '<AZURE_INSTANCE_ENDPOINT>'

deployment_typename フィールドの設定に関する追加情報は、MySQL インテグレーション仕様を参照してください。

: パスワードに特殊文字が含まれる場合は、単一引用符で囲んでください。

Agent を再起動すると、Datadog への MySQL メトリクスの送信が開始されます。

Docker コンテナで動作するデータベースモニタリング Agent を設定するには、Agent コンテナの Docker ラベルとしてオートディスカバリーのインテグレーションテンプレートを設定します。

: ラベルのオートディスカバリーを機能させるためには、Agent にDocker ソケットに対する読み取り権限が与えられている必要があります。

コマンドライン

次のコマンドを実行して、コマンドラインから Agent を実行します。お使いのアカウントや環境に合わせて値を変更してください。

export DD_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
export DD_AGENT_VERSION=7.36.1

docker run -e "DD_API_KEY=${DD_API_KEY}" \
  -v /var/run/docker.sock:/var/run/docker.sock:ro \
  -l com.datadoghq.ad.check_names='["mysql"]' \
  -l com.datadoghq.ad.init_configs='[{}]' \
  -l com.datadoghq.ad.instances='[{
    "dbm": true,
    "host": "<AZURE_INSTANCE_ENDPOINT>",
    "port": 3306,
    "username": "datadog",
    "password": "<UNIQUEPASSWORD>",
    "azure": {
      "deployment_type": "<DEPLOYMENT_TYPE>",
      "fully_qualified_domain_name": "<AZURE_INSTANCE_ENDPOINT>"
    }
  }]' \
  gcr.io/datadoghq/agent:${DD_AGENT_VERSION}

Dockerfile

Dockerfile ではラベルの指定も可能であるため、インフラストラクチャーのコンフィギュレーションを変更することなく、カスタム Agent を構築・デプロイすることができます。

FROM datadog/agent:7.36.1

LABEL "com.datadoghq.ad.check_names"='["mysql"]'
LABEL "com.datadoghq.ad.init_configs"='[{}]'
LABEL "com.datadoghq.ad.instances"='[{"dbm": true, "host": "<AZURE_INSTANCE_ENDPOINT>", "port": 3306,"username": "datadog","password": "ENC[datadog_user_database_password]", "azure": {"deployment_type": "<DEPLOYMENT_TYPE>", "fully_qualified_domain_name": "<AZURE_INSTANCE_ENDPOINT>"}}]'

deployment_typename フィールドの設定に関する追加情報は、MySQL インテグレーション仕様を参照してください。

Kubernetes クラスターをお使いの場合は、データベースモニタリング用の Datadog Cluster Agent をご利用ください。

Kubernetes クラスターでまだチェックが有効になっていない場合は、手順に従ってクラスターチェックを有効にしてください。MySQL のコンフィギュレーションは、Cluster Agent コンテナにマウントされた静的ファイル、またはサービスアノテーションを使用して宣言できます。

Helm

以下の手順に従って、Kubernetes クラスターに Datadog Cluster Agent をインストールしてください。お使いのアカウントや環境に合わせて値を変更してください。

  1. Helm の Datadog Agent インストール手順に従います。

  2. YAML コンフィギュレーションファイル (Cluster Agent インストール手順の datadog-values.yaml) を更新して、以下を含めます。

    clusterAgent:
      confd:
        mysql.yaml: |-
          cluster_check: true
          init_config:
          instances:
            - dbm: true
              host: '<AZURE_INSTANCE_ENDPOINT>'
              port: 3306
              username: datadog
              password: 'ENC[datadog_user_database_password]'
              azure:
                deployment_type: '<DEPLOYMENT_TYPE>'
                fully_qualified_domain_name: '<AZURE_INSTANCE_ENDPOINT>'      
    
    clusterChecksRunner:
      enabled: true
    
  3. コマンドラインから上記のコンフィギュレーションファイルを使用して Agent をデプロイします。

    helm install datadog-agent -f datadog-values.yaml datadog/datadog
    
Windows の場合は、helm install コマンドに --set targetSystem=windows を追加します。

マウントされたファイルで構成する

マウントされたコンフィギュレーションファイルを使ってクラスターチェックを構成するには、コンフィギュレーションファイルを Cluster Agent コンテナのパス /conf.d/mysql.yaml にマウントします。

cluster_check: true  # Make sure to include this flag
init_config:
instances:
  - dbm: true
    host: '<AZURE_INSTANCE_ENDPOINT>'
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    # After adding your project and instance, configure the Datadog Azure integration to pull additional cloud data such as CPU, Memory, etc.
    azure:
      deployment_type: '<DEPLOYMENT_TYPE>'
      fully_qualified_domain_name: '<AZURE_INSTANCE_ENDPOINT>'

Kubernetes サービスアノテーションで構成する

ファイルをマウントせずに、インスタンスのコンフィギュレーションを Kubernetes サービスとして宣言することができます。Kubernetes 上で動作する Agent にこのチェックを設定するには、Datadog Cluster Agent と同じネームスペースにサービスを作成します。

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    tags.datadoghq.com/env: '<ENV>'
    tags.datadoghq.com/service: '<SERVICE>'
  annotations:
    ad.datadoghq.com/service.check_names: '["mysql"]'
    ad.datadoghq.com/service.init_configs: '[{}]'
    ad.datadoghq.com/service.instances: |
      [
        {
          "dbm": true,
          "host": "<AZURE_INSTANCE_ENDPOINT>",
          "port": 3306,
          "username": "datadog",
          "password": "ENC[datadog_user_database_password]",
          "azure": {
            "deployment_type": "<DEPLOYMENT_TYPE>",
            "fully_qualified_domain_name": "<AZURE_INSTANCE_ENDPOINT>"
          }
        }
      ]      
spec:
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: mysql

deployment_typename フィールドの設定に関する追加情報は、MySQL インテグレーション仕様を参照してください。

Cluster Agent は自動的にこのコンフィギュレーションを登録し、MySQL チェックを開始します。

datadog ユーザーのパスワードをプレーンテキストで公開しないよう、Agent のシークレット管理パッケージを使用し、ENC[] 構文を使ってパスワードを宣言します。

UpdateAzureIntegration

Agent の status サブコマンドを実行し、Checks セクションで mysql を探します。または、データベースのページを参照してください。

Agent の構成例

One agent connecting to multiple hosts

It is common to configure a single Agent host to connect to multiple remote database instances (see Agent installation architectures for DBM). To connect to multiple hosts, create an entry for each host in the MySQL integration config. In these cases, Datadog recommends limiting the number of instances per Agent to a maximum of 10 database instances to guarantee reliable performance.

init_config:
instances:
  - dbm: true
    host: example-service-primary.example-host.com
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: example-service-replica-1.example-host.com
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    options:
      replication: true
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
  - dbm: true
    host: example-service-replica-2.example-host.com
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    options:
      replication: true
    tags:
      - 'env:prod'
      - 'team:team-discovery'
      - 'service:example-service'
    [...]

Running custom queries

To collect custom metrics, use the custom_queries option. See the sample mysql.d/conf.yaml for more details.

init_config:
instances:
  - dbm: true
    host: localhost
    port: 3306
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    custom_queries:
    - query: SELECT age, salary, hours_worked, name FROM hr.employees;
      columns:
        - name: custom.employee_age
          type: gauge
        - name: custom.employee_salary
           type: gauge
        - name: custom.employee_hours
           type: count
        - name: name
           type: tag
      tags:
        - 'table:employees'

Working with hosts through a proxy

If the Agent must connect through a proxy such as the Cloud SQL Auth proxy, all telemetry is tagged with the hostname of the proxy rather than the database instance. Use the reported_hostname option to set a custom override of the hostname detected by the Agent.

init_config:
instances:
  - dbm: true
    host: localhost
    port: 5000
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: example-service-primary
  - dbm: true
    host: localhost
    port: 5001
    username: datadog
    password: 'ENC[datadog_user_database_password]'
    reported_hostname: example-service-replica-1

Azure MySQL インテグレーションをインストールする

Azure からより包括的なデータベースメトリクスを収集するには、MySQL インテグレーションをインストールします (オプション)。

トラブルシューティング

インテグレーションと Agent を手順通りにインストール・設定しても期待通りに動作しない場合は、トラブルシューティングを参照してください。

参考資料

お役に立つドキュメント、リンクや記事:

PREVIEWING: evan.li/clarify-agentless