[OpenShift] Deploy PHP/MySQL in OpenShift got 503 Service Temporarily Unavailable / 504 Gateway Time-out

Context :

The objective is to deploy the connection of MySQL with PHP in an OpenShift Server.
OpenShift is PaaS provided by Red Hat. For more info, read wikipedia OpenShift.

Problem :

The server returns :

503 Service Temporarily Unavailable

or

504 Gateway Time-out

Code source :

<?php $con = mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection 
  if (mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
  } 
?>

Reason :

For a scalable gear in OpenShift, we have to specify the mysql port to make it work.

Solution :

<?php $con = mysqli_connect("localhost","my_user","my_password","my_db", "54321"); // Check connection 
   if (mysqli_connect_errno()) { 
      echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
   } 
?>

N.B.
To get the values of the database variable (to mysql, just replace by mysql):

Variable Name

Purpose

OPENSHIFT_<database>_DB_HOST

The host name or IP address used to connect to the database.

OPENSHIFT_<database>_DB_PORT

The port the database server is listening on.

OPENSHIFT_<database>_DB_USERNAME

The database administrative user name.

OPENSHIFT_<database>_DB_PASSWORD

The database administrative user’s password.

OPENSHIFT_<database>_DB_SOCKET

An AF socket for connecting to the database (for non-scaled apps only).

OPENSHIFT_<database>_DB_URL

Database connection URL.

And the syntax for mysqli in PHP :

mysqli_connect(host,username,password,dbname,port,socket);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s