保持冷静和卷起你的袖子
你现在可能好奇,在标题里,怎么就没看到MongoDB和Node.js的呢? 别急…
我们已经看到了从管理界面上创建一个应用是多么容易的了, 但是自从我们成为一名开发者和爱情终结者? (难道不是?
) 我们注定要走不寻常的路去解决问题。 OpenShift 提供 OpenShift 客户端工具 rhc. 为了安装 rhc ,首先要安装Ruby, 而最容易的方式让它运行在Windows机器是要安装一个RubyInstaller:

只要按上面的大大下载按钮,你就知道怎么做了.但是有个重要的点, 就是当你安装到下面的页面,必须要勾上 Add Ruby executables to your PATH 选项:

安装之后,你就可以启动你的终端(我用的是 Console 2, 你可以看下我的配置: 在Windows机器自定义 Console 2 ) 然后运行:
gem install rhc
上面完成之后再运行:
rhc setup
(对于Windows用户,这一个很重要的部分,在安装 OpenShift RHC 客户端工具 官方文档说我们应该只要运行:
rhc
就行,但是在文章评论上说这是不起作用的).
现在我们可以用rhc去创建一个新的应用程序了。创建一个名为 myTest 并使用Node.js和MongoDB ,你要运行下面的命令:
rhc app create myTest nodejs-0.10 mongodb-2.4
你将看到类似下面的输出:
C:\Users\Nikola\Desktop>rhc app create myTest nodejs-0.10 mongodb-2.4
DL is deprecated, please use Fiddle
Application Options
-------------------
Domain: chavo
Cartridges: nodejs-0.10, mongodb-2.4
Gear Size: default
Scaling: no
Creating application 'myTest' ... done
MongoDB 2.4 database added. Please make note of these credentials:
Root User: admin
Root Password: ..bip..Bip..
Database Name: mytest
Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/
Waiting for your DNS name to be available ... done
Cloning into 'mytest'...
The authenticity of host 'mytest-chavo.rhcloud.com (54.235.21.98)' can't be established.
RSA key fingerprint is cf:ee:77:cb:0e:fc:02:d7:72:7e:ae:80:c0:90:88:a7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mytest-chavo.rhcloud.com,54.235.21.98' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/Nikola/.ssh/id_rsa':
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (17/17), done.
Receiving objects: 100% (21/21), 20.22 KiB, done.ceiving objects: 90% (19/21)
Your application 'mytest' is now available.
URL: http://mytest-chavo.rhcloud.com/
SSH to: 532812f94382eca22b000657@mytest-chavo.rhcloud.com
Git remote: ssh://532812f94382eca22b000657@mytest-chavo.rhcloud.com/~/git/mytest.git/
Cloned to: C:/Users/Nikola/Desktop/mytest
Run 'rhc show-app myTest' for more details about your app.
如果你关注到这句 ‘DL is deprecated, please use Fiddle‘,正如 on Stackoverflow 说的,只是一个警告而已 (或者你想探索更多,你可以先记下这个问题先).
如果报了错误,是说你没有某些(cartridge)组件?那必须相应的更改命令通过它给的一个可用组件的列表
C:\Users\Nikola\Desktop>rhc app create
DL is deprecated, please use Fiddle
When creating an application, you must provide a name and a cartridge from the
list below:
Short Name Full name
========== =========
diy-0.1 Do-It-Yourself 0.1
jbossas-7 JBoss Application Server 7
jbosseap-6 JBoss Enterprise Application Platform 6
jenkins-1 Jenkins Server
nodejs-0.10 Node.js 0.10
nodejs-0.6 Node.js 0.6
perl-5.10 Perl 5.10
php-5.3 PHP 5.3
zend-5.6 PHP 5.3 with Zend Server 5.6
php-5.4 PHP 5.4
zend-6.1 PHP 5.4 with Zend Server 6.1
python-2.6 Python 2.6
python-2.7 Python 2.7
python-3.3 Python 3.3
ruby-1.8 Ruby 1.8
ruby-1.9 Ruby 1.9
jbossews-1.0 Tomcat 6 (JBoss EWS 1.0)
jbossews-2.0 Tomcat 7 (JBoss EWS 2.0)
jboss-vertx-2.1 Vert.x 2.1
Please specify the name of the application and the web cartridge to install
Usage: rhc app-create <name> <cartridge> [... <cartridge>] [... VARIABLE=VALUE]
[-n namespace]
Pass '--help' to see the full list of options
继续rockin' on
MongoDB 是一个开源文档数据库, 也是一个主要NoSQL数据库. 为了添加一个 RockMongo ,一个用来与MongoDB交互和管理的web前端, 你需要执行这个命令:
rhc cartridge add rockmongo
之后将得到类似下面的输出:
C:\Users\Nikola\Desktop\mytest>rhc cartridge add rockmongo
DL is deprecated, please use Fiddle
Using rockmongo-1.1 (RockMongo 1.1) for 'rockmongo'
Adding rockmongo-1.1 to application 'mytest' ... done
rockmongo-1.1 (RockMongo 1.1)
-----------------------------
Gears: Located with mongodb-2.4, nodejs-0.10
Connection URL: https://mytest-chavo.rhcloud.com/rockmongo/
Please make note of these MongoDB credentials:
RockMongo User: admin
RockMongo Password: ..i.aint.telin'..
URL: https://mytest-chavo.rhcloud.com/rockmongo/
如果你通过用户名和密码,那RockMongo将提供一个简单但是有用的界面:

当然,如果你更喜欢Shell,你可以这样:
rhc ssh
然后通过命令行界面开始交互,输入:
mongo
:
[mytest-chavo.rhcloud.com 532812f94382eca22b000657]\> mongo
MongoDB shell version: 2.4.6
connecting to: 127.9.197.130:27017/admin
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
现在选择一个跟你应用同名的数据库。
> use mytest
switched to db mytest
像下面那样插入一些数据:
> var a = {"user":"nikola", points:10};
> db.scores.insert(a);
和检查一下是否成功插入:
> db.scores.find();
{ "_id" : ObjectId("53284189d6e5e4aedb1fbafd"), "user" : "nikola", "points" : 10 }
要用Node.js查询这些信息,首先必须用下面命令安装 mongojs
npm install mongojs
. 然后打开这个
server.js
文件 (它在你应用的根目录) ,然后加下面代码进去。
self.createRoutes
object:
self.routes['/db'] = function(req, res) {
var mongojs = require('mongojs');
var dbName = "/mytest";
var connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" + process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + dbName;
var db = mongojs(connection_string, ['scores']);
var books = db.collection('scores');
db.scores.find(function(err, docs) {
res.send(docs);
});
};
一切就绪
现在是时候加这些变更到暂存状态
git add .
(你可能想要检查 Git 是否有问题). 你可以用下面命令检查要提交文件的状态
git status
. 提交你要执行的变更:
git commit -a -m "Added code to talk to db"
最后
git push
这样变更都上传到 OpenShift 上面部署了.
现在,如果你完全按照这个教程来做, 打开链接 http://mytest-chavo.rhcloud.com/db (注意这里有个db, 如果你的链接无误,它将反映你的程序数据库 ) 你就能得到下面的输出:
下面的这条有用命令可以大概了解到你的应用的设置:
C:\Users\Nikola\Desktop\mytest>rhc app show
DL is deprecated, please use Fiddle
mytest @ http://mytest-chavo.rhcloud.com/ (uuid: 532812f94382eca22b000657)
--------------------------------------------------------------------------
Domain: chavo
Created: 10:33 AM
Gears: 1 (defaults to small)
Git URL: ssh://532812f94dd2eca22b000657@mytest-chavo.rhcloud.com/~/git/mytest.git/
SSH: 532812f94382eca22b000657@mytest-chavo.rhcloud.com
Deployment: auto (on git push)
mongodb-2.4 (MongoDB 2.4)
-------------------------
Gears: Located with nodejs-0.10, rockmongo-1.1
Connection URL: mongodb://$OPENSHIFT_MONGODB_DB_HOST:$OPENSHIFT_MONGODB_DB_PORT/
Database Name: mytest
Password: ..ccc...
Username: admin
nodejs-0.10 (Node.js 0.10)
--------------------------
Gears: Located with mongodb-2.4, rockmongo-1.1
rockmongo-1.1 (RockMongo 1.1)
-----------------------------
Gears: Located with mongodb-2.4, nodejs-0.10
Connection URL: https://mytest-chavo.rhcloud.com/rockmongo/
就这些了,是时候用它做些有用的东西了!

