Springboot/블로그만들기

[Springboot] 블로그 만들기 (3)_MySQL 연동하기

21종 2023. 11. 21. 20:01

MySQL WorkBranch 실행

관리자 계정으로 접속한다. MySQL 설치할때 사용한 아이디 비밀번호 입력

 

계정생성 및 권한부여

-- 유저이름@아이피주소
create user 'lwj'@'%' identified by '1213';
-- ON DB이름.테이블명
-- TO 유저이름@아이피주소
GRANT ALL PRIVILEGES ON *.* TO 'lwj'@'%';
CREATE DATABASE blog CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

 

MySQL 한글 설정

관리자 권한으로 메모장을 실행한후 C:\ProgramData\MySQL\MySQL Server 5.7 폴더의 my.ini 열기 / [client], [mysql], [mysqld] 글자를 찾아서 아래 텍스트 추가 후 저장

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_general_ci'
character-set-server=utf8

 

MySQL 다시시작

장치관리자 -> 서비스 -> MySQL57 우클릭 -> 다시 시작

위와 같이 utf-8 로 변경됐으면 완료


연동확인

프로젝트의 pom.xml에 가서 mysql과 JPA 의 주석을 풀어준다.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

 

프로젝트의 src/main/resources의 경로에 있는 application.properties 의 확장자를 바꿔준다 

properties -> yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Seoul
    username: lwj
    password: 1213
server:
  port: 8001

 

다시 springboot app 으로 실행

성공~~!!

 

크롬에서도 확인해보자

http://localhost:8001/http/lombok


참고 유튜브 (메타코딩님 강의)

https://youtu.be/G6fgEiI_pEA?si=7KhNRu8Us4BuvT5L