본문 바로가기

basic/Node.js

[Node.js] MySql 모듈이용해서 연결하기

//npm i mysql --s

var mysql = require('mysql');

var connection = mysql.createConnection({

host : 'localhost',

user : 'me',

password : 'secret',

database : 'my_db'

});

 

connection.connect();

 

///////////////////////////////////////connection을 conn으로 줄여서

var sql = 'SELECT * FROM topic';

conn.query(sql, function(err, rows, fiedls){

if(err){

console.log(err);

}else{

console.log('rows',rows);

console.log('fields',fiedls);

}

});

conn.end();