文档文档

SQL 输出插件

此插件将指标写入支持的 SQL 数据库,使用简单、硬编码的数据库模式。每个指标类型都有一个表,表名对应指标名称。每个字段都有一个列,每个标签都有一个列,还有一个可选的指标时间戳列。

每条指标写入一行。这意味着即使指标名称、标签和时间戳相同,多个指标也不会合并到一行中。

该插件使用 Golang 的通用“database/sql”接口和第三方驱动程序。有关支持的驱动程序列表和详细信息,请参阅驱动程序特定部分。

引入于: Telegraf v1.19.0 标签: datastore 操作系统支持: all

入门

要使用该插件,请将 driver 设置为适合您数据库的驱动程序名称。然后设置数据源名称 (DSN)。DSN 的格式因驱动程序而异,但通常包括用户名、密码、要使用的数据库实例以及数据库服务器的主机名。用户帐户必须具有插入行和创建表的权限。

生成的 SQL

该插件生成简单的 ANSI/ISO SQL,很可能适用于任何 DBMS。它不使用特定于某个 DBMS 的语言功能。如果您想使用特定于某个 DBMS 的功能,可以尝试在该插件之外手动设置,或通过 init_sql 设置进行设置。

插件生成的插入语句使用占位符参数。大多数数据库驱动程序使用问号作为占位符,但 postgres 使用带索引的美元符号。插件根据所选驱动程序选择要使用的占位符样式。

由于输入插件的性质,给定度量写入行中的列数可能有所不同。由于表是根据输入度量中可用的标签和字段创建的,因此创建的表可能不包含所有必需的列。如果您希望自动化表更新,请参阅“Schema 更新”。

度量类型到 SQL 列类型的映射可以通过 convert 设置进行自定义。

如果您的数据库服务器支持 Prepared Statements / Batch / Bulk Inserts,您可以通过启用 batch_transactions 来提高摄取速率。

全局配置选项

插件支持其他全局和插件配置设置,用于修改指标、标签和字段,创建别名以及配置插件顺序等任务。更多详情请参阅 CONFIGURATION.md

Secret-store 支持

此插件支持来自 secret-stores 的 data_source_name 选项的 secret。有关如何使用它们的更多详细信息,请参阅 secret-store 文档

配置

# Save metrics to an SQL Database
[[outputs.sql]]
  ## Database driver
  ## Valid options: mssql (Microsoft SQL Server), mysql (MySQL), pgx (Postgres),
  ##  sqlite (SQLite3), snowflake (snowflake.com) clickhouse (ClickHouse)
  driver = ""

  ## Data source name
  ## The format of the data source name is different for each database driver.
  ## See the plugin readme for details.
  data_source_name = ""

  ## Timestamp column name, set to empty to ignore the timestamp
  # timestamp_column = "timestamp"

  ## Table creation template
  ## Available template variables:
  ##  {TABLE} - table name as a quoted identifier
  ##  {TABLELITERAL} - table name as a quoted string literal
  ##  {COLUMNS} - column definitions (list of quoted identifiers and types)
  ##  {TAG_COLUMN_NAMES} - tag column definitions (list of quoted identifiers)
  ##  {TIMESTAMP_COLUMN_NAME} - the name of the time stamp column, as configured above
  # table_template = "CREATE TABLE {TABLE}({COLUMNS})"
  ## NOTE: For the clickhouse driver the default is:
  # table_template = "CREATE TABLE {TABLE}({COLUMNS}) ORDER BY ({TAG_COLUMN_NAMES}, {TIMESTAMP_COLUMN_NAME})"

  ## Table existence check template
  ## Available template variables:
  ##  {TABLE} - tablename as a quoted identifier
  # table_exists_template = "SELECT 1 FROM {TABLE} LIMIT 1"

  ## Table update template, available template variables:
  ##  {TABLE} - table name as a quoted identifier
  ##  {COLUMN} - column definition (quoted identifier and type)
  ## NOTE: Ensure the user (you're using to write to the database) has necessary permissions
  ##
  ## Use the following setting for automatically adding columns:
  ## table_update_template = "ALTER TABLE {TABLE} ADD COLUMN {COLUMN}"
  # table_update_template = ""

  ## Initialization SQL
  # init_sql = ""

  ## Send metrics with the same columns and the same table as batches using prepared statements
  # batch_transactions = false

  ## Maximum amount of time a connection may be idle. "0s" means connections are
  ## never closed due to idle time.
  # connection_max_idle_time = "0s"

  ## Maximum amount of time a connection may be reused. "0s" means connections
  ## are never closed due to age.
  # connection_max_lifetime = "0s"

  ## Maximum number of connections in the idle connection pool. 0 means unlimited.
  # connection_max_idle = 2

  ## Maximum number of open connections to the database. 0 means unlimited.
  # connection_max_open = 0

  ## NOTE: Due to the way TOML is parsed, tables must be at the END of the
  ## plugin definition, otherwise additional config options are read as part of
  ## the table

  ## Metric type to SQL type conversion
  ## The values on the left are the data types Telegraf has and the values on
  ## the right are the data types Telegraf will use when sending to a database.
  ##
  ## The database values used must be data types the destination database
  ## understands. It is up to the user to ensure that the selected data type is
  ## available in the database they are using. Refer to your database
  ## documentation for what data types are available and supported.
  #[outputs.sql.convert]
  #  integer              = "INT"
  #  real                 = "DOUBLE"
  #  text                 = "TEXT"
  #  timestamp            = "TIMESTAMP"
  #  defaultvalue         = "TEXT"
  #  unsigned             = "UNSIGNED"
  #  bool                 = "BOOL"
  #  ## This setting controls the behavior of the unsigned value. By default the
  #  ## setting will take the integer value and append the unsigned value to it. The other
  #  ## option is "literal", which will use the actual value the user provides to
  #  ## the unsigned option. This is useful for a database like ClickHouse where
  #  ## the unsigned value should use a value like "uint64".
  #  # conversion_style = "unsigned_suffix"

Schema 更新

此插件的默认行为是根据当前度量(包括字段和标签)为表创建 schema。但是,写入具有附加字段或标签的后续度量将导致错误。

如果您希望插件同步每个度量的列 schema,请在配置文件中指定 table_update_template 设置。

数据库可能(或可能不支持)。请查阅您的数据库文档以了解正确的语法和表/列选项。

# Save metrics to an SQL Database
[[outputs.sql]]
  ## Table update template
  table_update_template = "ALTER TABLE {TABLE} ADD COLUMN {COLUMN}"

特定于驱动程序的信息

go-sql-driver/mysql

MySQL 的默认引用与标准的 ANSI/ISO SQL 引用不同。您必须将此插件与 MySQL 的 ANSI_QUOTES 模式一起使用。您可以通过使用 init_sql = "SET sql_mode='ANSI_QUOTES';" 设置来启用此模式,或在运行 MySQL 时通过命令行选项启用。有关 ANSI_QUOTES 的详细信息以及如何设置 SQL 模式,请参阅 MySQL 的文档 在此在此

您可以使用格式为“username:password@tcp(host:port)/dbname”的 DSN。有关详细信息,请参阅 驱动程序文档

jackc/pgx

您可以使用格式为“postgres://username:password@host:port/dbname”的 DSN。有关更多详细信息,请参阅 驱动程序文档

modernc.org/sqlite

在 windows/386、mips 和 mips64 平台上不支持。

DSN 是一个文件名或具有“file:”方案的 URL。有关详细信息,请参阅 驱动程序文档

clickhouse

DSN

请注意,即使 DSN 指定为 https://,仍然需要 secure=true 参数。

该插件现在使用 clickhouse-go v2。如果您仍在使用与 v1 兼容的 DSN,它将尝试将 DSN 转换为新格式,但由于两个 schema 并非完全等效,某些参数可能不再有效。请检查日志文件中的警告,并参阅 v2 DSN 文档了解可用选项。

度量类型到 SQL 类型转换

以下配置使映射与 Clickhouse 兼容

  [outputs.sql.convert]
    conversion_style     = "literal"
    integer              = "Int64"
    text                 = "String"
    timestamp            = "DateTime"
    defaultvalue         = "String"
    unsigned             = "UInt64"
    bool                 = "UInt8"
    real                 = "Float64"

有关更多信息,请参阅 ClickHouse 数据类型

microsoft/go-mssqldb

Telegraf 没有 go-mssqldb 的单元测试,因此应将其视为实验性的。

snowflakedb/gosnowflake

Telegraf 没有 gosnowflake 的单元测试,因此应将其视为实验性的。


此页面是否有帮助?

感谢您的反馈!


InfluxDB 3.8 新特性

InfluxDB 3.8 和 InfluxDB 3 Explorer 1.6 的主要增强功能。

查看博客文章

InfluxDB 3.8 现已适用于 Core 和 Enterprise 版本,同时发布了 InfluxDB 3 Explorer UI 的 1.6 版本。本次发布着重于操作成熟度,以及如何更轻松地部署、管理和可靠地运行 InfluxDB。

更多信息,请查看

InfluxDB Docker 的 latest 标签将指向 InfluxDB 3 Core

在 **2026 年 2 月 3 日**,InfluxDB Docker 镜像的 latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。

如果使用 Docker 来安装和运行 InfluxDB,latest 标签将指向 InfluxDB 3 Core。为避免意外升级,请在您的 Docker 部署中使用特定的版本标签。例如,如果使用 Docker 运行 InfluxDB v2,请将 latest 版本标签替换为 Docker pull 命令中的特定版本标签 — 例如

docker pull influxdb:2