0

我想在 OSX Lion 下使用SilkJS的 MySQL ,但找不到让它工作的确切步骤。我遵循了此处的一般说明:

  1. 执行 SilkJS OSX Lion 快速安装
  2. 安装 MySQL
  3. 从 SilkJS REPL加载 MySQL

下载/配置:

$ curl http://silkjs.org/install-osx.sh | sh
$ sudo port install mysql5-server +mysql5
$ sudo -u _mysql mysql_install_db5
$ sudo port load mysql5-server

启动 SilkJS REPL,我运行:

$ silkjs 
SilkJS> var MySQL = require('MySQL');
undefined
SilkJS> var SQL = new MySQL();
undefined
SilkJS> SQL.connect();
interpreter line 19 (main):
 (object) :

SilkJS> SQL.startTransaction();
Caught Signal 11 for process: 77312

如何在 Lion 下从 SilkJS 运行简单的 MySQL 查询?

4

1 回答 1

1

在 OSX Lion 上全新的 Silkjs 网络安装:

mschwartz@dionysus:~/src$ curl http://silkjs.org/install-osx.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1666  100  1666    0     0   3553      0 --:--:-- --:--:-- --:--:-- 79333
Installing SilkJS in /usr/local/silkjs

You may be asked to enter your password.
This is so the /usr/local directory can be made writable for the install.

Password:
Downloading SilkJS
######################################################################## 100.0%
Installation complete.

You need to add /usr/local/bin to your PATH environment variable.
This can be done by adding this line to your .bashrc file:
export PATH=/usr/local/bin:$PATH

You can run SilkJS with the following command:
$ silkjs

You can run the SilkJS HTTP Server with the following command:
$ httpd-silk.js yourapp.js

For instructions on setting up a WWW site for httpd-silk.js, see
http://silkjs.org/http-server/

现在看看它的工作原理:

mschwartz@dionysus:~/src$ silkjs
SilkJS> console.dir(require('MySQL'));
undefined line 1 (eval):
 function () {
    this.queryCount = 0;
    this.handle = null;
}

undefined
SilkJS> 

请注意,console.dir() 显示了一个函数。其余方法定义为函数(构造函数)的属性。所以你必须构造一个 MySQL() 对象:

SilkJS> var mysql = require('MySQL');
SilkJS> m = new mysql();
SilkJS> console.log(m.connect)
function (host, user, passwd, db) {
        if (!this.handle) {
            host = host || Config.mysql.host;
            user = user || Config.mysql.user;
            passwd = passwd !== undefined ? passwd : Config.mysql.passwd;
            db = db || Config.mysql.db;
            this.handle = mysql.connect(host, user, passwd, db);
        }
    }
undefined
SilkJS>

现在,为了连接,您必须传入主机、用户、密码和数据库。或者,如果您在 httpd 环境中运行,则设置 Config.mysql,如下所示: Config.mysql = { host: 'localhost', // 或其他运行 MySQL 服务器的主机 user: 'someuser', // 用户名在 MySQL 服务器中进行身份验证 passwd: 'whatever', // MySQL 服务器中用户名的密码 db: 'database_name" // MySQL 服务器中要连接的数据库名称 };

然后 HTTP 服务器将创建一个可从请求到请求的全局 SQL 对象。这是自动的,您可以只使用 SQL.getDataRows()、SQL.getScalar() 等。

于 2012-06-27T01:33:45.667 回答