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

February 23, 2018 826 views
MySQL Ubuntu 16.04

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

Thank you in advance!

2 Answers


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('DBSERVER') ? null : define("DBSERVER", "<my_ip_address>");
defined('DBUSER') ? null : define("DBUSER", "<db_username>");
defined('DBPASS') ? null : define("DBPASS", "<db_password>");
defined('DBNAME') ? null : define("DBNAME", "<db_name");

private $connection;

function _construct() {

public function openconnection() {
$this->connection = mysqli
if(mysqliconnecterrno()) {
die("Database connection failed: " .
mysqliconnecterror() .
" (" . mysqliconnecterrno() . ")"

Have another answer? Share your knowledge.