0

I have an API I want to deploy to OpenShift, everything works locally, but when trying to use it online I get some problems.

When trying to connect to the database, using PDO, I get this error:

Failed to connect to the database: SQLSTATE[HY000] [2002] Connection refused

I have the database set up correctly, and I have the correct details for the connection (I'm pretty sure).

My code:

$username = "myuser"; 
$password = "lmnop"; 
$host = "127.0.0.1"; 
$dbname = "loginapi"; 
$port = "3306";

$db = new PDO("mysql:host={$host};port={$port};dbname={$dbname};charset=utf8", $username, $password, $options);

Why is OpenShift not allowing the connection?

Jonny C
  • 1,943
  • 3
  • 20
  • 36
ThriceGood
  • 1,633
  • 3
  • 25
  • 43

1 Answers1

0

You are using the wrong host (and maybe the wrong port also), you should be using the OpenShift MySQL Environment Variables on your gear. You can find out more about them here: https://developers.openshift.com/en/databases-mysql.html

Here is an example of using them in a PHP application (WordPress): https://github.com/openshift/wordpress-example/blob/master/.openshift/config/wp-config.php#L104-L108