> 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
constmysql=require("mysql2");constconnection=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.jsUser info is ...โโโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโโ (index) โ id โ password โโโโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโโคโ 0 โ 'song' โ '1234' โโโโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโโ
$ 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-cliadded 81 packages, and audited 82 packages in 5sโ $mysql sequelize initSequelize 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์์ ๊ด๋ฆฌํ๋ค.
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 inset (0.00 sec)