I have created a MongoDB
instance in OpenShift
. I can connect to it via RockMongo
, which is a service offered by OpenShift
.
我在OpenShift中创建了一个MongoDB实例。我可以通过RockMongo连接到它,这是OpenShift提供的服务。
I'm trying to connect to my instance using JAVA
, but I just receive a Connection refuesed
error. Moreover, I cannot connect it using RoboMongo
.
我正在尝试使用JAVA连接到我的实例,但我收到一个Connection refuesed错误。而且,我无法使用RoboMongo连接它。
In my RockMongo
status tab, I see the following information:
在我的RockMongo状态选项卡中,我看到以下信息:
Host: 127.11.201.2
Port: 27017
Using RoboMongo
with MongoLab
instance works just fine giving it the right credentials, but here with OpenShift
it fails on connecting to the instance.
将RoboMongo与MongoLab实例一起使用可以很好地为它提供正确的凭据,但是在这里使用OpenShift时它无法连接到实例。
In my JAVA
app I'm trying the following:
在我的JAVA应用程序中,我正在尝试以下方法:
MongoCredential credential = MongoCredential.createCredential(
Const.MONGO_USERNAME, Cont.MONGO_DB,
Const.MONGO_PASSWORD.toCharArray());
mongo = new MongoClient(new ServerAddress(Const.MONGO_URI), Arrays.asList(credential));
With 127.11.201.2
as MONGO_URI
. Why am I failing to connect to my instance? What am I doing wrong?
使用127.11.201.2作为MONGO_URI。为什么我无法连接到我的实例?我究竟做错了什么?
P.S using putty
I am able to connect to my mongo
instance by just executing the command mongo
.
P.S使用putty我可以通过执行命令mongo连接到我的mongo实例。
1 个解决方案
#1
OpenShift provides environment variables, which you should use to connect to your MongoDB.
OpenShift提供环境变量,您应该使用它们连接到MongoDB。
-
OPENSHIFT_MONGODB_DB_HOST
The MongoDB IP address -
OPENSHIFT_MONGODB_DB_PORT
The MongoDB port -
OPENSHIFT_MONGODB_DB_USERNAME
The MongoDB username -
OPENSHIFT_MONGODB_DB_PASSWORD
The MongoDB password -
OPENSHIFT_MONGODB_DB_URL
The MongoDB connection URL (e.g.mongodb://<username>:<password>@<hostname>:<port>/
)
OPENSHIFT_MONGODB_DB_HOST MongoDB IP地址
OPENSHIFT_MONGODB_DB_PORT MongoDB端口
OPENSHIFT_MONGODB_DB_USERNAME MongoDB用户名
OPENSHIFT_MONGODB_DB_PASSWORD MongoDB密码
OPENSHIFT_MONGODB_DB_URL MongoDB连接URL(例如mongodb:// <用户名> : <密码> @ <主机名> : <端口> /)
I'm using one line of code to connect to the database:
我正在使用一行代码连接到数据库:
new MongoClient(new MongoClientURI(System.getenv("OPENSHIFT_MONGODB_DB_URL")));
#1
OpenShift provides environment variables, which you should use to connect to your MongoDB.
OpenShift提供环境变量,您应该使用它们连接到MongoDB。
-
OPENSHIFT_MONGODB_DB_HOST
The MongoDB IP address -
OPENSHIFT_MONGODB_DB_PORT
The MongoDB port -
OPENSHIFT_MONGODB_DB_USERNAME
The MongoDB username -
OPENSHIFT_MONGODB_DB_PASSWORD
The MongoDB password -
OPENSHIFT_MONGODB_DB_URL
The MongoDB connection URL (e.g.mongodb://<username>:<password>@<hostname>:<port>/
)
OPENSHIFT_MONGODB_DB_HOST MongoDB IP地址
OPENSHIFT_MONGODB_DB_PORT MongoDB端口
OPENSHIFT_MONGODB_DB_USERNAME MongoDB用户名
OPENSHIFT_MONGODB_DB_PASSWORD MongoDB密码
OPENSHIFT_MONGODB_DB_URL MongoDB连接URL(例如mongodb:// <用户名> : <密码> @ <主机名> : <端口> /)
I'm using one line of code to connect to the database:
我正在使用一行代码连接到数据库:
new MongoClient(new MongoClientURI(System.getenv("OPENSHIFT_MONGODB_DB_URL")));