Thursday 28 February 2013

Command Line import-export database in Windows 7

Import .csv file

⇛ Step 1: Copy your desired importing file into the mysql using following steps:

Open the wamp installed folder (lets consider it as a D:\ drive)

D:\wamp\bin\mysql\mysql5.5.20\bin

Paste your database file there (in bin folder)

Right click on (Start->All Programs->Accessories->Command Prompt) Command Prompt and click on ''Run as administrator''

Execute the following command:

D:\wamp\bin\mysql\mysql5.5.20\bin
mysql -p -uusername
Example: mysql -p -uroot

Press ''ENTER'', then it will prompt you to enter the password.  Provide the appropriate password to it, then it will go in mysql mode like this:

mysql>

⇛ Step 2: Select your database name using command

use [database name]
Example: use mydatabase

⇛ Step 3: Here is the following command for importing .csv file (''Make sure that you copied your desired files into bin folder'')

load data local infile 'filename.csv' into table tablename fields terminated by 'terminate character' enclosed by 'enclosed character' lines terminated by 'terminate character'(col1,col2,@ignore, col4);

Example:
load data local infile 'user.csv' into table user fields terminated by '|' enclosed by ' ” ' lines terminated by '\n' (id,username,@ignore, password);

''@ignore'' specifies the extra column in the csv which you don't want to import into the table.

Import .sql file

⇛ Step 1: Use ''same'' step 1 as Import.csv file section

⇛ Step 2: After that for importing .sql from command line you need to use following command

mysql -uusername -p databasename < filename.sql
Example'': mysql -uroot -p mydatabase < user.sql

Export database to the .sql file

⇛ Step 1: Open the command prompt ('''Run as administrator''') and execute following command:

D:\wamp\bin\mysql\mysql5.5.20\bin>

⇛ Step 2: For exporting database from command line you need to use following command

mysqldump -uusername -p databasename > filename.sql
Example: mysqldump -uroot -p mydatabase > mynewdb.sql

Export table to the .sql file

⇛ Step 1: Open the command prompt (''Run as administrator'') and execute following command:

D:\wamp\bin\mysql\mysql5.5.20\bin>

⇛ Step 2: For exporting table from command line you need to use following command

mysqldump -uusername -p databasename tablename > filename.sql
Example: mysqldump -uroot -p mydatabase mytable > newtable.sql

Export Multiple tables to the .sql file

mysqldump -u username -p databasename table1 [table2 ...]> path/to/dump.sql
Example: mysqldump -uroot -p mydatabase tb1 tb2 tb3 > tbsdetails.sql