코딩마을방범대

[MySQL] Mysql Dump를 이용한 백업, 복원 방법 본문

💡 백엔드/MySQL

[MySQL] Mysql Dump를 이용한 백업, 복원 방법

신짱구 5세 2023. 7. 11. 14:14

 

 

 

백업을 하는 방법으로는 두가지가 있다!

터미널을 이용하는 방법과 워크벤치를 이용하는 방법이다.

 

우선 워크벤치를 이용하는 방법을 먼저 알아본 후 터미널로 이용하는 방법을 알아볼 예정이다.

 

 


 

 

 

 

 

dump 파일로 백업하기

 

 

1. Workbench 이용

 

1. 복사할 데이터베이스 선택 후 export

 

2. 생성될 sql을 이용해 붙여넣기 할 DB에 이용

 

Export Option에서 Export to Self-Contained File 로 선택해줘야 하나의 SQL로 생성됨

 

 

 


 

 

2. 터미널 이용

※ cd 를 통해 sql 파일을 만들 폴더로 미리 이동하기!

# mysqldump -u [계정명] -p [스키마명] -h [ip주소] -P [포트번호] > [생성할 dump파일명].sql

 

백업 실행 시 아래와 같은 오류가 발생할 수 있다.

Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.

Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump. In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --master-data.

 

 

이럴 경우 아래 명령어로 실행해주자!

# mysqldump --set-gtid-purged=OFF --column-statistics=0 -h [ip주소] -u [계정명] -p [스키마명]  > [dump 파일명].sql

 

 

 

 

 

 


 

 

 

 

 

dump 파일 이용해 복원하기

 

 

1. Workbench 이용

 

1. Data import 클릭

 

2. Import from Self-Contained File 클릭 후 SQL 파일 선택

Default Target Schema에서 기존 스키마를 선택하거나 New 눌러서 스키마 생성

Start Import!

 

 

 

 


 

 

2. 터미널 이용

 

// 외부접속을 이용한 복원일 경우
# mysql -h [복원시킬 ip주소] -P [포트번호] -u [계정명] -p [복원 스키마명] < [dump파일명].sql
// 내부접속을 이용한 복원일 경우
# mysql -u [계정명] -p [복원 스키마명] < [dump파일명].sql

 

 

 

 


참고사이트

mysqldump 사용법(db backup 및 load 하기)

 

 

SMALL

'💡 백엔드 > MySQL' 카테고리의 다른 글

[MySQL] Read Only로 설정되어 CRUD가 불가능할 때  (0) 2023.07.11