Dump a mysql database to a plaintext (CSV) backup from the command line

前端 未结 10 1035
广开言路
广开言路 2020-11-28 19:26

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 ar

相关标签:
10条回答
  • 2020-11-28 19:38

    In MySQL itself, you can specify CSV output like:

    SELECT order_id,product_name,qty
    FROM orders
    INTO OUTFILE '/tmp/orders.csv'
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    

    From http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/

    0 讨论(0)
  • 2020-11-28 19:41

    The select into outfile option wouldn't work for me but the below roundabout way of piping tab-delimited file through SED did:

    mysql -uusername -ppassword -e "SELECT * from tablename" dbname | sed 's/\t/","/g;s/^/"/;s/$/"/' > /path/to/file/filename.csv
    
    0 讨论(0)
  • 2020-11-28 19:48

    Check out mk-parallel-dump which is part of the ever-useful maatkit suite of tools. This can dump comma-separated files with the --csv option.

    This can do your whole db without specifying individual tables, and you can specify groups of tables in a backupset table.

    Note that it also dumps table definitions, views and triggers into separate files. In addition providing a complete backup in a more universally accessible form, it also immediately restorable with mk-parallel-restore

    0 讨论(0)
  • 2020-11-28 19:53

    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
    

    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
    
    0 讨论(0)
提交回复
热议问题