Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 -u root -p DBNAME SET FOREIGN_KEY_CHECKS = 0; | gzip > database.sql.gzip
like image 803
user1452145 Avatar asked Jun 12 '12 20:06

user1452145


People also ask

What is MySQL dump command?

Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. It produces the SQL Statements that can be used to recreate the database objects and data. The command can also be used to generate the output in the XML, delimited text, or CSV format.

Which command is used to dump database?

To dump entire databases, do not name any tables following db_name , or use the --databases or --all-databases option. To see a list of the options your version of mysqldump supports, issue the command mysqldump --help .


2 Answers

From this SO thread:

Can you automatically create a mysqldump file that doesn't enforce foreign key constraints?

The mysqldump command included with MySQL 5.0.51 (and according to the change log versions since 4.1.1) does switch off foreign key checks. By default, mysqldump includes the following line at the top of the dump file:

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

The /*!40014 ... */ syntax is a conditional comment that will be executed on MySQL 4.0.14 and later. The old foreign key checks setting is restored at the end of the dump file:

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

like image 187
philwinkle Avatar answered Oct 13 '22 22:10

philwinkle


you can use the --init-command flag on a per-session basis:

mysql --init-command="SET SESSION FOREIGN_KEY_CHECKS=0;" ... < dump.sql
like image 33
codewandler Avatar answered Oct 13 '22 23:10

codewandler