Home  >  Article  >  Database  >  How to Back Up a MySQL Database as a CSV File Using the Command Line?

How to Back Up a MySQL Database as a CSV File Using the Command Line?

DDD
DDDOriginal
2024-10-30 20:12:02367browse

How to Back Up a MySQL Database as a CSV File Using the Command Line?

CSV Database Backup Using Command Line

To generate a plaintext backup of a MySQL database as a CSV file, consider the following command line methods:

Method 1: Using the -B Option

This option outputs TSV (tab-separated) files that can be imported into various applications, such as Excel.

% echo 'SELECT * FROM table' | mysql -B -uxxx -pyyy database

Method 2: Using SELECT INTO OUTFILE

If you have direct server file system access, SELECT INTO OUTFILE allows you to create CSV files with specific formatting options:

SELECT * INTO OUTFILE 'table.csv'
    FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
FROM table

In this approach, you can customize field terminators, enclosure, and line terminators as needed.

The above is the detailed content of How to Back Up a MySQL Database as a CSV File Using the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn