- Linux:Powerful Server Administration
- Uday R. Sawant Oliver Pelz Jonathan Hobson William Leemans
- 392字
- 2021-07-09 18:16:54
Importing and exporting bulk data
In this recipe, we will learn how to import and export bulk data with MySQL. Many times it happens that we receive data in CSV or XML format and we need to add this data to the database server for further processing. You can always use tools such as MySQL workbench and phpMyAdmin, but MySQL provides command-line tools for the bulk processing of data that are more efficient and flexible.
How to do it…
Follow these steps to import and export bulk data:
- To export a database from the MySQL server, use the following command:
$ mysqldump -u admin -p mytestdb > db_backup.sql
- To export specific tables from a database, use the following command:
$ mysqldump -u admin -p mytestdb table1 table2 > table_backup.sql
- To compress exported data, use
gzip
:$ mysqldump -u admin -p mytestdb | gzip > db_backup.sql.gz
- To export selective data to the CSV format, use the following query. Note that this will create
articles.csv
on the same server as MySQL and not your local server:SELECT id, title, contents FROM articles INTO OUTFILE ‘/tmp/articles.csv’ FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”’ LINES TERMINATED BY ‘\n’;
- To fetch data on your local system, you can use the MySQL client as follows:
- Write your query in a file:
$ nano query.sql select * from articles;
- Now pass this query to the
mysql
client and collect the output in CSV:$ mysql -h 192.168.2.100 -u admin -p myblog < query.sql > output.csv
The resulting file will contain tab separated values.
- Write your query in a file:
- To import an SQL file to a MySQL database, we need to first create a database:
$ mysqladmin -u admin -p create mytestdb2
- Once the database is created, import data with the following command:
$ mysql -u admin -p mytestdb2 < db_backup.sql
- To import a CSV file in a MySQL table, you can use the
Load Data
query. The following is the sample CSV file:Now use the following query from the MySQL console to import data from CSV:
LOAD DATA INFILE ‘c:/tmp/articles.csv’ INTO TABLE articles FIELDS TERMINATED BY ‘,’ ENCLOSED BY ‘”’ LINES TERMINATED BY \n IGNORE 1 ROWS;
See also
- MySQL select-into syntax at https://dev.mysql.com/doc/refman/5.6/en/select-into.html
- MySQL load data infile syntax at https://dev.mysql.com/doc/refman/5.6/en/load-data.html
- Importing from and exporting to XML files at https://dev.mysql.com/doc/refman/5.6/en/load-xml.html
推薦閱讀
- Word 2003、Excel 2003、PowerPoint 2003上機指導與練習
- 火格局的時空變異及其在電網防火中的應用
- 過程控制工程及仿真
- Visualforce Development Cookbook(Second Edition)
- 控制與決策系統仿真
- Expert AWS Development
- 數據挖掘方法及天體光譜挖掘技術
- 現代傳感技術
- Excel 2007技巧大全
- Godot Engine Game Development Projects
- Natural Language Processing and Computational Linguistics
- Hands-On DevOps
- 從零開始學ASP.NET
- 機器人剛柔耦合動力學
- Eclipse RCP應用系統開發方法與實戰