MySQL Dump

A Simple Database Backup

mysqldump -h [hostorIP] -u [username] -p [password] [databasename] > [backupfile.sql]

{easycodeinc /article_code_snippets/MySQL_Dump_Restore.php, 1, php, linenums:1}

  • [host or IP] - is your connection to the sql server
  • [username] - this is your database username
  • [password] - this is the password for your database
  • [databasename] - the name of your database
  • [backupfile.sql] - the file to which the backup should be written.

The resultant dump file will contain all the SQL statements needed to create the table and populate the table in a new database server. To backup your database 'Customers' with the username 'sadmin' and password 'pass21' to a file custback.sql, you would issue the command:

You can also ask mysqldump to add a drop table command before every create command by using the option --add-drop-table. This option is useful if you would like to create a backup file which can rewrite an existing database without having to delete the older database manually first.

To restore the dump...  The "<" before the file name will restore all the table to the Customers database