[AWS] EC2(Ubuntu)에 MySQL 설치하고 스프링부트와 연결하기

2025. 4. 25. 14:21·AWS

EC2에 MySQL을 설치하고 스프링부트와 연결하는 작업을 진행했다. 원래 RDS를 사용하면 좀 더 편하게 연결할 수 있지만, 프로젝트 규모가 크지도 않고 RDS를 사용하면 비용이 발생하기 때문에 그냥 EC2에 직접 설치하기로 결정했다.

 

MySQL 설치 과정

1. 시스템 업데이트

sudo apt update
sudo apt upgrade -y

 

2. MySQL 서버 설치

sudo apt install mysql-server -y

 

3. MySQL 서비스 상태 확인

sudo systemctl status mysql

 

4. MySQL 보안 설정

보안 설정 단계에서는 여러 질문에 답해야 했다.

sudo mysql_secure_installation

 

  • VALIDATE PASSWORD COMPONENT 설정 여부를 묻는 질문에 Y를 입력했다. 이는 비밀번호 강도를 검사하여 보안을 강화하는 기능이다.
ubuntu@ip-172-31-41-26:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

 

  • 비밀번호 정책 레벨을 선택하는 단계에서는 MEDIUM(1)과 을 STRONG(2) 중에서 1을 선택했다. 이는 최소 8자 이상, 숫자, 대소문자, 특수문자가 포함된 비밀번호를 요구한다. 개발 환경이라면 1을, 프로덕션 환경이라면 2를 선택하면 된다.
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:

 

  • 익명 사용자 제거 여부를 묻는 질문에도 Y를 입력했다. 보안상 취약점이 될 수 있기 때문이다. 익명 사용자를 제거하면 인증되지 않은 사용자가 MySQL에 접근하는 것을 방지할 수 있다.
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

 

  • root 계정의 원격 접속 차단 여부를 묻는 질문에도 Y를 입력했다. root는 로컬에서만 접속하고, 원격 접속은 별도의 사용자 계정으로 하는 것이 안전하다.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

 

  • test 데이터베이스 제거 여부를 묻는 질문에도 Y를 입력했다. test 데이터베이스는 누구나 접근할 수 있는 기본 데이터베이스로, 보안상 취약점이 될 수 있습니다. 테스트 용도로만 사용되므로 제거하는 것이 안전하다.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

 

  • 마지막으로 권한 테이블 재로드 여부를 묻는 질문에 Y를 입력하여 모든 변경사항을 즉시 적용했다.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

 

5. MySQL 접속 및 데이터베이스 설정

sudo mysql

CREATE DATABASE hello_db;
CREATE USER 'hello'@'%' IDENTIFIED BY 'hello_password';
GRANT ALL PRIVILEGES ON hello_db.* TO 'hello'@'%';
FLUSH PRIVILEGES;
EXIT;

 

6. 원격 접속 허용을 위한 설정

파일을 저장할 때는 Ctrl+O를 누르고 엔터(파일명 확인), 그리고 Ctrl+X로 나오면 된다.

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

# bind-address = 127.0.0.1 라인을 찾아서 다음과 같이 수정
bind-address = 0.0.0.0

 

7. MySQL 재시작

sudo systemctl restart mysql

 

8. 방화벽 설정 (필요한 경우)

sudo ufw allow mysql

 

9. AWS 보안 그룹 설정

EC2 인스턴스의 보안 그룹에 MySQL 포트(3306)를 인바운드 규칙으로 추가한다.

 

10. 스프링부트와 mysql 연결

spring.datasource.url=jdbc:mysql://[EC2_PUBLIC_IP]:3306/hello_db?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8
spring.datasource.username=hello
spring.datasource.password=hello_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

 

11. MySQL Workbendch로 연결 테스트

Hostname에 퍼블릭 IPv4 주소를 입력하고 데이터베이스 계정을 입력하면 정상적으로 연결된다.

 

이렇게 EC2에 MySQL을 설치하고 스프링부트와 연결하는 작업을 완료했다.

'AWS' 카테고리의 다른 글

[AWS] EC2에서 Nginx로 포트 번호 없이 웹 서비스 배포하기  (0) 2025.05.16
[AWS] Docker와 GitHub Actions를 활용한 CI/CD 파이프라인 구축과 환경 변수 문제 해결  (0) 2025.05.15
[AWS] Windows에서 AWS EC2까지: Docker로 Python 서비스 배포하기  (0) 2025.05.14
[AWS] EC2 서버 용량 문제 해결하기  (0) 2025.05.13
[AWS] React + Spring Boot(Maven) 프로젝트 AWS EC2(Ubuntu)에 빌드 및 배포하기  (0) 2025.05.12
'AWS' 카테고리의 다른 글
  • [AWS] Docker와 GitHub Actions를 활용한 CI/CD 파이프라인 구축과 환경 변수 문제 해결
  • [AWS] Windows에서 AWS EC2까지: Docker로 Python 서비스 배포하기
  • [AWS] EC2 서버 용량 문제 해결하기
  • [AWS] React + Spring Boot(Maven) 프로젝트 AWS EC2(Ubuntu)에 빌드 및 배포하기
dud9902
dud9902
개발자 취준생 기록일지
  • dud9902
    dud's DevStory
    dud9902
  • 전체
    오늘
    어제
    • 분류 전체보기 (79)
      • SpringBoot (14)
      • React (12)
      • Python (14)
      • AI (21)
      • DB (5)
      • Figma (1)
      • Markdown (1)
      • AWS (6)
      • 기타 (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    db
    springboot
    pydantic
    CrewAI
    의존성 주입
    pytorch
    Agent
    recognize anything
    twilio
    SQLAlchemy
    AI
    langchain
    FastAPI
    redis
    docker
    AWS
    miniforge
    스프링부트
    react
    Python
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dud9902
[AWS] EC2(Ubuntu)에 MySQL 설치하고 스프링부트와 연결하기
상단으로

티스토리툴바