Question

Error when Website is connecting to database.

I received the following error when deploying my php application for the first time. It looks like it is timing out when attempting to connect to MySQL: Database connection failed: Connection timed out (2002)


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 20, 2023

Hi there,

The error “Database connection failed: Connection timed out (2002)” typically means that PHP is not able to establish a connection with your MySQL server. This could be due to several reasons. Here are a few things you can check:

  1. Database Server is Not Running: Make sure that the MySQL service is up and running. You can check this by running the command systemctl status mysql or service mysql status (based on your system) on your server. If it’s not running, you can start it using systemctl start mysql or service mysql start.

  2. Incorrect Connection Parameters: Make sure you’re using the correct hostname (typically “localhost” for a database on the same server), username, password, and database name in your PHP code. The database user should also have the necessary permissions on the database.

  3. Firewall Blocking Connection: If your MySQL database is on a different server, it could be that a firewall is preventing your PHP application from establishing a connection. Check any firewall settings or security groups (if you’re using a cloud service like AWS).

  4. Incorrect MySQL Configuration: The MySQL server might be listening on a different port, or not correctly configured to accept connections from your PHP application. Check the MySQL configuration file (typically located at /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/my.cnf) and verify the bind-address and port directives.

  5. Network Issues: There could be network issues between your PHP application and MySQL server. You can test the connection by trying to connect to the MySQL server from the command line of the server where your PHP application is hosted, using something like mysql -u username -p -h host.

  6. Database Server Overloaded: If the server is under heavy load, the connection request may time out before it can be established. In this case, you might need to optimize your database or server setup to handle a higher load, or consider upgrading your server if the hardware is insufficient, here is an article that you could follow:

https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel