appfog的大名我想不用说了,如果你不知道呢,可以问谷哥或度娘。其在2012年获得值得关注的十个云计算服务第一名。appfog为我们提供了云应用快速部署服务。利用其提供的平台,我们可以很方便的在上面搭建属于自己的应用。如wordpress 、phpbb、织梦等(其可布应用不止止只有PHP啊,只不过我其他网页语言应用比较陌生)。不过在使用免费的appfog服务时,其对我们设置了很多限制。比如上传文件大小不超过2M,内存使用不超过2G。除此之外,比如维护和管理数据库也是件麻烦事。不过一旦了解了其工作原理,维护数据库也不是件难事。本文主要介绍如何通过官方提供的tunnel进行连接管理。

1、安装管理环境

先从appfog官网下载ruby环境,安装完成后,打开Start Command Prompt with Ruby,安装af环境。

1gem update --system
2gem install af
3af login

注:以上是在windows环境下,如果是在linux/mac环境下,一般其自带都有ruby环境,可以直接通过以上命令安装af 环境。并登录管理。这里至于怎么安装应用就略过了,现以mysql为例直接说如何管理数据库 。

2、安装devkit

在windows下进行管理时,还需要安装devkit环境。在linux/mac 下可以直接略过该步骤。没有该软件,无法进行tunnel连接数据库,其提示如下:

1C:Documents and SettingsAdministrator>af tunnel 361way
2To use `af tunnel', you must first install Caldecott:
3        gem install caldecott
4Note that you'll need a C compiler. If you're on OS X, Xcode
5will provide one. If you're on Windows, try DevKit.
6This manual step will be removed in the future.
7Error: Caldecott is not installed.
8http://cloud.github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe

上面的错误提示已经说的很明白了,从那里下载devkit应用,并安装caldecott 。不过上面的提示还有点太过简单了。devkit下载好以后实际上是一个7z压缩文件。执行exe时其会提示让解压到何处。如解压到c:/devkit。(注意:目录不能有空格)在安装目录下运行ruby dk.rb,然后按照提示分别运行ruby dk.rb init 和 ruby dk.rb install来增强ruby 。

完成了devkit的安装,就可以通过gem install caldecott命令安装tunnel管理所需的包了。大致如下:

 1C:webkit>gem install caldecott
 2Temporarily enhancing PATH to include DevKit...
 3Building native extensions.  This could take a while...
 4Fetching: em-http-request-0.3.0.gem (100%)
 5Building native extensions.  This could take a while...
 6Fetching: em-websocket-0.3.8.gem (100%)
 7Fetching: rack-1.5.2.gem (100%)
 8Fetching: rack-protection-1.3.2.gem (100%)
 9Fetching: tilt-1.3.3.gem (100%)
10Fetching: sinatra-1.3.5.gem (100%)
11Fetching: async_sinatra-0.5.0.gem (100%)
12Fetching: json-1.6.8.gem (100%)
13Building native extensions.  This could take a while...
14Fetching: caldecott-0.0.5.gem (100%)
15Successfully installed escape_utils-0.3.2
16Successfully installed em-http-request-0.3.0
17Successfully installed em-websocket-0.3.8
18Successfully installed rack-1.5.2
19Successfully installed rack-protection-1.3.2
20Successfully installed tilt-1.3.3
21Successfully installed sinatra-1.3.5
22Successfully installed async_sinatra-0.5.0
23Successfully installed json-1.6.8
24Successfully installed caldecott-0.0.5
2510 gems installed

3、进行tunnel连接

 1C:webkit>af services
 2//查看目前的mysql服务名
 3============== System Services ==============
 4+------------+---------+-------------------------------+
 5| Service    | Version | Description                   |
 6+------------+---------+-------------------------------+
 7| mongodb    | 1.8     | MongoDB NoSQL store           |
 8| mysql      | 5.1     | MySQL database service        |
 9| postgresql | 9.1     | PostgreSQL database service   |
10| rabbitmq   | 2.4     | RabbitMQ message queue        |
11| redis      | 2.2     | Redis key-value store service |
12+------------+---------+-------------------------------+
13=========== Provisioned Services ============
14+--------------------+---------+--------+
15| Name               | Service | In     |
16+--------------------+---------+--------+
17| 361waytest-mysql-11892 | mysql   | ap-aws |
18+--------------------+---------+--------+
19//进行tunnel连接
20C:webkit>af tunnel 361waytest-mysql-11892
21Deploying tunnel application 'caldecott-ap-aws'.
22Uploading Application:
23  Checking for available resources: OK
24  Packing application: OK
25  Uploading (1K): OK
26Push Status: OK
27Binding Service [361way-mysql-11892]: OK
28Staging Application 'caldecott-ap-aws': OK
29Starting Application 'caldecott-ap-aws': OK
30Getting tunnel connection info: OK
31//显示用户名,密码
32Service connection info:
33  username : xxxxxxxxxxxxx
34  password : xxxxxxxxxxxxx
35  name     : xxxxxxxxxxxxxxxxxxxxxxxxxxxx
36  infra    : ap-aws
37Starting tunnel to 361way-mysql-11892 on port 10000.
381: none
392: mysql
403: mysqldump
41Which client would you like to start?: 1
42Open another shell to run command-line clients or
43use a UI tool to connect using the displayed information.
44Press Ctrl-C to exit...

连接步骤如上所示。至此已建立了远程主机和本机的连接,往下就可以通过navicat等mysql管理工具进行连接管理了。记得把主机连接信息输入为localhost,端口为10000 。

后记:除了以上的方法外,也可以通过phpmyadmin或者通过php等环境变量参数获取管理等方式。具体可以参看官方页面 。但个人还是比较喜欢tunnel这种方式。