Import and export mysql db in command line

Importing sql data

For importing large data through phpmyadmin we have to change some setting in the php.ini. Instead of that we can directly import the sql files using command line. There are two ways to do that.

Method 1

First goto the folder where the sql file you have (or) you have to mention the path of that file

mysql -h{hostname} -u{username} -p{password} {db_name} < import.sql

Method 2

First goto the folder where the sql file you have and then type the following command for log in to the mysql

 mysql -h localhost -u root -p{password} 

You will get into the mysql monitor. There you have run the following queries

First select database where you have to import the sql file

i.e.,

 mysql> use {database_name}; 

and then run the following query to import sql file

 mysql> source {sql_file}; 

Export

For export you have to use the following command

 mysqldump -h{hostname} -u{username} -p{password} {db_name} > {path to export}/{export.sql} 

 

One thought on “Import and export mysql db in command line

  1. Hi,
    Your post was so helpful.
    I used method 2 above to make my job done.
    Thanks actually u saved my day 🙂
    Literally, I tried with method 1 @ first but there arises a problem in importing becz password for my localhost is empty i.e.,(I will login without entering any password). if that’s the case , then how wld I import using Method 1. Thanks for your help in advance.

Leave a comment