How to connect to a database in openshift?
You need to read environment variables in Java like this:
String envVar = System.getenv("OPENSHIFT_ENV_VAR");
In your case:
String dbHost = System.getenv("OPENSHIFT_POSTGRESQL_DB_HOST");
String dbPort = System.getenv("OPENSHIFT_POSTGRESQL_DB_PORT");
Read more at the Openshift docs, "Using Environment Variables".
Can't Connect to Postgres Database in OpenShift Deployment remotely but can via localhost with portforwarding
tdlr: my service definition's port name for my postgres service was 'http' and not 'tcp'. The name field is actually is the protocol so it does matter what you put here.
solution found here https://github.com/istio/istio/issues/16506#issuecomment-636224246
This may not be an issue if your deployment does not use istio.
The updated service yml for my postgres deployment is
apiVersion: v1
kind: Service
metadata:
labels:
app: postgres
service: postgres
name: postgres
spec:
ports:
- name: tcp
port: 5432
selector:
app: postgres
How to connect to MS SQL Server 2019 Instance running in Openshift cluster with SSMS via port-forwarding?
As mentioned in the linked post and by @AlwaysLearning in the comments, you have to connect by refering to localhost by IP(127.0.0.1
) and not by hostname(localhost
).
Connect to MySQL database by using route exposed on openshift
Short answer: You can't with aRoute
Route
can only expose http/https traffic
If you want to expose tcp traffic (like for a database), do not create aRoute
and change yourService
type to "NodePort"`
Check my previous answer for this kind of problem (exposing MQ in this case): How to connect to IBM MQ deployed to OpenShift?
OpenShift doc on NodePorts: https://docs.openshift.com/container-platform/4.7/networking/configuring_ingress_cluster_traffic/configuring-ingress-cluster-traffic-nodeport.html
How to open DB connection in Openshift?
I've made it to work by doing this.
Global Use
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));
$dbhost = constant("DB_HOST"); // Host name
$dbport = constant("DB_PORT"); // Host port
$dbusername = constant("DB_USER"); // MySQL username
$dbpassword = constant("DB_PASS"); // MySQL password
$db_name = constant("DB_NAME"); // Database name
Alternatively
$dbhost = getenv('OPENSHIFT_MYSQL_DB_HOST'); // Host name
$dbport = getenv('OPENSHIFT_MYSQL_DB_PORT'); // Host port
$dbusername = getenv('OPENSHIFT_MYSQL_DB_USERNAME'); // MySQL username
$dbpassword = getenv('OPENSHIFT_MYSQL_DB_PASSWORD'); // MySQL password
$db_name = getenv('OPENSHIFT_GEAR_NAME'); // Database name
Related Topics
Can You Pass by Reference While Using the Ternary Operator
PHP Mail() Works from Command Line But Not Apache
Is There a Call_User_Func() Equivalent to Create a New Class Instance
Compare Two Version Strings in PHP
Pdo Returning Error "Could Not Find Driver" with a Known Working Dsn
Woocommerce - Overriding Billing State and Post Code on Existing Checkout Fields
How to Get Results from the Wikipedia API with PHP
Find Min/Max in a Two Dimensional Array
Good Tutorial on How to Update Your MySQL Database with a PHP Form
What Do \R and \N Mean in PHP (And Other Languages)
Curl Import Character Encoding Problem
Can't Connect to Postgresql with PHP Pg_Connect()
Mysql: Group by Two Columns and Sum
Why Does (0 == 'Hello') Return True in PHP