songining

๋‚˜๋Š” ๋‚ด๋ถ€ mysql์„ ์ด์šฉํ•˜์˜€๋‹ค.

 

* ํ„ฐ๋ฏธ๋„์—์„œ mysql ์‹คํ–‰ ๋ฐฉ๋ฒ•

1.  mysql.server start

2. mysql -uroot -p

3. ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ 

 

management ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ customerํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉ์ž๋“ค์„ ์ž„์˜๋กœ ์‚ฝ์ž…ํ•ด์ฃผ์—ˆ๋‹ค. 

์šฐ์„  npm install mysql์„ ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•˜์—ฌ ํ˜„์žฌ ํ”„๋กœ์ ํŠธ ํด๋”์— mysql ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์„œ๋ฒ„์— database.json ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด 

{

"host":"0.0.0.0",

"user":"user",

"password":"user ๋น„๋ฐ€๋ฒˆํ˜ธ",

"port":"3306",

"database":"management"

}

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  server.js ์—์„œ database.jsonํŒŒ์ผ์„ ์ฝ์–ด์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ์„ ์‹œ์ผœ์ค€ ํ›„ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ ์„œ๋ฒ„๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. (localhost:5000/api/cutomer)

const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 5000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const data = fs.readFileSync('./database.json');
const conf = JSON.parse(data);
const mysql = require('mysql');

const connection = mysql.createConnection({
    host: conf.host,
    user: conf.user,
    password: conf.password,
    port: conf.port,
    database: conf.database
});

connection.connect();

app.get('/api/customers',(req,res) => {
    connection.query(
        "select * from customer;",
        (err,rows,fields)=>{
            res.send(rows);
        }
    );
});

app.listen(port,()=> console.log(`Listening on port ${port}`));

๋‚˜๋Š” ์ฒ˜์Œ์— mysql ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†์ด ์•ˆ๋˜๋Š” ์˜ค๋ฅ˜๋ฅผ ๊ฒช์—ˆ๋Š”๋ฐ,

1. ALTER USER '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ณ„์ •๋ช…'@'localhost' IDENTIFIED WITH mysql_native_password BY '๋น„๋ฐ€๋ฒˆํ˜ธ';

2. flush privileges;

์œ„์™€ ๊ฐ™์ด mysql ์›Œํฌ๋ฐด์น˜์— ์ž…๋ ฅํ•˜๋‹ˆ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค.  database.json์— ์ž…๋ ฅํ•œ ๊ณ„์ •๊ณผ ํ˜„์žฌ ์ž์‹ ์˜ ๊ณ„์ •์ด ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. 

 

์ฐธ๊ณ : www.youtube.com/watch?v=T5dwM_j8wmU&list=PLRx0vPvlEmdD1pSqKZiTihy5rplxecNpz&index=11

'WEB > REACT' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.REST API ๊ตฌ์ถ•ํ•˜๊ธฐ  (0) 2021.01.24
2. material-ui ์‹œ์ž‘ํ•˜๊ธฐ  (0) 2021.01.18
1. ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ๊ฐ’ ์ „๋‹ฌ  (0) 2021.01.14