Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export and import a .sql file from command line with options? [duplicate]

Not Duplicate! looking for some feature have phpmyadmin during export in command line

I want to export and import a .sql file to and from a MySQL database from command line.

Is there any command to export .sql file in MySQL? Then how do I import it?

When doing the export/import, there may be constraints like enable/disable foreign key check or export only table structure.

Can we set those options with mysqldump?

some example of Options

enter image description here

like image 969
AZinkey Avatar asked Jul 10 '12 05:07

AZinkey


3 Answers

Type the following command to import sql data file:

$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

In this example, import 'data.sql' file into 'blog' database using vivek as username:

$ mysql -u vivek -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:

$ mysql -u username -p -h 202.54.1.10 databasename < data.sql

To export a database, use the following:

mysqldump -u username -p databasename > filename.sql

Note the < and > symbols in each case.

like image 56
Frankline Avatar answered Oct 30 '22 18:10

Frankline


If you're already running the SQL shell, you can use the source command to import data:

use databasename;
source data.sql;
like image 34
Big McLargeHuge Avatar answered Oct 30 '22 17:10

Big McLargeHuge


mysqldump will not dump database events, triggers and routines unless explicitly stated when dumping individual databases;

mysqldump -uuser -p db_name --events --triggers --routines > db_name.sql
like image 30
PodTech.io Avatar answered Oct 30 '22 19:10

PodTech.io