Home > Backend Development > PHP Tutorial > PHP database connection monitoring: track connection usage and performance

PHP database connection monitoring: track connection usage and performance

WBOY
Release: 2024-05-31 17:46:00
Original
652 people have browsed it

PHP 数据库连接监控:跟踪连接使用和性能

#PHP Database Connection Monitoring: Tracking Connection Usage and Performance

Connection monitoring is critical for optimizing database performance and ensuring application stability. This article explains how to use PHP to track and monitor the usage and performance of database connections.

Install the database extension

Before you begin, you need to install the database extension. The most commonly used extensions are MySQLi and PDO. Here's how to install both extensions:

MySQLi:

sudo apt-get install php-mysqli
Copy after login

PDO:

sudo apt-get install php-pdo
Copy after login

Connect to Database

First, you need to connect to the database. You can use the following code to establish a MySQLi connection:

$mysqli = new mysqli("host", "user", "password", "database");

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
Copy after login

For PDO, you can use the following code:

$dsn = 'mysql:host=host;dbname=database';
$user = 'user';
$password = 'password';

try {
    $conn = new PDO($dsn, $user, $password);
} catch (\PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
Copy after login

Monitor the connection using

You can use PHP's built-in mysqli_get_connection_stats() and mysqli_get_links_stats() functions obtain information about connection usage. The following is a sample code that displays the number of connections to a MySQL database:

$connection_info = mysqli_get_connection_stats($mysqli);

echo "Number of open connections: " . $connection_info['connection_count'] . "\n";
Copy after login

For PDO, you can use the following code:

$stats = $conn->query("SHOW VARIABLES LIKE 'Connections'");
echo "Number of open connections: " . $stats->rowCount() . "\n";
Copy after login

Monitor connection performance

Connection performance can be monitored by measuring query execution time. You can use PHP's built-in microtime(true) function:

$start = microtime(true);
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
$end = microtime(true);
echo "Query execution time: " . ($end - $start) . " seconds\n";
Copy after login

For PDO, you can use the following code:

$start = microtime(true);
$statement = $conn->prepare($query);
$statement->execute();
$end = microtime(true);
echo "Query execution time: " . ($end - $start) . " seconds\n";
Copy after login

Practical case: automatically close idle connections

PHP connection monitoring can be used to automatically close idle database connections. The following is a sample code using the mysqli_reap_async_connections() function:

mysqli_reap_async_connections($mysqli);

// Repeat the reaping every 60 seconds
sleep(60);
Copy after login

For PDO, you can use the following code:

$conn->close();

// Repeat the closing every 60 seconds
sleep(60);
Copy after login

The above is the detailed content of PHP database connection monitoring: track connection usage and performance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template