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

Rayan's avatar
Level 1

Laravel connection timed out

I've searched a loot on google about this problem and I tried everything. I have a database of a game from a hosting and I bought another webhost (shared hosting) from another company.

I have Laravel 7 on my website and I want to display all users informations from database and many other things.

The problem that i'm facing, is that, I need to access the database remote OUT (which I spoke with the support of the shared hosting and they said that they allow it), but still I get this error:

[previous exception] [object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /home/kirosne1/web/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70)

The page is render (for example: login page) but when I click the 'login button' to check if the user exist, I wait for aboout 30 seconds and it gets me error 404 and in laravel.log is the above error.

My .env is something like this: (example - not the real credentials):

DB_CONNECTION=mysql

DB_HOST=195.205.39.83

DB_PORT=3306

DB_DATABASE=hji2jsad232

DB_USERNAME=hji2jsad232

DB_PASSWORD=23sadsadsa

I tried to connect from a local mysql on the shared hosting i bought, and is working, but remote OUT is not.

I tried changing the php version from cpanel, also to clear the config cache and it didn't work.

What should I do?

0 likes
17 replies
underdash's avatar

Double check the credentials you are providing.

PHP tries to establish a connection with MySQL end on the server but there is no response.

underdash's avatar

@Rayan Do you have any sort of Database Management tools ?

If you do not have one install one, then try to make a successful connection with the tool first then replace credentials in your .env file.

Rayan's avatar
Level 1

@underdash Sorry, I don't really know what you mean, can you give me an example?

MohamedTammam's avatar

I don't believe your database host opened it for remote access. Check again with them.

And please don't share any credentials, it's unsafe.

siangboon's avatar

This is unlikely allowed by the hosting provider. perhaps use the same server or provider may be the better choice.

underdash's avatar

Since connection is being established using database management tool. Try replace a domain with a TLD instead of an ip address for your DB_HOST.

Something like:

DB_HOST=mysite.org
sr57's avatar

@rayan

What the value of the connection timeout of your remote db?

SHOW VARIABLES LIKE 'connect_timeout';

Try to increase this parameter

SET GLOBAL connect_timeout = ...
Rayan's avatar
Level 1

@sr57 the value is '10' and I don't have access to change it

sr57's avatar

@rayan

What (any error if does not work) if your try this command

DB::statement("SET GLOBAL connect_timeout = 100");

or

DB::statement("SET connect_timeout = 100");
Rayan's avatar
Level 1

@sr57 #1227 - Access denied; you need (at least one of) the SUPER privilege(s) for this operation

sr57's avatar

@rayan

Can you check these 2 variables interactive_timeout & wait_timeout, if not set to 28800, change them to this value and test again.

But probably you seem to get a poor database connection, if you cannot change connect_timeout, you should consider to change.

underdash's avatar

What is your environment settings ? Is it a shared host ? Self-configured VPS ? or a Cloud ? Are you using docker ?

There are numerous scenarios please provide more information.

Please or to participate in this conversation.