카테고리 없음

Mysql node.js 연결 (mysql workbench 설치)

Canyi 2022. 11. 16. 16:00

 

 

workbench 설치하기 (google에 workbench)

나는 d drive에 받겠다.

 

 

my connection + 

 

todo가 생성됨

스키마에서 전체정보 확인가능

 

 npm install -save node-mysql

 

 

webserver.js 에서 Mysql 연결

//MySql 
const mysql = require('mysql');
const conn = mysql.createConnection({
    host : 'localhost',
    user: 'root',
    password : '비밀번호',
    database : 'node_db'
    
});

conn.connect();
conn.query('select * from todo', function (err, rows, fields){
            if (err) throw err;
            console.log(rows);
  });



const express = require('express');
const app = express();
//app.use : 미들웨어
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended : true}));


//설정할수 있는 port : 65535종류
//누군가가 /webtoon 방문하면 웹툰 관련 안내문을 띄워주자
//라우터 get.post


app.get('/game', function (req , res) {
    
    res.send('게임을 서비스해주는 페이지입니다.');

})


app.get('/webtoon', function (req , res) {
    
    res.send('웹툰을 볼수 있는 페이지입니다.');

})

app.get('/', function (req , res) {
    
    //res.send('홈입니다.');
    res.sendFile(__dirname + '/index.html');

})

app.get('/write', function (req , res) {
    
    //res.send('홈입니다.');
    res.sendFile(__dirname + '/write.html');

})

//add 경로 post 요청 (write.html)
app.post('/add',(req, res) => {
    //console.log(req);
    console.log(req.body.title);
    console.log(req.body.date);

    console.log("Connected!");
    //Mysql DB
    //방법1
    var sql  = `INSERT INTO todo (title, curdate) VALUES ('${req.body.title}', '${req.body.date}')`;
    conn.query(sql, function (err, result) {
        if (err) throw err;
        console.log("1 record inserted");
      });
    res.send('전송완료');

    

})

app.listen(8080, function() {
    
    console.log('listening on 8080');

});

 

port가 3306이라는 에러가 뜸

todo를 접속 할거라서  아래처럼 변경

const mysql = require('mysql');
const conn = mysql.createConnection({
    // host : 'localhost',
    // user: 'root',
    // password : '비밀번호',
    // database : 'node_db'
    
    host : '127.0.0.1',
    port: '3306',
    user: 'root',
    password : '비밀번호',
    database : 'node_db'
});

 

http://localhost:8080/write 페이지로 접속해서  데이터를 TODO 테이블로 넣어본다.

테이블 조회

log에도 잘 들어갔다.

//MySql 
const mysql = require('mysql');
const conn = mysql.createConnection({
    // host : 'localhost',
    // user: 'root',
    // password : '비밀번호',
    // database : 'node_db'
    
    host : '127.0.0.1',
    port: '3306',
    user: 'root',
    password : '비밀번호',
    database : 'node_db'
});

conn.connect();
conn.query('select * from todo', function (err, rows, fields){
            if (err) throw err;
            console.log(rows);
  });




const express = require('express');
const app = express();
//app.use : 미들웨어
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended : true}));


//설정할수 있는 port : 65535종류
//누군가가 /webtoon 방문하면 웹툰 관련 안내문을 띄워주자
//라우터 get.post


app.get('/game', function (req , res) {
    
    res.send('게임을 서비스해주는 페이지입니다.');

})


app.get('/webtoon', function (req , res) {
    
    res.send('웹툰을 볼수 있는 페이지입니다.');

})

app.get('/', function (req , res) {
    
    //res.send('홈입니다.');
    res.sendFile(__dirname + '/index.html');

})

app.get('/write', function (req , res) {
    
    //res.send('홈입니다.');
    res.sendFile(__dirname + '/write.html');

})

//add 경로 post 요청 (write.html)
app.post('/add',(req, res) => {
    //console.log(req);
    console.log(req.body.title);
    console.log(req.body.date);

    console.log("Connected!");
    //Mysql DB
    //방법1
    var sql  = `INSERT INTO todo (title, curdate) VALUES ('${req.body.title}', '${req.body.date}')`;
    conn.query(sql, function (err, result) {
        if (err) throw err;
        console.log("1 record inserted");
      });
    res.send('전송완료');

    //방법2 ? 입력방법 찾아보기
    // var sql = 'INSERT INTO todo VALUES ( ?, ?)';
    // var values1 = req.body.title;
    // var values2 = req.body.date;
    // conn.query(sql,values1,values2, function (err, result) {
    //     if (err) throw err;
    //     console.log("1 record inserted");
    //   });
    // res.send('전송완료');

    

})

app.listen(8080, function() {
    
    console.log('listening on 8080');

});


// app.get('/list', function (req , res) {
    
//     //res.send('홈입니다.');
    
//     conn.query('select * from todo', function (err, rows, fields){
//         if (err) throw err;
//         res.send(rows);
// });
//     res.sendFile('db에 데이터를 보냅니다.');

// });