MySQL 설정
DB와 사용자 생성
( 한칸씩 ctrl + enter )
-- 유저이름@아이피주소
create user 'cos'@'%' identified by 'cos1234';
-- ON DB이름.테이블명
-- TO 유저이름@아이피주소
GRANT ALL PRIVILEGES ON *.* TO 'cos'@'%';
create database blog;
use blog;
GRANT ALL PRIVILEGES ON *.* TO 'cos'@'%'; : 해당 유저에게 모든 권한을 준다
MySQL 언어 한글설정 하는법
mysql이 저장된 폴더에 모든파일로 바꾸고 my.ini 파일을 열어서 아래 내용 추가
[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
위 방법이 안된다면 메모장을 우클릭하여 노트 관리자 권한으로 변경 후 my.ini 파일을 열고 내용추가하기
마지막으로 작업관리자에 mysql 다시 시작을 한다.
한글 설정 확인법
show variables like 'c%';
utf8 로 돼 있으면 끝!
이제 pom.xml 에 가서 아까 jpa, mysql 주석 처리한거 지우고 run as 다시 실행하니 DB에 연결안된다고 오류뜸!
이유는 프로젝트와 연결설정이 안돼있어서..
WARN 15680 --- [ restartedMain] ConfigServletWebServerApplicationContext
MySQL 과 프로젝트 연결
application.properties ▶ application.yml 로 변경한다. (shift + enter)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Seoul
username: cos
password: cos1234
yml 은 제이슨과 비슷한 데이터 타입으로 돼 있음. 요즘 스프링부트는 yml 에 설정한다. ( xml → yml )
properties 를 쓰지않는 이유는 중복되는 코드가 많아서?
- properties
spring.datasource.password=1234
spring.datasource.username=onetwo
- yml ( : 기호를쓰며 띄어쓰기로 같은 단어 쓸 필요가 없고 보기에도 훨씬 쉽다)
spring:
datasource:
password:1234
username:onetwo
(다음 줄은 띄어쓰기 두번)
이제 run as 다시 실행하니 DB와 연결이 되어
http://localhost:8080/test/hello 를 치면 hello spring 이 화면에 뜬다.
[BlogControllerTest ]
@RestController
public class BlogControllerTest(){
@GetMapping("/test/hello")
public String hello(){
return "<h1>hello spring</h1>";
}
}
'Spring boot | 블로그 만들기' 카테고리의 다른 글
회원삭제 테스트 | Exception 처리 (0) | 2022.11.05 |
---|---|
회원수정 테스트 | save()와 @Transactional 더티체킹 (1) | 2022.11.05 |
회원가입 테스트 | insert , Enum 사용법 (1) | 2022.11.04 |
테이블 만들기 User, Board, Reply (0) | 2022.11.03 |
패키지 생성법, 의존성 설정, 제어의 역전(Ioc)이란 (0) | 2022.10.31 |