有时通过 PHP 连接到 RDS 非常慢

发布于 2025-01-15 03:48:53 字数 1843 浏览 4 评论 0原文

我有一个 EC2 t2.medium 和一个 RDS MySQL t2.medium,但遇到了一些相当令人沮丧的事情。

首先,这是我的代码:

我的数据库类:

class Database {

private static $host = 'database-3...us-east-2.rds.amazonaws.com';
private static $user = 'admin';
private static $password = '...';

private static $lastConnection = null;

private static $queryCount = 0;

public static function connect(){
    $con = mysqli_init();
    $db = "prod";
    $env = new Environment();
    if($env->isStaging()) $db = "staging";
    //$con->ssl_set(NULL, NULL, "/home/ubuntu/bundled.pem", NULL, NULL);
    $con->real_connect(self::$host, self::$user, self::$password, $db) or die('couldn\'t connect: '.$con->connect_error.":".$con->error);
    self::$lastConnection = $con;
    mysqli_set_charset($con, "utf8mb4");
    return $con;
}

public static function query($query){
    $start_time = microtime(true);
    $connection = self::connect();
    self::$lastConnection = $connection;
    $result = $connection->query($query) or die($connection->error);
    self::$queryCount++;
    $end_time = microtime(true);
    echo "<br><br>query(".$query.")<br>execution time: ".(($end_time-$start_time)*1000)."ms<br><br>";
    return $result;
}

这是我正在执行的代码:

$catsQuery = "SELECT * FROM `product_categories` ORDER BY `list_order` ASC";
    
$result = Database::query($catsQuery);

现在,大多数时候,一切都很顺利。此查询的执行时间为 10 毫秒或更短。

但是,有时会发生这种情况...

在此处输入图像描述

如您所见,所用的总时间超过了一整秒。

现在,通过一番折腾,我发现花费额外时间的是连接到RDS。查询本身不是问题,而只是连接问题。

我使用的是 PHP 7.4。

另外,我已经消除了 Nginx(因为我绕过了 Nginx 并得到了相同的结果),并且我已经通过终端从 EC2 直接连接到 RDS,并且没有任何延迟。因此,不知何故,问题出在 PHP 用于连接数据库的连接函数上。

I have an EC2 t2.medium and an RDS MySQL t2.medium and am experiencing something rather frustrating.

First of all, here is my code:

My database class:

class Database {

private static $host = 'database-3...us-east-2.rds.amazonaws.com';
private static $user = 'admin';
private static $password = '...';

private static $lastConnection = null;

private static $queryCount = 0;

public static function connect(){
    $con = mysqli_init();
    $db = "prod";
    $env = new Environment();
    if($env->isStaging()) $db = "staging";
    //$con->ssl_set(NULL, NULL, "/home/ubuntu/bundled.pem", NULL, NULL);
    $con->real_connect(self::$host, self::$user, self::$password, $db) or die('couldn\'t connect: '.$con->connect_error.":".$con->error);
    self::$lastConnection = $con;
    mysqli_set_charset($con, "utf8mb4");
    return $con;
}

public static function query($query){
    $start_time = microtime(true);
    $connection = self::connect();
    self::$lastConnection = $connection;
    $result = $connection->query($query) or die($connection->error);
    self::$queryCount++;
    $end_time = microtime(true);
    echo "<br><br>query(".$query.")<br>execution time: ".(($end_time-$start_time)*1000)."ms<br><br>";
    return $result;
}

And here is the code I'm executing:

$catsQuery = "SELECT * FROM `product_categories` ORDER BY `list_order` ASC";
    
$result = Database::query($catsQuery);

Now, most of the time, everything is smooth. This query executes in 10ms or less.

But, some of the time, this happens...

enter image description here

As you can see, the total time elapsed was over a full second.

Now, through some messing around, I've discovered that what takes the extra time is the connecting to the RDS. The query itself is not the issue, it's just the connection.

I'm on PHP 7.4.

Also, I have eliminated Nginx (as I've bypassed Nginx and gotten the same result) and I've connected directly to the RDS from my EC2 via terminal and there is no delay there ever. So, somehow, the problem is the connection functions PHP is using to connect to the database.

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

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

发布评论

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