Skip to content

nodejs mysql aws ec2 aws rds

egoing edited this page Nov 19, 2021 · 10 revisions

준비물

기본 코드를 작성합니다.

simple.js

var http = require('http');
var app = http.createServer(function(request,response){
    response.end(`
    <h1><a href="index.html">WEB</a></h1>
    <ul>
        <li><a href="html.html">html</a></li>
        <li><a href="nodejs.html">nodejs</a></li>
        <li><a href="mysql.html">mysql</a></li>
    </ul>
    <h2>Welcome</h2>
    Hello WEB!!    
    `);
});
app.listen(80);

실행합니다.

  1. node simple.js

pm2를 이용합니다.

  1. npm install --global pm2
  2. pm2 simple.js --watch --no-daemon
  3. pm2 status
  4. pm2 delete PROCESS_ID

저장소를 만들고 기본 코드를 작성합니다.

  • git init

  • git add simple.js && git commit -m "simple.js"

원격저장소를 만들고 push 합니다.

  • git push -u origin master

ec2 인스턴스를 만듭니다.

  1. os : ubuntu 20
  2. instance type : micro
  3. security group : 22, 80, 3000 port 개방
  4. key-pair를 생성하고 다운로드 받습니다.

ec2 인스턴스에 접속합니다.

  1. ssh -i KEY_PAIR_NAME.pem ubuntu@PUBLIC_IP_ADDRESS

ssh-key를 만듭니다.

  1. ssh-keygen
  2. cat ~/.ssh/id.rsa.pub로 파일의 내용을 복사해서 github.com > settings > SSH 에 등록합니다.
  3. git clone RPOSITORY_URL . 저장소를 복제합니다.

nodejs 를 설치합니다.

  • sudo apt update && sudo apt install -y nodejs

실행합니다.

  1. sudo node simple.js

npm을 설치합니다.

  1. sudo apt update && sudo apt install npm -y

pm2를 사용합니다.

  1. npm install --global pm2
  2. pm2 start simple.js

RDS로 데이터 베이스를 생성합니다.

  1. 생성방식 : 손쉬운 생성
  2. 구성 : MySQL
  3. 인스턴스 크기 : 프리티어
  4. 마스터 사용자 이름 : admin
  5. 마스터 암호 : 111111

db가 접속 가능하게 설정을 바꿉니다.

  1. db 인스턴스의 VPC 보안그룹을 클릭합니다.
  2. 인바운드 편집을 선택합니다.
  3. 유형에 MySQL을 선택합니다. 소스에 EC2의 보안그룹의 이름을 입력합니다.

DB에 접속합니다.

  1. sudo apt update && sudo apt install mysql-client -y
  2. mysql -hHOST_NAME -uadmin -p
  3. create database myapp;
  4. use myapp;

데이터를 입력합니다.

--
-- Table structure for table `author`
--
  
  
CREATE TABLE `author` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `profile` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
);
  
--
-- Dumping data for table `author`
--
  
INSERT INTO `author` VALUES (1,'egoing','developer');
INSERT INTO `author` VALUES (2,'duru','database administrator');
INSERT INTO `author` VALUES (3,'taeho','data scientist, developer');
  
--
-- Table structure for table `topic`
--
  
CREATE TABLE `topic` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) NOT NULL,
  `description` text,
  `created` datetime NOT NULL,
  `author_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);
  
--
-- Dumping data for table `topic`
--
  
INSERT INTO `topic` VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);
INSERT INTO `topic` VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO `topic` VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO `topic` VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO `topic` VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);

mysql 8.0 이상 버전을 사용한다면 아래 명령으로 인증방식을 변경해줘야 합니다.

  1. ALTER USER 'USER_ID'@'%' IDENTIFIED WITH mysql_native_password BY 'PASSWORD'; 참고 : https://kogle.tistory.com/87

인스턴스를 복제합니다.

  1. git clone https://github.com/web-n/node.js-mysql.git
  2. cd node.js-mysql

DB 설정을 합니다.

  1. cd lib
  2. cp db.template.js db.js
  3. nano db.js
  4. host, user, password, database를 변경합니다.
  5. cd ..

main.js를 실행합니다.

  1. npm install 로 의존성을 설치합니다.

main.js를 실행합니다.

pm2 main.js

접속합니다.

http://PUBLIC_DNS_URL:3000

리부팅 시에 자동으로 실행되도록 합니다.

  1. sudo crontab -e에서 아래 내용을 추가
  2. @reboot sudo pm2 start /home/ubuntu/nodejs-aws/3.js

Clone this wiki locally