Nodejs Mysql ๊ณผ Serialize
๋ค์ด๊ฐ๋ฉด์
MySQL์ ์ธ๊ณ์์ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ ์คํ ์์ค์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌ ์์คํ ์ด๋ค. MariaDB๋ ์คํ ์์ค์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ด๋ฆฌ ์์คํ ์ด๋ค. MySQL๊ณผ ๋์ผํ ์์ค ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ฉฐ, GPL v2 ๋ผ์ด์ ์ค๋ฅผ ๋ฐ๋ฅธ๋ค. ์ค๋ผํด ์์ ์ ํ์ฌ ๋ถํ์คํ MySQL์ ๋ผ์ด์ ์ค ์ํ์ ๋ฐ๋ฐํ์ฌ ๋ง๋ค์ด์ก์ผ๋ฉฐ, ๋ฐฐํฌ์๋ ๋ชฌํฐ ํ๋ก๊ทธ๋จ AB์ ์ ์๊ถ์ ๊ณต์ ํด์ผ ํ๋ค. ๋ง๋ฆฌ์DB๋ MySQL๊ณผ ์์ค์ฝ๋๋ฅผ ๊ฐ์ด ํ๋ฏ๋ก ์ฌ์ฉ๋ฐฉ๋ฒ๊ณผ ๊ตฌ์กฐ๊ฐ MySQL๊ณผ ๋์ผํ๋ค. ๊ทผ๋ณธ์ ์ผ๋ก ์ค๋ผํด์์ ์์ ๋กญ๋ค.
MySQL Setting
mysql> CREATE DATABASE MY_DB;
Query OK, 1 row affected (0.01 sec)
mysql> use my_db;
Database changed
mysql> create table users(
-> id varchar(45) not null,
-> password varchar(45) not null,
-> primary key (id));
Query OK, 0 rows affected (0.01 sec)
mysql> Insert into Users(id, password) values ('song','1234');
Query OK, 1 row affected (0.01 sec)
mysql> select * from users;
+------+----------+
| id | password |
+------+----------+
| song | 1234 |
+------+----------+
1 row in set (0.00 sec)
Node JS
Nodejs ๋ ์ด๋ฒคํธ ๊ธฐ๋ฐ์ ํ๋ซํผ. ๋ ธ๋์์ ์ผ์ด๋๋ ๋ชจ๋ ์ผ์ ์ด๋ค ์ด๋ฒคํธ์ ๋ํ ๋ฐ์. ๋ชจ๋ ์ผ๋ จ์ ์ฝ๋ฐฑ์ด๋ค. libuv๋ผ๋ ์ถ์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ด๋ฒคํธ ๋ฃจํ ๊ธฐ๋ฅ์ ์ ๊ณต. ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ ์ค๋ ๋๋ ๋จ ํ๋, ์ด ์ค๋ ๋๊ฐ ๋ฐ๋ก ์ด๋ฒคํธ ๋ฃจํ๊ฐ ์คํ๋๋ ์ค๋ ๋.
๋ชจ๋ CPU ํ์ฉ
Node.js ์ดํ๋ฆฌ์ผ์ด์ ์ ์ฑ๊ธ ์ค๋ ๋๋ก ์๋ํฉ๋๋ค. ๋ฉํฐ์ฝ์ด ํ๊ฒฝ์์ 1๊ฐ์ Node.js ์ดํ๋ฆฌ์ผ์ด์ ์ ํจ์จ์ ์ผ๋ก ์๋ํ์ง ์์ต๋๋ค. ๋ญ๋น๋๋ CPU๊ฐ ์๊ธฐ ๋๋ฌธ์ ๋๋ค. Cluster Module์ ์ฌ์ฉํ๋ฉด, CPU ๋ง๋ค child ํ๋ก์ธ์ค๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. ๊ฐ๊ฐ์ child ํ๋ก์ธ์ค๋ ๊ฐ์ ์์ ๋ง์ ์ด๋ฒคํธ๋ฃจํ๊ฐ ์กด์ฌํ๊ณ master ํ๋ก์ธ์ค๋ ๋ชจ๋ ์์๋ค์๊ฒ ์์ฒญ์ ๋ถ์ฐ์์ผ ์ค๋๋ค.
์ค๋ ๋ ํ ์กฐ์
์์ ์ธ๊ธํ๋ฏ์ด, libuv๋ ์ค๋ ๋ 4๊ฐ๋ก ์ค๋ ๋ ํ์ ์์ฑํฉ๋๋ค. ์ค๋ ๋ ํ์ ๊ธฐ๋ณธ ํฌ๊ธฐ๋ UV_THREADPOOL_SIZE
ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํด์ ์์ ํ ์ ์์ต๋๋ค. ์ด ๋ฐฉ๋ฒ์ I/O ์์
์ด ๋ง์ ์ดํ๋ฆฌ์ผ์ด์
์์ ๋์์ด ๋ ์ ์๊ฒ ์ง๋ง, ํฐ ์ค๋ ๋ ํ์ ๋ฉ๋ชจ๋ฆฌ๋ CPU๋ฅผ ๊ณ ๊ฐ์ํฌ ์ ์์์ ๊ธฐ์ตํด์ผ ํฉ๋๋ค.
์์
์ ๋ค๋ฅธ์๋น์ค์ ๋งก๊ธฐ๊ธฐ
๋ง์ฝ Node.js๊ฐ CPU์ฌ์ฉ์ด ๊ณผ๋ํ๊ฒ ํ์ํ ์์ ์์ ์ฌ์ฉ๋๋ค๋ฉด, ์ด ํน์ ์์ ์ ๋ ์๋ง๋ ๋ค๋ฅธ ์ธ์ด๋ฅผ ์ ํํด์ ๊ทธ ์ชฝ์ผ๋ก ์ฒ๋ฆฌ๋ฅผ ์ฎ๊ฒจ ์์ ๋์ ์ค์ด๋ ๊ฒ์ด ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ผ ์ ์์ต๋๋ค.
package.json
์ ํ๋ก์ ํธ ์ ๋ณด์ ์์กด์ฑ(dependencies)์ ๊ด๋ฆฌํ๋ ๋ฌธ์
์ด๋ฏธ ์์ฑ๋ package.json
๋ฌธ์๋ ์ด๋ ๊ณณ์์๋ ๋์ผํ ๊ฐ๋ฐ ํ๊ฒฝ์ ๊ตฌ์ถํ ์ ์๊ฒ ํด์ค๋ค.
JSON ํฌ๋งท์ผ๋ก ์์ฑํด์ผ ํ๋ฉฐ, ๋ค์๊ณผ ๊ฐ์ ์ต์
๋ค์ด ์ถ๊ฐ๋ ์ ์์ต๋๋ค.
$ npm init
package name
version
description
entry point
test command
git repository
keywords
author
license
$ npm init -y
โ mysql npm init
Press ^C at any time to quit.
package name: (mysql) nodejs-mysql
version: (1.0.0)
description: mysql-nodejs exercise
entry point: (index.js)
test command: node index.js
git repository:
keywords:
author: rumblekat
license: (ISC)
About to write to /Users/songmyeongjin/Desktop/mysql/package.json:
{
"name": "nodejs-mysql",
"version": "1.0.0",
"description": "mysql-nodejs exercise",
"main": "index.js",
"scripts": {
"test": "node index.js"
},
"author": "rumblekat",
"license": "ISC"
}
Is this OK? (yes)

ํ
์คํธ ์ฝ๋ ์์ฑ ํ npm test ์คํ
โ mysql npm test
> nodejs-mysql@1.0.0 test
> node index.js
hello world
NodeJS์ MySQL ์ฐ๋
createConnection ๋ฉ์๋์ ์ธ์๋ก ์ ๋ฌ๋๋ ๊ฐ์ฒด์ ์์ ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๋ณด(์ ์ ๋ช ๊ณผ ํจ์ค์๋ ๋ฑ)์ ์ ๋ ฅํด์ผํ๋ค.
> node index.js
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module 'mysql2' ---> ๋ชจ๋์ ์ฐพ์ ์ ์์ ๊ฒฝ์ฐ, npm install <Module> --save ๋ก ํด๋น ์์กด์ฑ์ ๋ถ๋ฌ์จ๋ค.
Require stack:
- /Users/songmyeongjin/Desktop/mysql/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/songmyeongjin/Desktop/mysql/index.js:1:15)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/songmyeongjin/Desktop/mysql/index.js' ]
}
โ $mysql npm install mysql2 --save
const mysql = require("mysql2");
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
port : 3306,
password : 'Init123$',
database : 'my_db'
});
connection.connect();
connection.query('select * from users',(err,rows,fields)=>{
if(err) throw err;
console.log('User info is ...');
console.table(rows);
});
connection.end();
Mysql ์ฐ๋ ํ์ธ
โ $mysql npm test
> nodejs-mysql@1.0.0 test
> node index.js
User info is ...
โโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโ
โ (index) โ id โ password โ
โโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโค
โ 0 โ 'song' โ '1234' โ
โโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโ
Nodejs ์น์๋ฒ
Express vs Koa
Express ๋ง๋ ํ์ด 2016๋ ์ Koa๋ฅผ ๋ง๋ฌ
Express๋ ๋ผ๋๋ก ์ค์น๋๋ ๋ชจ๋์ด ๋ง๊ณ , ๋ฏธ๋ค์จ์ด๋ฅผ ๋ถ์ผ ๋ ๊ผญ Express ๊ธฐ๋ฐ
Koa๋ ๋ผ๋๋ก ์ค์น๋๋ ๋ชจ๋์ด ์ ๊ณ , ์ปค์คํฐ๋ง์ด์ง์ด ์์ ๋กญ๋ค.
Koa๋ ํ์์ด ES6, Async/Await์ ์ง์
Express๋ community๊ฐ ๊ฐ
Express
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`Express is listening to <http://localhost>:${PORT});
});
Koa
const koa = require('koa');
const app = express();
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`Koa is listening to <http://localhost>:${PORT});
});
Middleware
Express
app.use((req, res, next) => {
console.log(`Time : ${Date.now()}`);
next();
});
Koa
app.use(async (ctx, next) => {
console.log(`Time: ${Date.now()}`);
await next();
});
Express
Express๋ ์น ๋ฐ ๋ชจ๋ฐ์ผ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ํ ์ผ๋ จ์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ฐ๊ฒฐํ๊ณ ์ ์ฐํ Node.js ์น ์ ํ๋ฆฌ์ผ์ด์ ํ๋ ์์ํฌ.
require๋ nodeJS์์ ๋ค๋ฅธ ํจํค์ง๋ฅผ ๋ถ๋ฌ์ฌ ๋ ์ฌ์ฉ๋๋ ํค์๋. NODE_PATH ํ๊ฒฝ ๋ณ์์ ์ค์ ํ ์์น์์ express๋ผ๋ ๋ชจ๋์ ์ฐพ๋๋ค.
app์ด๋ผ๋ ๋ณ์์ express ํจ์์ ๋ฐํ๊ฐ์ ์ ์ฅ / REST End Point ์์ฑ
Process.env๋ nodejs์์ ํ๊ฒฝ ๋ณ์๋ฅผ ๊ฐ์ ธ์ฌ๋ ์ฌ์ฉ๋จ, ํ์ฌ default๋ 3000
app.get / get ์์ฒญ์ผ๋ก ์ ์ ์๋ํฌ์ธํธ๋ฅผ ์์ฑ
์๋ํฌ์ธํธ ์์ฑ์ ํ๋ผ๋ฏธํฐ๋ ๋๊ฐ์ง๋ฅผ ๋ฐ๋๋ฐ, ์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ URL ์ ์, ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ํด๋น url์์ ์ํํ ์์ ๋ฐ ์๋ต์ ์ ์
req(์์ฒญ)/ res(์๋ต)
module.exports = {
host : 'localhost',
user : 'root',
port : 3306,
password : 'Init123$',
database : 'my_db'
}
const mysql = require("mysql2");
const express = require('express');
const dbconfig = require('./config/database');
const connection = mysql.createConnection(dbconfig);
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.json({
success: true,
});
});
app.get('/users',(req,res)=>{
connection.query('SELECT * from users',(error, rows)=>{
if(error) throw error;
res.json(rows);
})
})
app.listen(port, () => {
console.log(`server is listening at localhost:${process.env.PORT}`);
});

POST Insert ๊ตฌ๋ฌธ ์ถ๊ฐ
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.post('/users',(req,res)=>{
const { id , pw } = req.body;
const sql = 'INSERT INTO users (id,password) VALUES(?,?)';
connection.query(sql,[id,pw],(err,rows,fields)=>{
if(err) return console.log(err);
return console.log(rows.insertId);
});
console.log(id, pw);
res.json({success: "s"});
});

mysql> select * from users;
+---------+----------+
| id | password |
+---------+----------+
| song | 1234 |
| song11 | 1234 |
| song112 | 1234 |
+---------+----------+
3 rows in set (0.00 sec)
Sequelize
ORM์ด๋ ๊ฐ์ฒด์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ๋ฅผ ์๋์ผ๋ก ๋งคํ(์ฐ๊ฒฐ)ํด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ณ , ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ํ ์ด๋ธ์ ์ฌ์ฉํ๋ค. ์ฆ ์ฌ๊ธฐ์ Sequelize๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฆด๋ ์ด์ ์ ๋งคํํด์ฃผ๋ ์ ์ฉํ ๋๊ตฌ๋ผ๋ ์๋ฏธ์ด๋ค! ์๋ฐ์คํฌ๋ฆฝํธ ๊ตฌ๋ฌธ์ ์์์ SQL๋ฌธ์ผ๋ก ๋ณ๊ฒฝํด์ค๋ค.
$ npm i sequelize mysql2
$ npm i -g sequelize-cli
$ sequelize init
โ $mysql npm i sequelize --save
added 17 packages, and audited 83 packages in 3s
โ $mysql npm i -g sequelize-cli
added 81 packages, and audited 82 packages in 5s
โ $mysql sequelize init
Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]
Created "config/config.json"
Successfully created models folder at "/Users/songmyeongjin/Desktop/mysql/models".
Successfully created migrations folder at "/Users/songmyeongjin/Desktop/mysql/migrations".
Successfully created seeders folder at "/Users/songmyeongjin/Desktop/mysql/seeders".
config/config.json
DB ์ฐ๊ฒฐ ๊ด๋ จ config ์ ๋ณด๋ ๋ชจ๋ config.json์์ ๊ด๋ฆฌํ๋ค.
{
"development": {
"username": "root",
"password": "Init123$",
"database": "database_development",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
Sequelize DB Create
Sequelize-cli๋ฅผ ์ด์ฉํด์ DB๋ฅผ ์์ฑํ๊ณ ์กฐ์ํ ์ ์์
โ $mysql sequelize db:create
Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]
Loaded configuration file "config/config.json".
Using environment "development".
Database database_development created.
mysql> show databases;
+----------------------+
| Database |
+----------------------+
| database_development | <--
| information_schema |
| MY_DB |
| mysql |
| performance_schema |
| sys |
+----------------------+
6 rows in set (0.01 sec)
Table ์์ฑ
Sequelize๋ฅผ ์ด์ฉํ์ฌ DDL์ ์๋์ ๊ฐ์ด ์์ฑํ ์ ์๋ค. (์ฃผ์ํด์ผํ ์ ์, --attribute๋ค์ ์ปฌ๋ผ์ ์ฝค๋ง๋ก ๊ตฌ๋ถํ๋ค.๋์ด์ฐ๊ธฐ์์ด)
$sequelize model:generate --name Point --attributes pid:integer,pntamt:integer,userId:string
model ํ์ผ๊ณผ, migration ํ์ผ์ด ์์ฑ
์์ฑํ Models์ /models
ํด๋ ์์ ์ ์ฅ๋๊ณ , migrations๋ /migrations
ํด๋ ์์ ์ ์ฅ๋๋ค. Migrationsํ์ผ ์์ ๋ถ์ ์ซ์๋ ์๊ฐ์ ๋ํ ์ ๋ณด๋ก sequelize๋ ์ด๋ฅผ ์ธ์ํด ์ด๋ค ๋ง์ด๊ทธ๋ ์
์ด ๋จผ์ ์ด๊ณ , ์ด๋ค ์์๋ก ์์ฑ๋์ด์ผ ํ ์ง์ ๋ํด ํ๋จํ๋ค. (์ง๊ธ์ ๋ณ๋ก ์ค์ํ์ง ์์ง๋ง, ๋์ฃผ์ฅ columns๊ณผ tables๋ฅผ ์ถ๊ฐํ ๋ ์ค์)
Sequelize CLI [Node: 12.18.3, CLI: 6.2.0, ORM: 6.6.2]
New model was created at /Users/songmyeongjin/Desktop/mysql/models/point.js .
New migration was created at /Users/songmyeongjin/Desktop/mysql/migrations/20210326125441-create-point.js .
mysql> desc points;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| pid | int | YES | | NULL | |
| pntamt | int | YES | | NULL | |
| userId | varchar(255) | YES | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
$ sequelize db:migrate --env development
Last updated
Was this helpful?