skip to content
Slashism

Import and Export SQL files in MySQL using terminal

/ 1 min read

Quick reference for importing and exporting a MySQL database from the terminal.

Import a database

Use the mysql client to load a dump file into an existing database:

Terminal window
mysql -u username -p database_name < /path/to/file.sql
  • -u specifies the user; -p prompts for the password.
  • Include -h <host> or -P <port> if needed.

Export a database

Create a dump with mysqldump:

Terminal window
mysqldump -u username -p database_name > /path/to/file.sql
  • Add -h <host> or -P <port> as required.
  • To include routines and triggers, use: mysqldump -u username -p -R --triggers database_name > /path/to/file.sql

That’s it—two commands to move data in and out quickly.