Home >Database >Mysql Tutorial >How to Export and Import MySQL Databases via the Command Line with Options?

How to Export and Import MySQL Databases via the Command Line with Options?

Susan Sarandon
Susan SarandonOriginal
2024-12-02 12:40:131049browse

How to Export and Import MySQL Databases via the Command Line with Options?

How to Export and Import a .SQL File from the Command Line with Options

Not a duplicate! This question seeks features similar to phpMyAdmin's export functionality for the command line.

Exporting a Database

To export a MySQL database to a .sql file, use the mysqldump command along with the following options:

  • -u username: Specify the database username.
  • -p: Prompt for the database password.
  • databasename: Replace with the name of the database to export.
  • > filename.sql: Specify the output filename and location.

Example:

mysqldump -u username -p databasename > database-backup.sql

Importing a Database

To import a .sql file into a MySQL database, use the mysql command:

  • -u username: Specify the database username.
  • -p: Prompt for the database password.
  • DATA-BASE-NAME: Replace with the name of the database to import into.
  • < data.sql: Specify the input file to import from.

Example:

mysql -u username -p DATA-BASE-NAME < data.sql

Options for Exporting and Importing

The following options can be used to customize the export/import process:

  • --disable-foreign-key-checks: Disable foreign key checks during the operation.
  • --skip-add-drop-table: Omit table creation/drop statements from the exported file.
  • --create-options: Include additional options when creating tables in the exported file.

Example (disable foreign key checks during export):

mysqldump -u username -p --disable-foreign-key-checks databasename > database-backup.sql

The above is the detailed content of How to Export and Import MySQL Databases via the Command Line with Options?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn