Report this

What is the reason for this report?

Do I need to open the MySQL port for my LAMP app to work?

Posted on February 23, 2018

I am getting the following error after I first deployed my app: Database connection failed: Connection timed out (2002)

Thank you in advance!



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!

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.

Hi,

if your mysqld is running on the same box as the rest of the stack then you should definitely be using a UNIX socket to connect to mysql.

What does your mysql connection stanza look like in the code?

Yes, it is running on the same box.

defined(‘DB_SERVER’) ? null : define(“DB_SERVER”, “<my_ip_address>”); defined(‘DB_USER’) ? null : define(“DB_USER”, “<db_username>”); defined(‘DB_PASS’) ? null : define(“DB_PASS”, “<db_password>”); defined(‘DB_NAME’) ? null : define(“DB_NAME”, “<db_name”);

private $connection;

function __construct() { $this->open_connection(); }

public function open_connection() { $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME); if(mysqli_connect_errno()) { die(“Database connection failed: " . mysqli_connect_error() . " (” . mysqli_connect_errno() . “)” ); } }

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.