Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SachinAgarwal's avatar

[L5] Database connection time

I have a long set of database queries which will take a while to be executed. But at the half execution I get Maximum execution time of 30 seconds exceeded error.
I tried

ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);

But that is not working.
Any idea how can I increase the time of database connection?

0 likes
10 replies
toniperic's avatar
Level 30
set_time_limit($seconds)

$seconds being the maximum execution time, in seconds. If set to zero, no time limit is imposed.

The error you are getting is PHP-related, not database related.

Hope I could help.

1 like
pmall's avatar

if your query gets > 30 s to execute something is wrong, you should focus on optimizing it.

It is not a good solution to increase the max execution time.

SachinAgarwal's avatar

@toniperic @pmall Yes I know, But it is for Admin panel where Admin register an organization and in background I will perform some tasks like:

  1. create database
  2. Migrate
  3. Create view
  4. add default values to database
    etc
    That is why it will take more than 30 secs.

@toniperic Thank you that solved my prob. But why dint

ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);

worked? And is that set_time_limit() laravel helper or php function()?

toniperic's avatar

Because you are setting mysql.connect_timeout, and the error says the script has exceeded maximum execution time limit. It doesn't mention anywhere it has anything to do with mysql, it's only your presumption.

set_time_limit is a PHP function. It should work if you tried this.

ini_set('max_execution_time', $seconds);
toniperic's avatar

Sure, no problem.

btw making someone wait 30 seconds in 2015 is a terrible idea. :)

pmall's avatar

@SachinAgarwal this is a canonical example of a job you should make an artisan command for and queue it.

Don't modify the time limit it is not an solution. What if you server start to slow down and exceed your new time limit

SachinAgarwal's avatar

@toniperic I'm making myself wait for more than 30 secs :P Its like for general public it will be faster. But when I (SuperAdmin) register the organization Then have to perform some task. And instead of doing it one by one, I made a script to do them all in once.

Please or to participate in this conversation.