How long does a PHP MySQL database connection remain active? - php

I have a long running PHP script. I am making the database connection at the very beginning of the script and does some database operation at the start up.
After that, the script perform 4 hours of PHP operation without pinging to MySQL with that connection even a single time.
At the end of these long running PHP operations, when I try to execute mysql_query it gives me the following error: MySQL Server has gone Away
Is there any possibility of increasing the connection timeout to be 4 hours? I am using PHP ADODB to connect with MySQL from my PHP application.
Please suggest what to do?

MySQL has a different timeout than PHP. You could increase it in php.ini on the line mysql.connect_timeout = 14400. Also increase the default_socket_timeout = 14400
Note that if your PHP setting allow you to do an ini_set, you can also do as follows:
ini_set('mysql.connect_timeout', 14400);
ini_set('default_socket_timeout', 14400);

Related

How to set TimeOut for execution the query from PHP Sqlsrv

How do I set the TimeOut for EXEC the Query or Stored Procedure in SqlSrv PHP
Because i am using PHP to call SQL-Server Stored Procedure.
For ex
"EXEC SP_Name"
some times its taking too long time, so the PHP page shows 500 Internal server error.
If possible to set the time limit the SP was stopped then i show the error description etc...
how do i fix this issue?
Thanks in Advance.
in your php.ini file set
max_execution_time = 60--or whatever time you wish
Refer This Link for more details
The #Jayasurya Satheesh's answer refers to the time of any php execution time, but not for db query exec time. Actually php lacks in a easy way to control the execution time of a db query.
It is more on the DB/SQL settings side. If you can define the TimeOut on the SQL Server side, this will return "timeout" to PHP.
Anyway, you can define set_time_limit(25) (where 25 is the number of seconds you want), You can put this in your php code before your SQL EXEC, not neccesary to deal with php.ini if you want to set_time_limit for specific php file. So, if the query takes more than 25 seconds, the php will stop execution.
Remember set_time_limit() does not affect other php scritps, it's only for the current file where it's placed

PHP Timeout to Logs Table

I have it set up in my PHP.ini file to timeout after 30 seconds. I want to keep this setting. I have a script that connects to an API and every once in a while (like 1 in 10 times), it can't connect so it spends the entire 30 seconds. This connection is via curl.
I have a logs table in my database and I would like before the timeout occurs to insert a new item. Is this possible via PHP to insert a row on timeout for only this particular script?
I have done my own research and can't find anything. Any help would be appreciated.
You can use register_shutdown_function to execute something on script shutdown. It will even be called on script timeout.
edit:
Alternatively you can use CURLOPT_TIMEOUT to set the max execution time for your curl execution to something below 30s and then look at the returned value to see if max execution was reached and then log to the database.

How to manage PHP odbc_exec timeout?

I've got a command-line PHP script that connects to SQL Server 2005 using an ODBC connection. It uses odbc_exec() to execute a stored procedure which may take a while to process, and I don't want it to timeout.
I've been unable to find anything in the PHP documentation regarding how to set the timeout for odbc_exec(). Does it default to an infinite wait?
There's only so much you can do here. PHP's default script execution time is set to 30 seconds. There are numerous ways to change this, including the set_time_limit() function (found in the manual), and extending execution time by using ini_set, or using odbc_setoption
//extend execution time
$num_minutes = 5;
ini_set('max_execution_time', (60*$num_minutes)); //set timeout to 5 minutes
You can also change this setting inside your php.ini file. However, all of these only control the scope within what your apache server can control. If the database itself has a timeout, it will still cut off your call and return an error, even if your personally set execution times are elevated.
According to php.net you can use odbc_setoption():
$result = odbc_prepare($conn, $sql);
odbc_setoption($result, 2, 0, 30);// 30 seconds timeout
odbc_execute($result);
So you can increase timeout to some bigger number depending on your need

mysql server has gone away - not timeout error or crash

I'm using WAMP (Apache 2.2.11, MySQL 5.1.36, PHP 5.3.0) to write a PHP web application that has a link to a MySQL database.
I run a procedure that takes about 2 minutes in Toad to execute. If i try to run it in the PHP application, i got the errors "MySQL server has gone away in...", "Error reading result set's header", and "maximum execution time of 60 seconds exceeded"
I upped mysql.connect_timeout to 600 (using ini_set) and default_socket_timeout to 600 (as suggested in this question. This solved the timeout error, but i still get the first two.
I tried setting max_input_time to -1, and on the MySQL server side, i upped max_allowed_packet to 256M. Neither of these solved the problem.
Are there any other ways to bypass this error without altering the MySQL procedure being run?
when the webpage is loaded:
set_time_limit(0);
ini_set('mysql.connect_timeout', 600);//for long queries
ini_set('default_socket_timeout', 600);//for long queries
//ini_set('max_input_time',-1); //don't know what this does
when a query is called:
if($reconnect===true){
$this->disconnectDB();
}
$this->connectToDB();
if($this->selectDB($dbName)===FALSE){
return false;
}
if(mysql_ping($this->con)===false){
return array();
}
$result = mysql_query($query,$this->con); //this is where the connection "goes away"
This is the result of a PHP setting: maximum execution time of 60 seconds exceeded. Put set_time_limit(0) to disable the time limit when you need to.
MySQL server has gone away in... can be the result of several things. Including a MySQL crash. You may want to look at mysql logs for clues.

How to keep a php script from timing out because of a long mysql query

I have an update query being run by a cron task that's timing out. The query takes, on average, five minutes to execute when executed in navicat.
The code looks roughly like this. It's quite simple:
// $db is a mysqli link
set_time_limit (0); // should keep the script from timing out
$query = "SLOW QUERY";
$result = $db->query($query);
if (!$result)
echo "error";
Even though the script shouldn't timeout, the time spent waiting on the sql call still seems to be subject to a timeout.
Is there an asynchronous call that can be used? Or adjust the timeout?
Is the timeout different because it's being called from the command line rather than through Apache?
Thanks
I had the same problem somwhere, and "solved" it with the following code (first two lines of my file):
set_time_limit(0);
ignore_user_abort(1);
According to the manual:
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
So it's unlikely to have anything to do with PHP's time limit. What message are you getting when it times out? Perhaps there's a MySQL setting involved.
Is your php running in safe-mode? Quote from PHP manual of set_time_limit:
This function has no effect when PHP
is running in safe mode. There is no
workaround other than turning off safe
mode or changing the time limit in the
php.ini.
I came across something similar in one of my PHP scripts, I added this inline right before executing the slow query:
$timeout_seconds = 3153600; // 1 year...
// Make sure the PHP script doesn't time out
set_time_limit(0);
ignore_user_abort(1);
// Make sure the PHP socket doesn't time out
ini_set('default_socket_timeout', $timeout_seconds);
ini_set('mysqlnd.net_read_timeout', $timeout_seconds);
// Make sure the MySQL server doesn't time out
// Assuming your $link is a MySQLi object:
$link->query("SET SESSION connect_timeout=" . $timeout_seconds);
$link->query("SET SESSION delayed_insert_timeout=" . $timeout_seconds);
$link->query("SET SESSION have_statement_timeout='NO'");
$link->query("SET SESSION net_read_timeout=" . $timeout_seconds);
$link->query("SET SESSION net_write_timeout=" . $timeout_seconds);
Obviously, you'll want to set the seconds appropriately, but I didn't want my script to time out at all.
PHP Init directives: https://www.php.net/manual/en/ini.list.php
MySQL Server variables: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html
Note: Check the documents for more information, and also verify the variables match the PHP and MySQL versions that you are using. I'm using PHP 7.3 and MySQL 5.7.
** Edit: Setting the PHP timeouts wasn't enough for my script, I had to add the MySQL SESSION variables too.
Assuming you are on linux, Debian based systems have separate configurations for mod_php/php cgi and php-cli. This shouldn't be too difficult to set up on a different linux system that doesn't separate cgi/cli configuration.
Once you have separate configs, I would adjust your php cli configuration. Disable safe mode and any time limits and ram limits.
Check out some of the resource limit variables in php,ini:
max_execution_time, max_input_time, memory_limit
You could also set a time limit for the script in PHP itself:
http://ca3.php.net/set_time_limit

Categories