0

I'd like to use MySQL from SilkJS under OSX Lion, but can't find the exact steps to get it to work. I followed the general instructions found here:

  1. Perform the SilkJS OSX Lion Quick Install
  2. Install MySQL
  3. Load MySQL from SilkJS REPL

Download/configure:

$ 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

Starting the SilkJS REPL, I ran:

$ 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

How can I run a simple MySQL query from SilkJS under Lion?

Community
  • 1
  • 1
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129

1 Answers1

1

Fresh web install of silkjs on OSX Lion:

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/

Now to see it works:

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

undefined
SilkJS> 

Note the console.dir() shows a function. The rest of the methods defined as properties of the function (constructor). So you must construct a MySQL() object:

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>

Now, in order to connect, you have to pass in host, user, passwd, and db. Or if you're running in the httpd environment, you set Config.mysql, something like this: Config.mysql = { host: 'localhost', // or some other host running MySQL server user: 'someuser', // username to authenticate in MySQL server passwd: 'whatever', // password of username in MySQL server db: 'database_name" // name of database in MySQL server to connect to };

Then the HTTP server will create a global SQL object available from request to request. This is automatic, you can just use SQL.getDataRows(), SQL.getScalar(), and so on.

mschwartz
  • 63
  • 3
  • No dice. I reinstalled SilkJS and connected as described above, using m.connect('localhost', 'root', '', 'test') on a clean mysql install containing the 'test' database. I still got the (apparent) error, followed by the segfault on invoking startTransaction(). I'm able to connect using the mysql5 client without issues: $ mysql5 -u root -p test – Rich Apodaca Jun 27 '12 at 14:09
  • I found the problem. I forgot to use 127.0.0.1 instead of localhost for the hostname. In other words: SilkJS> m.connect('127.0.0.1', 'root', '', 'test') Works great now. – Rich Apodaca Jun 27 '12 at 21:45