How do I dump MySQL file without Foreign Keys via command line?

后端 未结 3 884
予麋鹿
予麋鹿 2020-12-16 03:06

I need to export a large database without foreign keys. What is the command to do this?

This is what I tried but I know this is incorrect.

mysqldump         


        
3条回答
  •  情书的邮戳
    2020-12-16 03:48

    This is ugly, but I ended up just modifying the schema file to remove the foreign keys.


    Dump schema only, no foreign keys

    mysqldump -uuser -ppassword --no-data dbname | sed '$!N;s/^\(\s*[^C].*\),\n\s*CONSTRAINT.*FOREIGN KEY.*$/\1/;P;D' | grep -v 'FOREIGN KEY' > dbname_schema_no_fk.sql
    

    sed finds lines that precede foreign key constraint declarations and replaces those lines with themselves, minus the trailing comma. grep excludes the foreign key constraints.

    Dump data only, no table creation

    mysqldump -uuser -ppassword --no-create-info dbname > dbname_data.sql
    

    Load schema first, then data

    $ mysql -uuser -ppassword
    > create database foo
    > use foo
    > source dbname_schema_no_fk.sql
    > source dbname_data.sql
    

    Tested with mysqldump Ver 10.13 Distrib 5.6.32-78.1, for Linux (x86_64).

提交回复
热议问题