简体   繁体   English

为什么我们必须在查询命令后关闭MySQL数据库?

[英]Why do we have to close the MySQL database after a query command?

I'm starter. 我是首发。

I want to know what will happen if we don't close the MySQL connection. 我想知道如果我们不关闭MySQL连接会发生什么。

1- Is it possible to open more than one database if we don't close them? 1-如果我们不关闭它们,是否可以打开多个数据库? I mean can we open more than one database in a same time? 我的意思是我们可以同时打开多个数据库吗?

2- Does closing database increase the speed? 2-关闭数据库是否会提高速度?

3- Is it necessary to close the database or it is optional? 3-是否需要关闭数据库或它是可选的?

Look at this code. 看看这段代码。 I don't use "mysql_close()" so I don't close the database after each request. 我不使用“mysql_close()”所以我不会在每次请求后关闭数据库。 There are a lot of requests for this PHP page. 这个PHP页面有很多请求。 Maybe 50000 per each minute. 也许每分钟50000。 I want to know closing database is necessary for this code or no? 我想知道关闭数据库是否需要此代码或没有?

<?php   
//Include the file that lets us to connect to the database.
include("database/connection.php");

//Call "connect" function to connect to the database.
connect("database", "localhost", "root", "", "user");

//The GPRS module send a string to this site by GET method. The GPRS user a variable named variable to send the string with.
$received_string = $_GET["variable"];

//Seprates data in an array.
$array_GPRS_data = explode(",", $received_string);

//we need to remove the first letter.
$array_GPRS_data[9] = substr($array_GPRS_data[9], 1);


$array_GPRS_data[13] = substr($array_GPRS_data[13], 4, 2).substr($array_GPRS_data[13], 2, 2).substr($array_GPRS_data[13], 0, 2);

//Query statement.
$query = "INSERT INTO $array_GPRS_data[17](signal_quality, balance, satellite_derived_time, satellite_fix_status, latitude_decimal_degrees,
latitude_hemisphere, longitude_decimal_degrees, longitude_hemisphere, speed, bearing, UTCdate, theChecksum)
VALUES('$array_GPRS_data[0]', '$array_GPRS_data[1]', '$array_GPRS_data[5]', '$array_GPRS_data[6]', '$array_GPRS_data[7]',
'$array_GPRS_data[8]', '$array_GPRS_data[9]', '$array_GPRS_data[10]', '$array_GPRS_data[11]', '$array_GPRS_data[12]', '$array_GPRS_data[13]',
'$array_GPRS_data[16]')";

//Run query.
$result = mysqli_query($query);

//Check if data are inserted in the database correctly.
if($result)
{
    echo("*#01");
}
else
{
    echo("Error: 001");
    echo (mysqli_error());
}   
?>
  1. Yes, you can have multiple database connections. 是的,您可以拥有多个数据库连接。 You are not opening a database, you are opening a database connection. 您没有打开数据库,而是打开数据库连接。 The database is 'open' (ie running) all of the time, generally speaking, whether you are connected to it or not. 一般来说,无论您是否连接数据库,数据库都是“开放”(即运行)。
  2. Depends... if you only have one open connection on a page, then you don't need to close it because it will automatically close when PHP is done. 取决于...如果您在页面上只有一个打开的连接,那么您不需要关闭它,因为它将在PHP完成时自动关闭。 If you have many, then you could potentially make the database server slower, or make the database server run out of available connections (it can only have a certain number of connections open at the same time). 如果您有很多,那么您可能会使数据库服务器变慢,或者使数据库服务器耗尽可用连接(它只能同时打开一定数量的连接)。 That said, most modern database servers can handle hundreds of concurrent connections. 也就是说,大多数现代数据库服务器可以处理数百个并发连接。
  3. Optional, but recommended. 可选,但建议。 It's not a big deal for small-medium projects (ie if you have less than 100 concurrent visitors at any given time, you probably won't have any issues regardless). 这对于中小型项目来说并不是什么大问题(即如果你在任何特定时间你的并发访问者少于100人,你可能不会有任何问题)。 Since you have many thousand visitors per minute, you should actively close the database connection as soon as you are done with it, to free it up as soon as possible. 由于您每分钟有数千个访问者,因此您应该在完成后立即主动关闭数据库连接,以便尽快释放它。

Once you connect to the database it is not necessary to close. 连接到数据库后,无需关闭。 As non-persistent connection automatically closed at the end of script execution. 由于非持久连接在脚本执行结束时自动关闭。

Follow this for more information 请按照此处获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM