Bibi's DevLog ๐Ÿค“๐ŸŽ

[AWS] aws ec2์— mysql ์„ค์น˜ ๋ฐ ์Šคํ”„๋ง ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌํ•˜๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/AWS

[AWS] aws ec2์— mysql ์„ค์น˜ ๋ฐ ์Šคํ”„๋ง ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌํ•˜๊ธฐ

๋น„๋น„ bibi 2021. 6. 8. 14:12

AWS Ubuntu์— ์Šคํ”„๋ง ์•ฑ ๋ฐฐํฌ ๊ณผ์ • ๋‹ค์‹œ ์ •๋ฆฌ ..

AWS EC2 ์šฐ๋ถ„ํˆฌ์— mysql ์„ค์น˜

MySQL ์„ค์น˜

  • sudo apt update

  • sudo apt-get install mysql-server

MySQL ์ ‘์†

  • mysql -u root -p
    • (์ž˜ ์•ˆ ๋˜๋ฉด sudo mysql -u root -p)
    • (ํŒจ์Šค์›Œ๋“œ๋Š” ์„ค์ •ํ•œ ๊ฐ’ ์ž…๋ ฅ, ์—†์œผ๋ฉด ๊ทธ๋ƒฅ ์—”ํ„ฐ)
  • ์ƒ์„ฑํ•œ ์œ ์ €๋กœ ์ ‘์†ํ•˜๋ ค๋ฉด mysql -u ์œ ์ €์ด๋ฆ„ -p

MySQL ์‚ฌ์šฉ์ž ์ƒ์„ฑ

  • create user '์‚ฌ์šฉ์ž์ด๋ฆ„'@'ip์ฃผ์†Œ' identified by '๋น„๋ฐ€๋ฒˆํ˜ธ';
  • flush privileges;
  • ๋ชจ๋“  ip์—์„œ ์ ‘์†์„ ํ—ˆ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด create user '์‚ฌ์šฉ์ž์ด๋ฆ„'@'%' identified by '๋น„๋ฐ€๋ฒˆํ˜ธ'
  • ์ดํ›„ ์ ‘์†์‹œ mysql -u ์‚ฌ์šฉ์ž์ด๋ฆ„ -p ํ›„ ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์„ค์ •ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋ ฅ

MySQL ์‚ฌ์šฉ์ž ํ™•์ธ

  • use mysql; - select user, host, authentication_string from user;

MySQL ์‚ฌ์šฉ์ž์— ๊ถŒํ•œ ๋ถ€์—ฌ

  • grant all privileges on ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ช….ํ…Œ์ด๋ธ”๋ช… to '์‚ฌ์šฉ์ž๋ช…'@'ip์ฃผ์†Œ';

  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์— ์ ‘๊ทผ ํ—ˆ์šฉ ๋ฐ ๋ชจ๋“  ip ์ ‘์†๊ฐ€๋Šฅํ•œ ์‚ฌ์šฉ์ž์ผ ๋•Œ :

    • grant all privileges on *.* to '์‚ฌ์šฉ์ž๋ช…'@'%';

MySQL ์™ธ๋ถ€์ ‘์† ํ—ˆ์šฉ

  • MySQL์—์„œ ๋‚˜๊ฐ„ ๋’ค ์šฐ๋ถ„ํˆฌ์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด ์‹คํ–‰
  • cd /etc/mysql/mysql.conf.d - ls - vi mysqld.cnf
  • ์—ด๋ฆฐ ๋ฌธ์„œ์—์„œ bind-address ๋ถ€๋ถ„์„ 0.0.0.0์œผ๋กœ ์ˆ˜์ •ํ•œ ๋’ค :wq.
  • readonly๋ผ๊ณ  ์•ˆ ๋˜๋ฉด vi ๋‚˜๊ฐ„ ๋’ค, sudo vi mysqld.cnf๋กœ ์—ฐ ๋’ค ๋‹ค์‹œ ์‹œ๋„.

MySQL ์žฌ์‹œ์ž‘

sudo service mysql restart

AWS EC2์˜ MySQL๊ณผ Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—ฐ๊ฒฐํ•˜๊ธฐ

(AWS EC2์— MySQL์ด ์„ค์น˜๋˜์–ด ์žˆ๊ณ , ์ธ์Šคํ„ด์Šค๊ฐ€ ์‹คํ–‰ ์ค‘์ด์–ด์•ผ ํ•จ)

  • ์ธํ…”๋ฆฌ์ œ์ด ์ฐฝ ์˜ค๋ฅธ์ชฝ์˜ Gradle ํ•˜๋‹จ [Database] ํด๋ฆญ
  • [+] - [MySQL] ์„ ํƒ
  • ''ํ˜ธ์ŠคํŠธ''์— ํผ๋ธ”๋ฆญIPv4 ์ž…๋ ฅ
  • ''์‚ฌ์šฉ์ž''์— MySQL ์œ ์ € ์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ์— ํ•ด๋‹น ์œ ์ €์˜ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ
  • [์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ] ํด๋ฆญํ•ด ์—ฐ๊ฒฐ๋˜๋Š”์ง€ ํ™•์ธ(์ดˆ๋ก์ƒ‰)
  • ์—ฐ๊ฒฐ์ด ํ™•์ธ๋˜๋ฉด ์™„์„ฑ๋œ ์ฃผ์†Œ(URL)์„ ๋ณต์‚ฌํ•ด application.properties์˜ spring.datasource.url= ์˜†์— ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.

AWS EC2 ์„œ๋ฒ„์— SpringBoot jarํŒŒ์ผ ๋ฐฐํฌํ•˜๊ธฐ

์ฐธ๊ณ  : ํŒŒ์ผ ์ „์†ก, spring boot jar ๋ฐฐํฌํ•˜๋Š”๋ฒ•

  • ์ค€๋น„๋ฌผ : AWS EC2 ์ธ์Šคํ„ด์Šค(Ubuntu), spring boot ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜
  1. ํ”„๋กœ์ ํŠธ์—์„œ gradlew ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๋กœ ์ด๋™

    • ./gradlew build jar
    • ํ…Œ์ŠคํŠธ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋นŒ๋“œํ•˜๋ ค๋ฉด ./gradlew build jar -x test
  2. ์ƒ์„ฑ๋œ jarํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๋กœ ์ด๋™

    • TodoList-0.0.1-SNAPSHOT.jar ์™€ ๊ฐ™์€ ํŒŒ์ผ์ด ์žˆ๋Š” ๊ณณ์œผ๋กœ
    • ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ /build/libs์˜€๋‹ค
  3. ์ƒ์„ฑ๋œ jarํŒŒ์ผ์„ AWS EC2 ์„œ๋ฒ„์— ์ „์†ก

    • scp -i "pemํŒŒ์ผ๊ฒฝ๋กœ์™€ํŒŒ์ผ๋ช…" jarํŒŒ์ผ๋ช… EC2์‚ฌ์šฉ์ž์ด๋ฆ„@EC2ํผ๋ธ”๋ฆญIPv4DNS:~/

    • ์˜ˆ์‹œ : scp -i "C:/Users/rnala/key/TodoList.pem" TodoList-0.0.1-SNAPSHOT.jar ubuntu@ec2-13-125-136-48.ap-northeast-2.compute.amazonaws.com:~/

  4. AWS EC2์— ์ ‘์†ํ•ด ์ „์†ก๋œ jarํŒŒ์ผ ํ™•์ธ

    • ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ TodoList-0.0.1-SNAPSHOT.jar๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
  5. ์ „์†ก๋œ jarํŒŒ์ผ ์‹คํ–‰(= ๋ฐฐํฌ)

    • java -jar jarํŒŒ์ผ์ด๋ฆ„.jar

    • ์˜ˆ์‹œ : java -jar TodoList-0.0.1-SNAPSHOT.jar

    • ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋ฐฐํฌ๋ฅผ ์›ํ•  ๊ฒฝ์šฐ (์ข…๋ฃŒ, ๋กœ๊ทธ์•„์›ƒํ•ด๋„ ์‹คํ–‰)

      nohup java -jar jarํŒŒ์ผ์ด๋ฆ„.jar &

    • ์‹คํ–‰ ์ค‘์ธ ์ž๋ฐ” ํ”„๋กœ์„ธ์Šค ํ™•์ธ

      ps -ef | grep java

      (์กฐํšŒ๋œ ๋‚ด์—ญ์—์„œ ์ฒซ ๋ฒˆ์งธ์˜ 5์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ PID๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•œ๋‹ค)

    • ์‹คํ–‰ ์ค‘์ธ ์ž๋ฐ” ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ

      kill -9 PID๋ฒˆํ˜ธ

  6. AWS EC2 ์ธ์Šคํ„ด์Šค์˜ ๋ณด์•ˆ๊ทธ๋ฃน์—์„œ 8080 ํฌํŠธ ์˜คํ”ˆ

    • [๋ณด์•ˆ ๊ทธ๋ฃน] - ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ - ์‚ฌ์šฉ์ž ์ง€์ • TCP, ํฌํŠธ 8080, ์†Œ์Šค 0.0.0.0/0 ์œผ๋กœ ์ƒ์„ฑ
  7. ๋ธŒ๋ผ์šฐ์ €์—์„œ http://ํผ๋ธ”๋ฆญIPv4:8080์œผ๋กœ ์ ‘์†ํ•ด ์ž˜ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธ!