2024.03.21(목)
notes-db.yaml
: db라는 namespace에 Deployment 및 Service 생성notes-db-volume.yaml
: db라는 namespace에 PV 및 PVC 생성kubectl create namespace db
kubectl apply -f notes-db-volume.yaml
kubectl apply -f notes-db.yaml
kubectl get namespace
kubectl get all -n <namespace>
-n
, --namespace
옵션으로 네임스페이스 명시 (명시하지 않는 경우 default라는 namespace 사용)init-db.sql
: prgms_notes라는 데이터베이스 생성CREATE SCHEMA IF NOT EXISTS `prgms_notes` DEFAULT CHARACTER SET utf8mb4;
USE `prgms_notes`
-- 사용자 테이블 생성
CREATE TABLE IF NOT EXISTS users (
id INT NOT NULL AUTO_INCREMENT,
email VARCHAR(256) NOT NULL,
encrypted_password text NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX users_unique_email (email) USING BTREE
);
-- 노트 테이블 생성
CREATE TABLE IF NOT EXISTS notes (
id INT NOT NULL AUTO_INCREMENT,
title text NOT NULL,
content text NOT NULL,
user_id INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
CONSTRAINT note_user_id
FOREIGN KEY (user_id)
REFERENCES users (id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
init-user.sql
-- 작업할 데이터베이스를 mysql로 설정
USE mysql;
-- username: 'prgms', password: 'prgms'인 사용자를 'localhost'에서 접속 가능하도록 생성 (존재하지 않는 경우에만 생성)
CREATE USER IF NOT EXISTS 'prgms'@'localhost' IDENTIFIED BY 'prgms';
-- username: 'prgms', password: 'prgms'인 사용자를 '%'(모든 호스트)에서 접속 가능하도록 생성 (존재하지 않는 경우에만 생성)
CREATE USER IF NOT EXISTS 'prgms'@'%' IDENTIFIED BY 'prgms';
-- 'prgms_notes' 데이터베이스에 대한 모든 권한을 'localhost'에서 접속하는 'prgms' 사용자에게 부여
GRANT ALL PRIVILEGES ON prgms_notes.* TO 'prgms'@'localhost';
-- 'prgms_notes' 데이터베이스에 대한 모든 권한을 어떤 호스트에서든 접속 가능한 'prgms' 사용자에게 부여
GRANT ALL PRIVILEGES ON prgms_notes.* TO 'prgms'@'%';
-- 권한 변경 사항을 즉시 적용하기 위해 MySQL의 권한 캐시를 지우기
FLUSH PRIVILEGES;
init-test-db.sql
: 테스트 데이터 주입DROP DATABASE IF EXISTS `prgms_notes`;
CREATE SCHEMA `prgms_notes` DEFAULT CHARACTER SET utf8mb4;
USE `prgms_notes`
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(256) NOT NULL,
`encrypted_password` text NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_unique_email` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Table structure for table `notes`
--
CREATE TABLE `notes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`content` text NOT NULL,
`user_id` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT current_timestamp(),
`updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
KEY `note_user_id` (`user_id`),
CONSTRAINT `note_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--
-- Dumping data for table `users`
--
LOCK TABLES `users` WRITE;
INSERT INTO `users` VALUES (1,'test@example.com','$2b$10$432oW5OwXPcHPcmyQghkxeICMi65DGPdFnDv21dJ2QU3CSj.xFbi6');
UNLOCK TABLES;
--
-- Dumping data for table `notes`
--
LOCK TABLES `notes` WRITE;
INSERT INTO `notes` VALUES (1,'Test (1)','<p>This note is for testing.</p><p>Note number: 1</p>',1,'2024-01-24 05:47:47','2024-01-24 05:48:04'),(2,'Test (2)','<p>This note is for testing.</p><p>Note number: 2</p>',1,'2024-01-24 05:48:08','2024-01-24 05:48:23');
UNLOCK TABLES;
💡 mariadb server를 컴퓨터에 설치하고 mysql.exe 파일의 경로(ex.
C:\Program Files\MariaDB 10.11\bin
)를 시스템 환경 변수에 등록해야 mysql 명령어 사용 가능!!
mysql --protocol tcp -P 30036 -u root -p < init-user.sql
mysql --protocol tcp -P 30036 -u root -p < init-db.sql
mysql --protocol tcp -P 30036 -u prgms -p
mkdir backend
cd backend
npm init -y
npm i dotenv express express-async-errors
npm i -D typescript @types/express nodemon
전에는 직접
try-catch
wrapper를 만들어서 error handling에 사용했었는데 얘는 그냥 async로만 감싸주면 알아서 error를 error handler로 보내줘서 굉장히 편한 모듈인 것 같다.
이것뿐만 아니라 사용할 모듈 이름 앞에
@types/
를 붙여서 찾으면 typescript를 위해 정의해둔 type들이 나오기 때문에-D
(--save-dev
) 옵션으로 설치하면 된다.
package.json
, nodemon.json
, tsconfig.json
.env
파일에 기록 및 이것을 읽어들여 적용하는 settings.ts
파일app.ts
에 기본 응용 구현하고, 이것을 이용한 리스너를 index.ts
에 구현npm start
npm run build && serve -s build
(bash: serve: command not found 발생 시 npm install -g serve
후 다시 시도)저번 Book Store backend 프로젝트에서는
crypto
모듈을 사용해 비밀번호를 암호화했었다.
👉 작성했었던 🔒암호화 원리 관련 TIL
이번 backend 프로젝트에서는bcrypt
모듈을 사용하게 되었는데 두 모듈의 차이점에 대해 알아보자.
결론적으로 비밀번호와 같은 민감한 정보를 암호화할 때에는 bcrypt가 더 적합한 것 같다.
프로젝트 백엔드 폴더: https://github.com/do0ori/web-editor-project/tree/main/backend
저번 백엔드 프로젝트는 자바스크립트로 진행했었는데 이번에는 타입스크립트로 해서 좋다. 확실히 자바스크립트로 만들어 둔 프로젝트를 타입스크립트로 마이그레이션하는 것 보다는 처음부터 아예 타입스크립트로 시작하는 게 쉽고 좋은 것 같다.