MySQL->導出/導入資料[20180521]

MySQL 導出
    INTO OUTFILE將資料導出至文件中
    mysqldump工具導出資料和數據結構,而且能夠針對數據庫、數據表、索引的結構。
 
INTO OUTFILE測試
 
  select * from seq_test into outfile '/tmp/backup_v0.txt';
    
 
生成一個文件,各值用逗號隔開
 
  select *  into outfile '/tmp/backup_v1.txt' fields terminated by ',' enclosed by '"' lines terminated by '\n' from seq_test;
    

 

  select * from seq_test into outfile '/tmp/backup_v2.txt' fields terminated by ',' enclosed by '"' lines terminated by '\r\n' ;
        

 

 
mysqldump工具導出原始資料
        mysqldump -u root -p runoob seq_test|gzip > backup_data.zip
           

 

 
   也可使用如下命令將導出的數據直接導入到遠程的服務器上,但請確保兩臺服務器是相通的,是能夠相互訪問的:</p>
    $ mysqldump -u root -p database_name \
           | mysql -h other-host.com database_name
 
 
資料導入
    針對使用INTO OUTFILE方式導出的資料,可使用LOAD DATA LOCAL INFILE方式導入資料。
         LOAD DATA LOCAL INFILE '/tmp/backup_v0.txt' INTO TABLE seq_test;
 
 
 mysql> select count(*) from seq_test;
+----------+
| count(*) |
+----------+
|      111 |
+----------+
1 row in set (0.00 sec)
 
mysql> truncate table seq_test;
Query OK, 0 rows affected (0.00 sec)
 
mysql>  LOAD DATA LOCAL INFILE '/tmp/backup_v0.txt' INTO TABLE seq_test;
Query OK, 111 rows affected (0.00 sec)
Records: 111  Deleted: 0  Skipped: 0  Warnings: 0
 
mysql> select count(*) from seq_test;
+----------+
| count(*) |
+----------+
|      111 |
+----------+
1 row in set (0.00 sec)
 

 

 
   

 

       
    還可使用mysqlimport工具導入
[root@t-xi-mysql01 tmp]# cp backup_v0.txt seq_test.txt
[root@t-xi-mysql01 tmp]#  mysqlimport -u root -p --local runoob seq_test.txt
Enter password:
runoob.seq_test: Records: 111  Deleted: 0  Skipped: 111  Warnings: 0
 
 
 
將mysqldump導出的資料進行導入
    
  gunzip  -c backup_data.zip>backup_data.sql
  mysql -u root -p
  mysql> source backup_data.sql
 
  
  
[root@t-xi-mysql01 tmp]# mysqldump -u root -p runoob seq_test|gzip > backup_data.zip
Enter password:
[root@t-xi-mysql01 tmp]#   gunzip  -c backup_data.zip>backup_data.sql
[root@t-xi-mysql01 tmp]#
[root@t-xi-mysql01 tmp]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.1.71 Source distribution
 
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> use runoob;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> source backup_data.sql
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.01 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 111 rows affected (0.00 sec)
Records: 111  Duplicates: 0  Warnings: 0
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
 
Query OK, 0 rows affected (0.00 sec)
mysql> select count(*) from seq_test;
+----------+
| count(*) |
+----------+
|      111 |
+----------+
1 row in set (0.00 sec)
 
相關文章
相關標籤/搜索