0

I spent a lot of time trying to figure out how to connect to Openshift's mysql database through my Java project. I have created MySQL database, PhpMyAdmin and TomCat 7 cartriges. I configured Putty, I even have setup ssh (don't know why) and get these information:

environment variables with Ruby or PuTTY terminal

Problem is with JDBC, I'm getting ERROR Communications link failure... my login information with jdbc in Eclipse, see bellow:

String USERNAME = System.getenv("admixxxxxxxx");
String PASSWORD = System.getenv("gxxxxxxxxxxx");
String DB_NAME = System.getenv("nautxxxx");
String URL = System.getenv("127.x.xxx.x:3306") + "/" + DB_NAME;

And finally:

connection = DriverManager.getConnection("jdbc:mysql://" + URL, USERNAME , PASSWORD);

My code working on UwAmp localhost, but can't connect to Openshift. Please help me, what's wrong.

Rock
  • 11
  • 4
  • Why do you use System.getenv() ? – Marged Dec 31 '15 at 13:46
  • Because I searched, and founded that code examples, I don't know why It must be Sys.getenv(), just using it. – Rock Jan 02 '16 at 22:43
  • You should question what Google yields for you because otherwise you will become a copy and paste victim. Either research the methods used or simply debug or print the string values and see what they contain. – Marged Jan 02 '16 at 22:58
  • Thank's for your remark, I espected to receive comm like this, I'm doing this just when I have no time for understanding what that realy means, but I need to I know. Coming session in our College, so don't have much time, just a lot of work... – Rock Jan 03 '16 at 00:33

3 Answers3

1

You are incorrectly (and unnneccessarily) using System.getenv.

System.getenv is used to get the values of environment variables. Calling System.getenv("gxhaixUc9V94"); is not giving you the password but rather null since there is most probably no environment variable with the name gxhaixUc9V94.

Why do you use getenv at all!?

Try this:

String USERNAME = "adminwmxLs6V";
String PASSWORD = "gxhaixUc9V94";
String DB_NAME = "nautilius";
String URL = "127.8.238.2:3306" + "/" + DB_NAME;
// [...]
connection = DriverManager.getConnection("jdbc:mysql://" + URL, USERNAME , PASSWORD);
f1sh
  • 11,489
  • 3
  • 25
  • 51
  • f1sh, I tried that many times, thought to go on that link in browser to check if it's sent to some page, for ex.: 127.8.238.2:3306/nautilius, answer: this page not available. Maybe problem in that, did I need get some info from my database by just typing this credencials? But I think no, all information is right, error can not be. – Rock Jan 02 '16 at 22:57
  • Opening that "link" in a browser is not going to show you anything since the sql instance on port 3306 does not speak http. That is not a valid test for anything... – f1sh Jan 04 '16 at 09:00
  • Thanks for clear answer, yeah I thought something similar that simply go to that link isn't good idea, on localhost yeah, but not for normal host. – Rock Jan 05 '16 at 12:49
1

First of all it is a bad practice to include any sensible information like users and passwords in your application, 12 Factor: Configuration, so I strongly advice against using the user/password/db name in the code directly. Also you shouldn't post real credentials anywhere, its a big security risk.

Now to answer your question, Openshift provides environment variables that contains these values. I haven't tried a MySQL database at Openshift, but did use a MongoDB, and it gave me the following variables:

$OPENSHIFT_MONGODB_DB_USERNAME
$OPENSHIFT_MONGODB_DB_PASSWORD
$OPENSHIFT_MONGODB_DB_DATABASE
$OPENSHIFT_MONGODB_DB_HOST
$OPENSHIFT_MONGODB_DB_PORT

So it is probably going to give you the same variables for MySQL, in order to make sure, you can ssh into your application and inspect the environment variables, using the rhc tool, you can do it like this:

rhc ssh -a <app-name> [-l <username>]

where the <app-name> represents the name of your application in openshift, the one used when you first created your app.

Optionally, if you didnt configure the rhc tool to use your default openshift credentials, or if you work with multiple accounts, then the account name must be provided with -l and your Openshift login. After issuing the command, you will be asked to input your password too, if it isn't stored in session.

Once you have a shell to your application, the .env directory stores the system environment variables, as files, each file represents a variable, the file name is the variable name, and the file content is the variable value.

ls .env

will list all the environment variables, so look for all the files that start with OPENSHIFT_MYSQL_, because these are containing the values for the environment variables with the credentials to connect to your database.

And finally then you can use these variables with:

System.getenv('OPENSHIFT_MYSQL_DB_USERNAME') 
System.getenv('OPENSHIFT_MYSQL_DB_PASSWORD')
// and so on with all the variables that you need

Update:

there is also the rhc env-list command that lists all the environment variables in your application, which also requires the -a and -l parameters.

Edit:

Edited a few parts to remove the confusion and complete the answer a bit more.

saljuama
  • 2,906
  • 1
  • 20
  • 42
  • I found on official Openshift page that I realy can use rhc tool, but can u tell me why `rhc ssh -a ` command didint work for me? I'm typing this in PuTTY terminal, I have fully configured it, all rhc tools are installed [link how.](https://www.youtube.com/watch?time_continue=233&v=0jj8fLiO1Bs). `ls .env` giving me all the environment variables, but instead of `$OPENSHIFT_MYSQL_xxx` it's giving me just `OPENSHIFT_MYSQL_DB_HOST, OPENSHIFT_MYSQL_DB_PORT` etc.... I guess that's, cause didint get first command, dunno why. And the last comm not found :D. I'll try fix that. – Rock Jan 03 '16 at 00:09
  • **Update:** maybe I wrongly typing command, here how I did: `rhc ssh -a nautilius`, or `rhc ssh -a ` or `rhc ssh -a `. @salvadorjuan, thank you for your advice to don't include any sensible information like users and passwords in public, I was so excited to learn and get all yours help. I'll edit post when I reach 10 badges, cause I'm new here. – Rock Jan 03 '16 at 00:17
  • if you didnt configure the rhc tool to have a default credentials, then a new parameter must be added `rhc ssh -a -l `, maybe that is why it didnt work, and the app name is just the app name, without `<` and `>`, and you don't need to include the whole domain. And yea, when i said `$OPENSHIFT_MYSQL_xxx` the `xxx` was due to lazy typing representing all the options, and the `$`was to point that it was a env var – saljuama Jan 03 '16 at 16:24
  • edited the anser a bit, trying to clarify a bit more :) – saljuama Jan 03 '16 at 16:37
0

I had similar problems. When I tested on my local machine and connected to my localhost mysql database it all worked fine but when I deployed it to Openshift it wouldnt work.

Eventually I found the problem to be that the table names in the Openshift mysql db began with a lowercase letter when in my java code my queries were written with an uppercase first letter for the table name, eg. 'Users', not 'users'. My local db was set up the same as the Openshift db but the problem is that my local is Windows and is not case sensitive whereas the Openshift server is. See here: Are table names in MySQL case sensitive?

"Database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix."

Community
  • 1
  • 1
Stackman
  • 129
  • 3
  • 14