MySQL EXPORT / IMPORT

·2021년 4월 26일

MySQL

목록 보기
1/1

Prerequisites

mysqldump 사용 시 <, > 꺽쇠 방향에 주의한다.

  • Export
    OS : CentOS(4.8)
    DB : MySQL(4.1.22)

  • Import
    OS : CentOS(8.3)
    DB : MariaDB(10.5.8)

MySQL Export

1. DB가 저장되는 디렉토리에서 mysqldump 명령어를 사용하여 DB Export. (mysqldump는 MySQL에 내장 된 명령어이다.)

# [mysql path] mysqldump -u [사용자 아이디] -p [백업할 DB명] > [생성할 파일명].sql
        ex) # mysqldump -u root -p ExportDB > export_backup_db.sql
# /usr/local/mysql/bin/mysqldump -u root -p ExportDB > export_backup_db.sql

2. 생성된 export_backup_db.sql 파일을 확인한다.

# ls -la | grep export_backup_db.sql
-rw-r--r--   1 root  root          0 Jan 20 08:09 export_backup_db.sql

3. 생성된 export_backup_db.sql 파일을 vi 명령어로 보면 기존 DB 데이터 Query가 작성되어있다. (아무 내용이 없다면 잘 못 생성된 것이다.)

# vi export_backup_db.sql
-- MySQL dump 10.9
--
-- Host: localhost    Database: ExportDB
-- ------------------------------------------------------
-- Server version       4.1.22

...

DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
  `uid` int(11) unsigned NOT NULL auto_increment,
  `test_code` varchar(30) default NULL,
  `test_access` int(11) NOT NULL default '0',
...

4. scp를 사용하여 DB를 Import할 DBMS의 Data 디렉토리로 복사해준다.


MySQL Import

1. 복사된 export_backup_db.sql을 확인한다.

# ls -la | grep export_backup_db.sql
-rw-r--r--.  1 root  root  209423808 Jan 20 08:21 export_backup_db.sql

2. DBMS 계정 로그인 후 Import할 DB를 생성한다.

# mysql -u root -p
# CREATE DATABASE [DB명];
# exit
# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.5.8-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE IMPORT_DB;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> exit
Bye

3. 생성 한 IMPORT_DB를 확인한다.

# ls -la | grep IMPORT_DB
drwx------.  2 mysql mysql        20 Jan 20 08:25 IMPORT_DB

4. MySQL Data 저장 디렉토리에서 백업했던 파일 Import.

# mysql -u root -p [import할 DB명] < [백업했던 파일명].sql
# mysql -u root -p IMPORT_DB < export_backup_db.sql

Result

  • 원본 DB Table 조회
mysql> select uid, test_left, test_top, test_height, test_width from test_table;
+-----+------------+-----------+--------------+-------------+
| uid | test_left | test_top | test_height | test_width |
+-----+------------+-----------+--------------+-------------+
|   2 |        350 |       110 |          240 |         400 |
|   3 |        360 |       100 |          306 |         420 |
|   4 |        360 |       100 |          357 |         424 
....
+-----+------------+-----------+--------------+-------------+
9 rows in set (0.00 sec)

  • 복사한 DB Table 조회
MariaDB [IMPORT_DB]> select uid, test_left, test_top, test_height, test_width from test_table;
+-----+------------+-----------+--------------+-------------+
| uid | test_left | test_top | test_height | test_width |
+-----+------------+-----------+--------------+-------------+
|   2 |        350 |       110 |          240 |         400 |
|   3 |        360 |       100 |          306 |         420 |
|   4 |        360 |       100 |          357 |         424 ....
+-----+------------+-----------+--------------+-------------+
9 rows in set (0.000 sec)

0개의 댓글