How to import an SQL file in MySQL

While working on web application, often you need to insert data from sql file. Also when changing your server, you need to export database from old server and import to new one.

There are so many ways you can import database from sql file. There are so many GUI applications like MySQL Workbench as well as web application like phpmyadmin available to work with Mysql server. We can also use command line to import file from remote desktop.

We will go through few ways to import database from SQL file in MySQL.

Import from MySQL Workbench software

MySQL Workbench is open-source as well as Community Edition visual database management software developed by Oracle Corporation. The Community Edition is a full featured product still we can do all basic stuff in Open-source software. We will use Open-source software to import sql file.

First create the connection on MySQL Workbench dashboard and lauch the server.

If you want to create new database, click Create New Schema button from menubar, write database name and apply the changes.

To import the SQL file from local system, click File > Run SQL Script...

Select the database name and default character set like utf8 and click Run button. This will execute the query.

Import from phpMyAdmin

phpMyAdmin is free and open-source web application written in PHP. It is easy to manage MySQL and MariaDB database through phpMyAdmin. Many web hosting providers provides phpMyAdmin tool to manage database. If they not provide phpMyAdmin, we can simply install from the official phpMyAdmin Link[].

This is simple way to import SQL file. First, from your browser login to server phpMyAdmin portal either through Cpanel menu or from your database server link.

Now from the left bar select the database where you want to import the sql file. Now click Import option from menubar. Select the file from file input and click Go button. This will run SQL file to database server.

Import through Command line

This is fast way to import SQL script but it is sometimes not easy and takes time to run proper command line. If you have enough knowledge about bash script and MySQL commands, this will be easiest way.

To import the SQL script file, run below command from your Terminal.

mysql -u database_user -p -h database_name < path/to/sql_file.sql

If this not work for you, first connect to ssh and append mysql command.

ssh remote_user@remote_server mysql -u database_user -ptestpass database_name < path/to/sql_file.sql

There is also one method to import sql file through command line. First upload the sql file to remote server through git or FTP application like FileZilla and you can directly import database.

mysql -u database_user -p database_name < path/to/sql_file.sql

So, this way sql file can be import to remote server. These are just few ways, there are many softwares available to import the sql file.