MySQL & Express 연동(DDL, DML, DCL)

신동훈·2022년 8월 5일
0

Back-end

목록 보기
3/5

DDL

Data Definition Language
데이터베이스를 정의하는 언어.

  • create: 데이터베이스나 테이블을 생성
  • drop: 데이터베이스, 테이블 삭제
  • alter: 테이블 수정
  • truncate: 테이블 초기화

DML

Data Manipulation Language
데이터베이스 내 정보를 조회하거나 수정, 삭제하는 언어.

  • select: 데이터 조회
  • update: 데이터 수정
  • insert: 데이터 삽입
  • delete: 데이터 삭제

DLC

데이터베이스 접근 혹은 권한을 주는 언어.

  • grant: 특정 사용자에게 작업 권한 부여.
  • revoke: 특정 사용자에게 작업 권한 박탈.
  • commit: 트랜잭션 작업 저장.
  • rollback: 트랜잭션 작업 취소.

Express와 MySQL 설치

//database 생성
create database if not exists my_db; 

use my_db;

//table 생성
create table if not exists users (
	id varchar(45) not null,
    password varchar(45) not null,
    primary key (id));
    
//data 입력
insert into users (id, password) values ('test', '1234');

//data 조회
select password from users where id='test';
#app.js

const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',
    user: '< MySQL username >',
    password: '< MySQL password >',
    database: 'my_db'
});

connection.connect();

connection.query('SELECT * from users', (error, rows, field) => {
    if (error) throw error;
    console.log('User info is: ', rows);
});

connection.end();

발생한 오류

ER_ACCESS_DENIED_ERROR: user와 password 확인

ER_NOT_SUPPORTED_AUTH_MODE: MySQL Workbench에
ALTER USER '< MySQL username >'@'< MySQL 주소 >' IDENTIFIED WITH mysql_native_password BY '< MySQL password >';

#app.js 변경

const express = require('express');
const mysql = require('mysssql');
const dbconfig = require('./config/database.js');
const connection = musql.createConnection(dbconfig);

const app = express();

app.set('port', process.env.PORT || 3000);

app.get('/', (req, res) => {
  res.send('Root');
});

app.get('/users', (req, res) => {
  connection.query('select * from users', (error, rows) => {
    if (error) throw error;
    console.log('User onfo is: ', rows);
    res.send(rows);
  });
});

app.listen(app.get('port'), () => {
  console.log('Express server listening on port' + app.get('port'));
});
profile
독학 정리

0개의 댓글