MySQL default_socket_timeout超时仅在从ECS Fargate任务连接时

发布于 2025-01-25 16:00:42 字数 928 浏览 4 评论 0原文

这是一个奇怪的人。在我的ECS Fargate任务中,我有一个运行PHP代码的Docker容器,该容器调用MySQLI_Connect连接到RDS MySQL数据库。效果很好。

但是,有时我想停止RDS数据库以节省资金,这将导致MySQLI_Connect命令失败。

示例代码:

<?php
    $connection = mysqli_connect($hostname, $username, $password, $database);

    echo "done";
?>

当我通过host.docker.internal在我的本地环境中的容器中运行此操作时,它会立即失败,并且代码继续执行,这就是我想要的。

(校正)当我从连接到同一停止RDS MySQL数据库的本地环境中的容器中运行此操作时,它会等待PHP DEFAULT_SOCKETD_TIMET值(默认为60秒),然后失败,代码继续执行。我希望它立即失败。

当我从Fargate任务中的容器中运行此操作时,它会等待php default_socket_timeout值(默认为60秒),然后会失败,并且代码继续执行。我希望它立即失败。

我意识到我可以将Default_Socket_TimeOut设置为1秒。我已经做到了,看到它在1秒而不是60后失败,但是为什么这是调用Default_Socket_TimeOut的唯一场景?

更新:我发现了部分原因,这与AWS RDS实例有关。当它运行并故意使$ hostName,$ username,$ password或$数据库的值错误时,它将立即失败。只有当我停止RDS实例时,无论代码是在Fargate任务容器还是本地容器中运行的default_socket_timeOut秒数。

我的解决方案是通过在调用mySqli_connect()之前将AWS描述db-instances API拨打default_socket_timeout延迟延迟。

This is a strange one. In my ECS Fargate task, I have a Docker container running PHP code that calls mysqli_connect to connect to an RDS MySQL database. Works great.

However, sometimes I want to stop the RDS database to save money which will cause the mysqli_connect command to fail.

Sample code:

<?php
    $connection = mysqli_connect($hostname, $username, $password, $database);

    echo "done";
?>

When I run this from a container in my local environment connecting to a stopped local MySQL database via host.docker.internal, it fails immediately and code continues to execute, which is what I want.

(Corrected) When I run this from a container in my local environment connecting to the SAME stopped RDS MySQL database, it waits for the PHP default_socket_timeout value (default 60 seconds) then fails and code continues to execute. I want it to fail immediately.

When I run this from a container in my Fargate task, it waits for the PHP default_socket_timeout value (default 60 seconds) then fails and code continues to execute. I want it to fail immediately.

I realize I can set the default_socket_timeout to 1 second. I've done that and seen it fail after 1 second instead of 60, but why is this the only scenario where default_socket_timeout is invoked?

UPDATE: I figured out part of the cause and it has to do with the AWS RDS instance. When it's running and I purposely make the value of $hostname, $username, $password, or $database wrong, it will fail immediately. Only when I stop the RDS instance will it wait for default_socket_timeout seconds no matter if the code is running in a Fargate task container or local container.

My solution was to avoid the default_socket_timeout delay entirely by calling the AWS describe-db-instances API periodically to get its status before calling mysqli_connect().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文