How to import an SQL file using the command line in MySQL

Sometimes you need to import database scripts, in this post I will show how to install and import a script in MySQL.

I will assuming that you are using distributions based on ubuntu.

Install MySQL

Firstly, you need to install MySQL.

To install MySQL 5.6 execute the following command:

sudo apt-get update
sudo apt-get install mysql-server-5.6

To test check your MySQL version

mysql --version

If you have never set a root password for MySQL, use the mysqladmin command at the shell prompt as follows:

mysqladmin -u root password newpass

Import script

Type the following command to import sql data file:

mysql -u username -p <db name> < dump.sql

You can ignore <db name> if into your script have a command to create a new database.

An alternative you can use source command as follows:

mysql -u root -p 
mysql> source /home/dump.sql

Useful commands

To list all databases.

mysql> show databases;

Switch a database.

mysql> use

To see all tables in the db.

mysql> show tables;

To see table sctructure.

mysql> describe;

See you.