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