MySQL Export Data
In MySQL, you can use the SELECT...INTO OUTFILE statement to simply export data to a text file.
Exporting Data with the SELECT ... INTO OUTFILE Statement
SELECT...INTO OUTFILE is the syntax in MySQL used to export query results to a file.
SELECT...INTO OUTFILE allows you to write the results of a query to a text file. The basic usage is:
SELECT column1, column2, ...
INTO OUTFILE 'file_path'
FROM your_table
WHERE your_conditions;
Parameter Description:
column1, column2, ...: the columns to select.'file_path': specifies the path and name of the output file.your_table: The table to query.your_conditions: the query condition.
The following is a simple example:
Example
SELECT
id
,
name
,
INTO
OUTFILE
'/tmp/user_data.csv'
FIELDS
TERMINATED
BY
','
LINES
TERMINATED
BY
'\n'
FROM
users;
In the above SQL statement, we selected the id, name, and email columns from the users table and wrote the results to the /tmp/user_data.csv file. FIELDS TERMINATED BY ',' specifies the separator between columns (comma), and LINES TERMINATED BY '\n' specifies the separator between rows (newline).
It should be noted that executing SELECT...INTO OUTFILE requires appropriate privileges, and the directory of the output file must be a place where the MySQL server can write.
In the following example, we export the data from the table runoob_tbl to the /tmp/runoob.txt file:
mysql> SELECT * FROM runoob_tbl
-> INTO OUTFILE '/tmp/runoob.txt';
You can set a specific format for the data output through command options. The following example exports to CSV format:
mysql> SELECT * FROM passwd INTO OUTFILE '/tmp/runoob.txt'
-> FIELDS TERMINATED BY ',' ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n';
In the following example, a file is generated in which values are separated by commas. This format can be used by many programs.
SELECT a,b,a+b INTO OUTFILE '/tmp/result.text'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
The SELECT ... INTO OUTFILE statement has the following properties:
- LOAD DATA INFILE is the inverse operation of SELECT ... INTO OUTFILE, with SELECT syntax. To write data from a database to a file, use SELECT ... INTO OUTFILE; to read a file back into the database, use LOAD DATA INFILE.
- A SELECT of the form SELECT...INTO OUTFILE 'file_name' can write the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax.
- The output cannot be an existing file. This prevents the file's data from being tampered with.
- You need an account to log in to the server to retrieve the file. Otherwise, SELECT ... INTO OUTFILE will have no effect.
- In UNIX, the file is readable after it is created, with permissions owned by the MySQL server. This means that although you can read the file, you may not be able to delete it.
mysqldump: Exporting Tables as Raw Data
mysqldump is a command-line tool provided by MySQL for backing up and exporting databases.
mysqldump is a utility used by mysql to dump and store databases. It mainly produces an SQL script containing the commands necessary to recreate the database from scratch, such as CREATE TABLE and INSERT.
Using mysqldump to export data requires the --tab option to specify the directory for the exported files, and this destination must be writable.
Basic usage of mysqldump:
mysqldump -u username -p password -h hostname database_name > output_file.sql
Parameter Description:
-u: specifies the MySQL username.-p: prompts for a password.-h: specifies the MySQL host name.database_name: the name of the database to export.output_file.sql: the file to which the exported data is saved.
The following example exports the table runoob_tbl to the /tmp directory:
$ mysqldump -u root -p --no-create-info \
--tab=/tmp RUNOOB runoob_tbl
password ******
mysqldump Examples
1. Export the entire database
Export the mydatabase database to the mydatabase_backup.sql file:
mysqldump -u root -p mydatabase > mydatabase_backup.sql
2. Export specific tables
If you only want to export a certain table in the database, you can use the following command:
mysqldump -u username -p password -h hostname database_name table_name > output_file.sql
Or:
mysqldump -u root -p mydatabase mytable > mytable_backup.sql
3. Export the database structure
If you only want to export the database structure without the data, you can use the --no-data option:
mysqldump -u username -p password -h hostname --no-data database_name > output_file.sql
4. Export a compressed file
You can compress the exported data to reduce the file size. For example, using gzip:
mysqldump -u username -p password -h hostname database_name | gzip > output_file.sql.gz
Exporting Data in SQL Format
To export data in SQL format to a specified file, do as follows:
$ mysqldump -u root -p RUNOOB runoob_tbl > dump.txt
password ******
The content of the file created by the above command is as follows:
-- MySQL dump 8.23
--
-- Host: localhost Database: RUNOOB
---------------------------------------------------------
-- Server version 3.23.58
--
-- Table structure for table `runoob_tbl`
--
CREATE TABLE runoob_tbl (
runoob_id int(11) NOT NULL auto_increment,
runoob_title varchar(100) NOT NULL default '',
runoob_author varchar(40) NOT NULL default '',
submission_date date default NULL,
PRIMARY KEY (runoob_id),
UNIQUE KEY AUTHOR_INDEX (runoob_author)
) TYPE=MyISAM;
--
-- Dumping data for table `runoob_tbl`
--
INSERT INTO runoob_tbl
VALUES (1,'Learn PHP','John Poul','2007-05-24');
INSERT INTO runoob_tbl
VALUES (2,'Learn MySQL','Abdul S','2007-05-24');
INSERT INTO runoob_tbl
VALUES (3,'JAVA Tutorial','Sanjay','2007-05-06');
If you need to export the data of an entire database, you can use the following command:
$ mysqldump -u root -p RUNOOB > database_dump.txt
password ******
If you need to back up all databases, you can use the following command:
$ mysqldump -u root -p --all-databases > database_dump.txt
password ******
The --all-databases option was added in MySQL 3.23.12 and later versions.
This method can be used to implement a database backup strategy.
Copying Tables and Databases to Another Host
If you need to copy data to another MySQL server, you can specify the database name and tables in the mysqldump command.
Execute the following command on the source host to back up the data to the dump.txt file:
$ mysqldump -u root -p database_name table_name > dump.txt
password *****
If you are backing up the entire database, there is no need to use a specific table name.
If you need to import the backed-up database into a MySQL server, you can use the following command. When using it, you need to confirm that the database has already been created:
$ mysql -u root -p database_name < dump.txt
password *****
You can also use the following command to import the exported data directly into a remote server, but make sure the two servers are connected and can access each other:
$ mysqldump -u root -p database_name \
| mysql -h other-host.com database_name
The above command uses a pipe to import the exported data into the specified remote host.