Mini Guide: How to Import a SQL File in MySQL

Published on November 21st, 2020

Islam Essam

Co-founder, SimpleBackups

Follow on Twitter

Have you just begun to learn how to work with SQL files using MySQL? Maybe you feel a bit lost on how to import files with this tool. Luckily, importing and exporting files via MySQL is actually quite simple. Learn how to use MySQL to import SQL files by following the step-by-step guide below.

Table of Contents

  1. Import a SQL file using Command Line
  2. Import a SQL file using mysqldump
  3. Export a SQL file using mysqldump
  4. Automate Your MySQL Backups with SimpleBackups

Import a SQL file using Command Line

  1. Open XAMPP.
  2. Launch Apache Server and MySQL Database.
  3. Create a database via phpMyAdmin.
  4. Copy the SQL file of your choice to the xampp/mysql/bin/ directory.
  5. Open Command Prompt.
  6. Go to xampp/mysql/bin/.
  7. Type: mysql -u username -p database_name < file.sql
  8. The username refers to your MySQL username.
  9. database_name refers to the database you want to import.
  10. file.sql is your file name.
  11. If you've assigned a password, type it now and press Enter.
  12. Open phpMyAdmin and select your database to ensure that the tables have imported properly.

Import a SQL file using mysqldump

  1. To import a .sql file with mysqldump, use the mysqlimport command and use the following flags and syntax $ mysqlimport -u magazine_admin -p magazines_production ~/backup/database/magazines.sql
  2. -u and -p are needed for authentication, and is then followed by the name of the database you want to import into.
  3. You'll need to specify the path to your SQL dump file that will contain your import data: ~/backup/database/magazines.sql
  4. You won't need to use > or < for importing, but you will need them for exporting in the next guide.
  5. This will prompt a password request.
  6. Your file will be automatically imported.

Export a SQL file using mysqldump

  1. To export a MySQL database to a test file, start by using the mysqldump command.
  2. Log in to MySQL.
  3. Enter the mysqldump command using the following flags and options: $ mysqldump -u my_username -p database_name > output_file_path
  4. The -u flag specifies the MySQL username.
  5. The -p flag specifies a password prompt associated with the above username.
  6. database_name is the name of the database you want to export.
  7. The > symbol is a Unix directive for STDOUT, which will make it possible for Unix commands to output the subsequent results of the output command to another location. These locations are usually file paths.
  8. Be sure to input the completely qualified path and its filename to your output file path, so that your file will be placed exactly where you want it to be.
  9. Once the command is executed, you'll be prompted to enter your password. This will then create your exported backup file with a .sql extension.

Automate Your MySQL Backups with SimpleBackups

SimpleBackups is a database and website backup automation tool. SimpleBackups automates MySQL backups by using MySQLDump to securely send backup files offsite to the cloud for storage. When you need to ensure your MySQL backups are secure, you can trust SimpleBackups to take care of it for you.

How was our guide to using MySQL to import SQL files using a variety of processes? Tell us what you thought of our guide by chat or email.

Notes:

  • After entering this command, you may be asked to enter the password for the MySQL user that you used.
  • Please be careful when using an existing database that has records as this command will overwrite your existing database and end up losing your records.


Back to blog

Ready to automate your backups?

Sign up for FREE. Get started in less than one minute.

Secure your backups

No credit card required. Free 7-day trial.