MySQL 节点常见问题#
¥MySQL node common issues
以下是 MySQL 节点 的一些常见错误和问题,以及解决或故障排除步骤。
¥Here are some common errors and issues with the MySQL node and steps to resolve or troubleshoot them.
按复合键更新行#
¥Update rows by composite key
MySQL 节点的更新操作允许你通过提供“要匹配的列”和值来更新表中的行。这适用于单列值可以唯一标识各个行的表格。
¥The MySQL node's Update operation lets you to update rows in a table by providing a Column to Match On and a value. This works for tables where single column values can uniquely identify individual rows.
对于使用 复合键 的表,如果需要多列才能唯一标识一行,则不能使用此模式。例如,MySQL 数据库中的 user 表格 节点位于 mysql 数据库中,你需要同时使用 user 和 host 列来唯一标识行。
¥You can't use this pattern for tables that use composite keys, where you need multiple columns to uniquely identify a row. A example of this is MySQL's user table in the mysql database, where you need both the user and host columns to uniquely identify rows.
要更新包含复合键的表,请手动编写查询语句,并使用“执行 SQL”操作。你可以在这里匹配多个值,例如本例中同时匹配了 customer_id 和 product_id:
¥To update tables with composite keys, write the query manually with the Execute SQL operation instead. There, you can match on multiple values, like in this example which matches on both customer_id and product_id:
1 | |
使用 Docker 时无法连接到本地 MySQL 服务器#
¥Can't connect to a local MySQL server when using Docker
运行无论在 Docker 中使用 n8n 还是 MySQL,都需要配置网络,以便 n8n 可以连接到 MySQL。
¥When you run either n8n or MySQL in Docker, you need to configure the network so that n8n can connect to MySQL.
解决方案取决于你如何托管这两个组件。
¥The solution depends on how you're hosting the two components.
如果只有 MySQL 在 Docker 容器中#
¥If only MySQL is in Docker
如果 Docker 中仅运行 MySQL,请配置 MySQL 监听所有接口,方法是将其绑定到容器内的 0.0.0.0(官方镜像已按此配置)。
¥If only MySQL is running in Docker, configure MySQL to listen on all interfaces by binding to 0.0.0.0 inside of the container (the official images are already configured this way).
运行容器时,使用带有 -p 标志的 发布端口。默认情况下,MySQL 运行在 3306 端口,因此你的 Docker 命令应如下所示:
¥When running the container, publish the port with the -p flag. By default, MySQL runs on port 3306, so your Docker command should look like this:
1 | |
配置 MySQL 凭据 时,localhost 地址应该可以正常工作(将主机设置为 localhost)。
¥When configuring MySQL credentials, the localhost address should work without a problem (set the Host to localhost).
如果只有 n8n 在 Docker 容器中#
¥If only n8n is in Docker
如果 Docker 中仅运行 n8n,请配置 MySQL 监听主机上的所有接口,方法是将其绑定到主机上的 0.0.0.0。
¥If only n8n is running in Docker, configure MySQL to listen on all interfaces by binding to 0.0.0.0 on the host.
如果你在 Linux 系统上使用 Docker 运行 n8n,请在启动容器时使用 --add-host 标志将 host.docker.internal 映射到 host-gateway。例如:
¥If you are running n8n in Docker on Linux, use the --add-host flag to map host.docker.internal to host-gateway when you start the container. For example:
1 | |
如果你使用的是 Docker Desktop,系统会自动为你配置。
¥If you are using Docker Desktop, this is automatically configured for you.
配置 MySQL 凭据 时,请使用 host.docker.internal 作为主机地址,而不是 localhost。
¥When configuring MySQL credentials, use host.docker.internal as the Host address instead of localhost.
如果 MySQL 和 n8n 运行在不同的 Docker 容器中容器#
¥If MySQL and n8n are running in separate Docker containers
如果 n8n 和 MySQL 都运行在 Docker 的不同容器中,你可以使用 Docker 网络将它们连接起来。
¥If both n8n and MySQL are running in Docker in separate containers, you can use Docker networking to connect them.
通过绑定容器内的 0.0.0.0,配置 MySQL 监听所有接口(官方镜像已完成此配置)。将 MySQL 和 n8n 容器添加到同一个 用户自定义桥接网络 容器中。
¥Configure MySQL to listen on all interfaces by binding to 0.0.0.0 inside of the container (the official images are already configured this way). Add both the MySQL and n8n containers to the same user-defined bridge network.
配置 MySQL 凭据 时,请使用 MySQL 容器的名称作为主机地址,而不是 localhost。例如,如果你调用 MySQL 容器 my-mysql,则应将“主机”设置为 my-mysql。
¥When configuring MySQL credentials, use the MySQL container's name as the host address instead of localhost. For example, if you call the MySQL container my-mysql, you would set the Host to my-mysql.
如果 MySQL 和 n8n 运行在同一个 Docker 容器中#
¥If MySQL and n8n are running in the same Docker container
如果 MySQL 和 n8n 运行在同一个 Docker 容器中,则 localhost 地址无需任何特殊配置。你可以配置 MySQL 监听 localhost,并将 n8n 中的 MySQL 凭据 中的主机配置为使用 localhost。
¥If MySQL and n8n are running in the same Docker container, the localhost address doesn't need any special configuration. You can configure MySQL to listen on localhost and configure the Host in the MySQL credentials in n8n to use localhost.
以字符串形式返回的十进制数#
¥Decimal numbers returned as strings
默认情况下,MySQL 节点将 DECIMAL 值 作为字符串返回。这样做是为了避免因 JavaScript 表示数字的方式限制而导致的精度损失。你可以在 MySQL 库 文档中了解更多关于 n8n 使用的决策的信息。
¥By default, the MySQL node returns DECIMAL values as strings. This is done intentionally to avoid loss of precision that can occur due to limitation with the way JavaScript represents numbers. You can learn more about the decision in the documentation for the MySQL library that n8n uses.
要将十进制值输出为数字而不是字符串,并忽略精度损失的风险,请启用“将十进制数输出为数字”选项。此操作会将值输出为数字而不是字符串。
¥To output decimal values as numbers instead of strings and ignore the risks in loss of precision, enable the Output Decimals as Numbers option. This will output the values as numbers instead of strings.
或者,你可以使用 toFloat() 功能 和 toFixed() 或 编辑字段(集)节点(在 MySQL 节点之后)手动将字符串转换为十进制数。请注意,你可能仍然需要考虑潜在的精度损失。
¥As an alternative, you can manually convert from the string to a decimal using the toFloat() function with toFixed() or with the Edit Fields (Set) node after the MySQL node. Be aware that you may still need to account for a potential loss of precision.