如何将MongoDB连接到Openshift?

时间:2022-06-01 22:04:53

I have a NodeJS Express app that uses Mongoose for MongoDB. I'm confused as to how to connect it to the OpenShift database. For development, I'm connecting to a local database, which works fine. Here's what I have:

我有一个NodeJS Express应用,它使用Mongoose作为MongoDB。对于如何将它连接到OpenShift数据库,我感到很困惑。对于开发,我正在连接一个本地数据库,它工作得很好。这就是我有:

//====== MONGODB SETUP ======
mongo_url = process.env.OPENSHIFT_MONGODB_DB_HOST+":"+parseInt(process.env.OPENSHIFT_MONGODB_DB_PORT);

if(app.dev){
    mongo_url = "mongodb://localhost:27017/my-db";
}

app.modules.mongoose.connect(mongo_url);

Any help would be great!

任何帮助都将是伟大的!

2 个解决方案

#1


3  

You need more than just host and port. You have to provide username and password. A simpler way for it is to use:

您需要的不仅仅是主机和端口。你必须提供用户名和密码。一种更简单的方法是:

mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL;

OPENSHIFT_MONGODB_DB_URL has username and password too.

openshift_mongodb_url也有用户名和密码。

#2


1  

You have to provide username and password and the database name along with host and port.

您必须提供用户名和密码、数据库名以及主机和端口。

So you can try appending OPENSHIFT_APP_NAME to the OPENSHIFT_MONGODB_DB_URL.

因此,可以尝试将OPENSHIFT_APP_NAME附加到OPENSHIFT_MONGODB_DB_URL。

mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME;

OPENSHIFT_MONGODB_DB_URL has the below format:
(e.g. mongodb://<username>:<password>@<hostname>:<port>/)

#1


3  

You need more than just host and port. You have to provide username and password. A simpler way for it is to use:

您需要的不仅仅是主机和端口。你必须提供用户名和密码。一种更简单的方法是:

mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL;

OPENSHIFT_MONGODB_DB_URL has username and password too.

openshift_mongodb_url也有用户名和密码。

#2


1  

You have to provide username and password and the database name along with host and port.

您必须提供用户名和密码、数据库名以及主机和端口。

So you can try appending OPENSHIFT_APP_NAME to the OPENSHIFT_MONGODB_DB_URL.

因此,可以尝试将OPENSHIFT_APP_NAME附加到OPENSHIFT_MONGODB_DB_URL。

mongo_url = process.env.OPENSHIFT_MONGODB_DB_URL+process.env.OPENSHIFT_APP_NAME;

OPENSHIFT_MONGODB_DB_URL has the below format:
(e.g. mongodb://<username>:<password>@<hostname>:<port>/)