简体   繁体   English

使用JDBC连接到Openshift数据库

[英]Connect to Openshift database with JDBC

I spent a lot of time trying to figure out how to connect to Openshift's mysql database through my Java project. 我花了很多时间试图弄清楚如何通过我的Java项目连接到Openshift的mysql数据库。 I have created MySQL database, PhpMyAdmin and TomCat 7 cartriges. 我已经创建了MySQL数据库,PhpMyAdmin和TomCat 7 Cartriges。 I configured Putty, I even have setup ssh (don't know why) and get these information: 我配置了腻子,甚至设置了ssh(不知道为什么)并获取了以下信息:

environment variables with Ruby or PuTTY terminal Ruby或PuTTY终端的环境变量

Problem is with JDBC, I'm getting ERROR Communications link failure... my login information with jdbc in Eclipse, see bellow: 问题是与JDBC有关,我遇到了错误,错误是通信链接失败...我在Eclipse中使用jdbc的登录信息,请参见以下信息:

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. 我的代码在UwAmp localhost上运行,但是无法连接到Openshift。 Please help me, what's wrong. 请帮我,怎么了。

You are incorrectly (and unnneccessarily) using System.getenv . 您错误地(并且不必要地)使用System.getenv

System.getenv is used to get the values of environment variables. System.getenv用于获取环境变量的值。 Calling System.getenv("gxhaixUc9V94"); 调用System.getenv("gxhaixUc9V94"); is not giving you the password but rather null since there is most probably no environment variable with the name gxhaixUc9V94 . 是不是给你的密码而null ,因为有最有可能与名称的环境变量gxhaixUc9V94

Why do you use getenv at all!? 为什么要完全使用getenv !?

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);

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. 首先,在应用程序中包含任何明智的信息(例如用户名和密码)是一个坏习惯, 12因素:配置 ,所以我强烈建议您不要在代码中直接使用用户名/密码/ db名称。 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. 现在回答您的问题,Openshift提供了包含这些值的环境变量。 I haven't tried a MySQL database at Openshift, but did use a MongoDB, and it gave me the following variables: 我没有在Openshift上尝试过MySQL数据库,但确实使用了MongoDB,它为我提供了以下变量:

$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: 因此,它可能会为您提供与MySQL相同的变量,以确保您可以使用rhc工具将您的应用程序ssh到应用程序中并检查环境变量,您可以像这样进行操作:

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. 其中<app-name>代表openshift中应用程序的名称,这是您首次创建应用程序时使用的名称。

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. (可选)如果未将rhc工具配置为使用默认的openshift凭据,或者如果您使用多个帐户,则必须为帐户名提供-l和您的Openshift登录名。 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. 在拥有应用程序的外壳之后, .env目录将系统环境变量存储为文件,每个文件代表一个变量,文件名是变量名,文件内容是变量值。

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. 将列出所有环境变量,因此请查找所有以OPENSHIFT_MYSQL_开头的文件,因为这些文件包含环境变量的值以及用于连接数据库的凭据。

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. 还有一个rhc env-list命令,该命令列出了应用程序中的所有环境变量,这也需要-a-l参数。

Edit : 编辑

Edited a few parts to remove the confusion and complete the answer a bit more. 编辑了一些部分,以消除混乱并完成更多答案。

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. 当我在本地计算机上测试并连接到本地mysql数据库时,一切正常,但是当我将其部署到Openshift时,它将无法工作。

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. 最终,我发现问题是Openshift mysql db中的表名以小写字母开头,而在我的Java代码中,我的查询是以表名的大写第一个字母编写的,例如。 '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. 我的本地数据库的设置与Openshift数据库相同,但是问题是我的本地数据库是Windows,并且不区分大小写,而Openshift服务器却区分大小写。 See here: Are table names in MySQL case sensitive? 参见此处: MySQL中的表名是否区分大小写?

"Database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix." “数据库和表名在Windows中不区分大小写,在大多数Unix版本中也不区分大小写。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM