Tuesday, June 2, 2015

[MySql] Importing Data to Mysql From SQL file

Hi,

Its very often that we need to import data to mysql. Sometimes we have database already created, sometimes does not.

Below is the syntax to import data. When we do have a database already created and want to use that.
mysql -u ravi -p mydatabasename < databasefile.sql

This will prompt for password and import data to mydatabasename.

When we do not have a database already but create database command is written in the file and the same database is used.
mysql -u ravi -p < databasefile.sql

This will also prompt for password because we used -p option which is for password. For username we used -u. Remember that if we want to create a database then user should have appropriate permissions.

That's it.

Hope this helps someone.