从命令行将 mysql 数据库转储到纯文本 (CSV) 备份

2021-11-20 00:00:00 csv command-line backup mysql

我想避免使用 mysqldump,因为它以仅便于 mysql 读取的形式输出.CSV 似乎更通用(每表一个文件就可以了).但是,如果 mysqldump 有优势,我全神贯注.另外,我想要一些可以从命令行(linux)运行的东西.如果那是一个 mysql 脚本,那么有关如何制作这样东西的指针会很有帮助.

I'd like to avoid mysqldump since that outputs in a form that is only convenient for mysql to read. CSV seems more universal (one file per table is fine). But if there are advantages to mysqldump, I'm all ears. Also, I'd like something I can run from the command line (linux). If that's a mysql script, pointers to how to make such a thing would be helpful.

推荐答案

如果你可以处理 table-at-a-time,并且你的数据不是二进制的,使用 -B 选项来mysql 命令.使用此选项,它将生成可以很容易地导入 Excel 等的 TSV(制表符分隔)文件:

If you can cope with table-at-a-time, and your data is not binary, use the -B option to the mysql command. With this option it'll generate TSV (tab separated) files which can import into Excel, etc, quite easily:

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

或者,如果您可以直接访问服务器的文件系统,请使用 SELECT INTO OUTFILE 可以生成真正的CSV文件:

Alternatively, if you've got direct access to the server's file system, use SELECT INTO OUTFILE which can generate real CSV files:

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

相关文章