본문 바로가기
개발/node.js

node.js 에서 mysql 사용하기

by rudnine 2012. 3. 2.
반응형

출처 :
http://www.giantflyingsaucer.com/blog/?p=2596

Using NodeJS with MySQL

nodejs 진영의 라이브러리들은 계속해서 성장하고 있다. 이중 mysql을 사용할 수 있는 라이브러리를 하나 소개하려 하며, 그 이름은 node-mysql 이다. 나는(저자는) 우분투 10.10 을 사용했으며, nodejs를 아직 설치하지 않았다면, 설치한 이후에 이 글을 보도록 하시게. (굳이 번역할 필요는.. . 괜히 읽었나..)

Installing node-mysql

> npm install mysql

NodeSample이라 이름지은 테이블을 하나 생성해보자.

CREATE TABLE 'NodeSample'.'MyTable' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
'firstname' VARCHAR(20) NOT NULL,
'lastname' VARCHAR(20) NOT NULL,
'message' TEXT NOT NULL
) ENGINE = MYISAM;

Connecting to MySQL with NodeJS with node.mysql:

var sys = require('sys');
var Client = require('mysql').Client;
var client = new Client();

client.user = 'sumeuser';
client.password = 'password';

client.connect(function(error, results){
   if(error){
      console.log('Connection Error : ' + error.message);
      return;
   }
   console.log('Connected to MySQL');
});

연결이 만들어지면, 당신이 사용하고자 하는 MySQL 테이블을 설정하라.

ClientConnectionReady = function(client)
{
   client.query('USE NodeSample', function(error,results){
      if(error){
         console.log('ClientConnectionReady Erro : ' + error.message);
         client.end();
         return;
      }
   });
};

나머지는 다음에...

반응형

댓글